题面

The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others.

The Commission has to fulfill the following conditions:
1.Each party has exactly one representative in the Commission,
2.If two deputies do not like each other, they cannot both belong to the Commission.

Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party .

Task
Write a program, which:
1.reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,
2.decides whether it is possible to establish the Commission, and if so, proposes the list of members,
3.writes the result in the text file SPO.OUT.

题解

基础2-SAT建图,二者取其一,对每个点建两个分点分别代表取这个点/不取这个点,dfs染色取得字典序最小解

代码

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;const int maxn = 20020;
const int maxm = 100010;struct Edge {int v, next;
}e[maxm];int fi[maxn], ecnt;void init() {ecnt = 0;memset(fi, -1, sizeof(fi));
}void add_edge(int u, int v) {e[ecnt] = (Edge) {v, fi[u]};fi[u] = ecnt++;
}bool vis[maxn];
int st[maxn], top;bool dfs(int u) {if(vis[u^1])    return false;if(vis[u])      return true;vis[u] = true;st[top++] = u;for(int i = fi[u]; i != -1; i = e[i].next)if(!dfs(e[i].v))    return false;return true;
}bool T_SAT(int n) {memset(vis, 0, sizeof(vis));for(int i = 0; i < n; i += 2) {if(vis[i] || vis[i^1])  continue;top = 0;if(!dfs(i)) {while(top)  vis[st[--top]] = false;if(!dfs(i^1))   return false;}  }return true;
}int main() {int n, m;int u, v;while(scanf("%d%d", &n, &m) == 2) {init();for(int i = 1; i <= m; i++) {scanf("%d%d", &u, &v);u--;    v--;add_edge(u, v^1);add_edge(v, u^1);}if(T_SAT(n*2)) {for(int i = 0; i < n*2; i++)if(vis[i]) {printf("%d\n", i+1);}} else  printf("NIE\n");}return 0;
}

hdu1814 Peaceful Commission 2-SAT建图入门相关推荐

  1. hdu1814 Peaceful Commission 2-sat

    hdu1814 Peaceful Commission 链接 emm,三个链接,三个都不同 loj随便做 hdu字典序最小 bzoj求合法方案数 思路 loj是任意一组解,直接跑tarjan然后判. ...

  2. 从零开始搭建ROS下无人机激光雷达SLAM——hector_slam建图入门——综述篇

    首先我们看下最终无人机SLAM定位的计算图,然后根据计算图追根溯源分析实现算法在ROS中实现的流程 从图中可以看出总共包含的ros节点有5个,分别所属三个包,其中包1.2是ROS官方现成直接可以使用的 ...

  3. SLAM建图入门(以X2L为例)

    前言 本次的探索是从激光雷达拆开上电开始,到SLAM图建立的过程,中间涉及的专业性知识,比如订阅与发布之类的,可以在B站搜索ROS进行学习 正文 前提条件 Ubuntu20.04(不要使用21.04, ...

  4. hdu1814 Peaceful Commission

    题目链接:戳我 菜得不行了,直到今天才刚开始学2-SAT.... 2-SAT的模板,因为是求最小字典序,所以只能用上限为\(O(nm)\)的最暴力的方法来做... #include<iostre ...

  5. hdu1814 Peaceful Commission,2-sat

    题目大意:一国有n个党派,每个党派在议会中都有2个代表,现要组建和平委员会,要从每个党派在议会的代表中选出1人,一共n人组成和平委员会.已知有一些代表之间存在仇恨,也就是说他们不能同时被选为和平委员会 ...

  6. HDU1814 Peaceful Commission 2SAT

    http://acm.hdu.edu.cn/showproblem.php?pid=1814 输出最小字典序. 1 #include<bits/stdc++.h> 2 const int ...

  7. [hdu-1814] Peaceful Commission题解

    题目传送门 题意解析:题目就是给了你n组,每一组有两个人,然后在给你m组关系,关系x,y表示x和y不能在放一起,然后让你在每组中各取出一个人,找出字典序最小的一组方案(原题是任意一组方案,但是hdu上 ...

  8. walking机器人入门教程-视觉转激光建图-cartographer算法建图

    系列文章目录 walking机器人入门教程-目录 walking机器人入门教程-硬件清单 walking机器人入门教程-软件清单 walking机器人入门教程-测试底盘 walking机器人入门教程- ...

  9. Turbot4机器人入门教程-使用统一建图入口

      系列文章目录: Turbot4机器人入门教程-硬件清单 Turbot4机器人入门教程-软件清单 Turbot4机器人入门教程-NoMachine远程控制 Turbot4机器人入门教程-配置网络 T ...

最新文章

  1. mysql libs 冲突_mysql-libs的冲突
  2. 2016最新cocoapods版本更新,以及多个版本的问题
  3. 简单枚举(算法竞赛入门经典)
  4. 使用Spring Boot自动发布和监视API
  5. Android应用程序显示未读消息计数
  6. fx5u以太网通讯设置_操作示例 | 实现S7300和FX5U的数据交换
  7. python快速入门 数据输出和基本类型 常用的循环遍历等
  8. 【热聘】蚂蚁金服-系统软件和安全资深/高级/专家/工程师
  9. 《嵌入式-STM32开发指南》第三部分 外设篇 - 第4章 超声波测距
  10. java根据书签添加内容_Java PDF书签——添加、编辑、删除、读取书签
  11. 《剑魂之刃》游戏破解
  12. 学问经得起时间考验的傅立叶
  13. mysql/Java服务端对emoji(utf8mb4编码)的支持有关的问题
  14. 从入门到放弃表情包 python_Python从入门到放弃(1)
  15. 手撕LongAdder-add()源码
  16. Your license has expired IDEA过期问题
  17. 操作系统学习笔记:保护
  18. VP8 的败笔 VS H264
  19. java用switch语句算,Java的switch语句与条件运算符
  20. 从50亿图文中提取中文跨模态新基准Zero,奇虎360全新预训练框架超越多项SOTA

热门文章

  1. css如何将div画成三角形
  2. iMeta | 扬州大学杜予州团队揭示同域内同食物的两种昆虫肠道微生物群落装配机制...
  3. 【其它】Mac配置输入法切换快捷键
  4. 外媒:朝鲜导弹发射失败或因美国网络攻击所致
  5. mysql数据库教学系统设计_MySQL-教学系统数据库设计
  6. 【JavaScript笔记 · 基础篇(五)】Array全家桶(引用数据类型中的数组 / Array对象 / Array.prototype)
  7. Spring核心机制IoC与AoP梳理
  8. 推荐一款很好用的调试JS的Eclipse插件
  9. 熬了一夜!我用Python做了一个网站,帮小姐姐生成漫画头像
  10. QQ音乐酷狗音乐竞品分析