题目大意:

有n对的人,编号从1-2*n,m对的人之间互相不喜欢,每对人中必徐选1个人加入和平委员会,求字典序最小的解

————————————————————————————————

2-SAT问题,由于要最小字典序,就不能scc的方法求了,只能暴力染色。O(n*m)

————————————————————————————————

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 const int maxn=16020;
7 const int maxm=4e4+20;
8 int n,m;
9 struct edge
10 {
11 int u,v,nxt;
12 }e[maxm];
13 int head[maxn],js;
14 void addage(int u,int v)
15 {
16 e[++js].u=u;e[js].v=v;
17 e[js].nxt=head[u];head[u]=js;
18 }
19 int clo[maxn],st[maxn],top;
20 bool dfs(int x)
21 {
22 if(clo[x]==1)return 1;
23 if(clo[x]==2)return 0;
24 clo[x]=1;clo[x^1]=2;
25 st[++top]=x;
26 for(int i=head[x];i;i=e[i].nxt)
27 {
28 int v=e[i].v;
29 if(!dfs(v))return 0;
30 }
31 return 1;
32 }
33 bool sat()
34 {
35 memset(clo,0,sizeof clo);
36 for(int i=0;i<(n<<1);i+=2)
37 {
38 if(clo[i])continue;
39 top=0;
40 if(!dfs(i))
41 {
42 while(top)clo[st[top]]=0,clo[st[top--]^1]=0;
43 if(!dfs(i^1))return 0;
44 }
45 }
46 return 1;
47 }
48 void init()
49 {
50 memset(head,0,sizeof head);
51 js=0;
52 memset(clo,0,sizeof clo);
53 memset(st,0,sizeof st);
54
55 }
56 int main()
57 {
58 while(scanf("%d%d",&n,&m)==2)
59 {
60 init();
61 for(int a,b,i=1;i<=m;++i)
62 {
63 scanf("%d%d",&a,&b);
64 --a,--b;
65 addage(a,b^1);
66 addage(b,a^1);
67 }
68 if(sat())
69 {
70 for(int i=0;i<=(n<<1);++i)
71 if(clo[i]==1)printf("%d\n",i+1);
72 }
73 else puts("NIE");
74 }
75 return 0;
76 }

最新文章

  1. 断电不断网——Linux的screen
  2. 你们以为运营商只是HTTP插点广告而已么?
  3. Java 操作Excel 之Poi(第一讲)
  4. linux kernel (proc文件系统)参数
  5. javascript学习初衷
  6. 201521123083《Java程序设计》第11周学习总结
  7. Vscode新建html文件
  8. Django模板语言的复用
  9. BTARN 接收消息流以3A7为例
  10. Linux 系统级开启文件句柄 调优
  11. python零碎知识点
  12. 百度云的ubuntu16.04.1部署Apache服务器+Django项目
  13. vue实现左侧滑动删除
  14. [基础知识]在PeopleSoft中SMTP设置不生效如何查找问题
  15. 【bzoj1044】木棍分割
  16. AI 经典书单 | 人工智能学习该读哪些书
  17. 玩转X-CTR100 l STM32F4 l TB6612直流电机调速控制
  18. 什么是ground truth(GT)
  19. 20165203 第6周《Java程序设计》学习
  20. IDEA+Gradle相关资料

热门文章

  1. 后端Long类型传到前端精度丢失的正确解决方式
  2. 5个有趣且不必要的 JavaScipt 技巧
  3. hive 将hive表数据查询出来转为json对象和json数组输出
  4. 上班如何优雅的使用idea刷LeetCode(力扣)
  5. 如何优雅的传递 stl 容器作为函数参数来实现元素插入和遍历?
  6. IP包头分析
  7. FastAPI学习: 个人博客的后端API
  8. dubbo配置启动时检查
  9. Linux监控工具vmstat命令
  10. 【Spring】Spring的事务管理 - 1、Spring事务管理概述(数据库事务、Spring事务管理的核心接口)