Peaceful Commission

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4180    Accepted Submission(s): 1395

Problem Description

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. 

 

Input

In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other. 
There are multiple test cases. Process to end of file. 
 

Output

The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write mininum number sequence. 
 

Sample Input

3 2
1 3
2 4
 

Sample Output

1
4
5
 

Source

 
染色法的2-SAT
  1. //2017-08-28
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const int N = ;
  11. const int M = ;
  12. const int INF = 0x3f3f3f3f;
  13. int head[N], tot;
  14. struct Edge{
  15. int to, next;
  16. }edge[M];
  17.  
  18. void init(){
  19. tot = ;
  20. memset(head, -, sizeof(head));
  21. }
  22.  
  23. void add_edge(int u, int v){
  24. edge[tot].to = v;
  25. edge[tot].next = head[u];
  26. head[u] = tot++;
  27. }
  28.  
  29. bool book[N];
  30. vector<int> vec;//保存dfs过程中经过的点
  31.  
  32. //input: u 顶点
  33. //output: true 从u开始染色,不会出现NOT u和u染为同一种颜色; false dfs染色失败
  34. bool dfs(int u){
  35. if(book[u^])return false;//表示染到非u,染色失败
  36. if(book[u])return true;
  37. book[u] = true;
  38. vec.push_back(u);
  39. for(int i = head[u]; i != -; i = edge[i].next){
  40. int v = edge[i].to;
  41. if(!dfs(v))
  42. return false;
  43. }
  44. return true;
  45. }
  46.  
  47. //input:n 图的顶点数
  48. //output:true 存在可行解; false 不存在可行解
  49. bool twoSAT(int n){
  50. memset(book, , sizeof(book));
  51. for(int u = ; u < n; u += ){
  52. if(book[u] || book[u^])continue;
  53. vec.clear();
  54. if(!dfs(u)){//如果选u不成功,把dfs过程中的点都从答案中删去
  55. for(int i = ; i < vec.size(); i++)
  56. book[vec[i]] = ;
  57. vec.clear();
  58. if(!dfs(u^))return false;//如果选NOT u也不成功,说明不存在可行解
  59. }
  60. }
  61. return true;
  62. }
  63.  
  64. int n, m;
  65.  
  66. int main()
  67. {
  68. std::ios::sync_with_stdio(false);
  69. //freopen("inputG.txt", "r", stdin);
  70. while(cin>>n>>m){
  71. init();
  72. int u, v;
  73. while(m--){
  74. cin>>u>>v;
  75. u--; v--;
  76. add_edge(u, v^);// u -> NOT v
  77. add_edge(v, u^);// v -> NOT u
  78. }
  79. if(twoSAT(n<<)){
  80. for(int i = ; i < (n<<); i++)//字典序输出解
  81. if(book[i])
  82. cout<<i+<<endl;
  83. }else cout<<"NIE"<<endl;
  84. }
  85. return ;
  86. }

HDU1814(2-SAT)的更多相关文章

  1. 多边形碰撞 -- SAT方法

    检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...

  2. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  3. 2—sat

    模型的解决方法看论文<利用对称性解决2-SAT问题> HDU1814 :难度1.5 HDU1824: 难度 2 HDU1815: 难度3 HDU1816: 对于每两个人,二选一HDU181 ...

  4. hdu1814 Peaceful Commission

    hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...

  5. Map Labeler POJ - 2296(2 - sat 具体关系建边)

    题意: 给出n个点  让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置 解析: 当然是二分,但建图就有点还行..比较难想..行吧...我太垃圾... 2 - s ...

  6. HDU1814 Peaceful Commission 2-sat

    原文链接http://www.cnblogs.com/zhouzhendong/p/8099115.html 题目传送门 - HDU1814 题面 Description 根据宪法,Byteland民 ...

  7. HDU3062&&HDU1814

    Preface 两道2-SAT模板题. HDU3062 看题目就一眼2-SAT.一对夫妻看成一个变量,之间的矛盾可以看成限制. 考虑不同席的限制,相当于选了\(i\)就不选\(j\),即必选\(j'\ ...

  8. 学习笔记(two sat)

    关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...

  9. LA 3211 飞机调度(2—SAT)

    https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...

  10. HIT 1917 2—SAT

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

随机推荐

  1. SpringBoot使用ModelAndView时配置视图解析器

    spring boot 使用视图modelandview 原文:https://www.cnblogs.com/liyafei/p/7955943.html 1:springboot使用视图解析器,添 ...

  2. C语言Socket-模拟远程CMD(客户端向服务器发送命令,服务器执行该命令)

    服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...

  3. 12_python_生成器

    一.生成器 python中有三种方式获取生成器 (1)通过生成器函数 (2)通过各种推导式来实现生成器 (3)通过数据的转换也可以获取生成器   1.只要函数中存在了yield,那么这个函数就是一个生 ...

  4. 反射 方法和函数 type

    1. isinstance/issubclass/type  *** issubclass 判断xxx类是否是xxx类的子类 class Animal: pass class Cat(Animal): ...

  5. Akka(20): Stream:异步运算,压力缓冲-Async, batching backpressure and buffering

    akka-stream原则上是一种推式(push-model)的数据流.push-model和pull-model的区别在于它们解决问题倾向性:push模式面向高效的数据流下游(fast-downst ...

  6. Java并发编程总结2——慎用CAS

    一.CAS和synchronized适用场景 1.对于资源竞争较少的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源:而CAS基于硬件实 ...

  7. mysql数据库崩溃:InnoDB: Database page corruption on disk or a failed

    修改mysql配置文件my.cnf,添加 innodb_force_recovery = 6 innodb_purge_thread = 0 重启mysql 这时只可以执行select,create, ...

  8. 开发创建XMPP“发布订阅”扩展(xmpp pubsub extend)

    发布订阅(PubSub)是一个功能强大的XMPP协议扩展.用户订阅一个项目(在xmpp中叫做node),得到通知时,也即当事项节点更新时.xmpp服务器通知用户(通过message格式). 节点类型: ...

  9. (转)WebSphere禁用SSLv3和RC4算法教程

    原文:https://www.cnblogs.com/lsdb/p/7126399.html WebSphere经常会报“SSL 3.0 POODLE攻击信息泄露”和"SSL/TLS 受诫礼 ...

  10. 全网最详细的Sublime Text 3的激活(图文详解)

    不多说,直接上干货! 前期博客 全网最详细的Windows里下载与安装Sublime Text *(图文详解) ZYNGA INC. User License EA7E- 927BA117 84C93 ...