经典2sat裸题,dfs的2sat能够方便输出字典序最小的解...

Peaceful Commission

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

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
POI 2001
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int maxn=100000;struct Edge
{int to,next;
}edge[maxn];int Adj[maxn],Size;void init()
{Size=0; memset(Adj,-1,sizeof(Adj));
}void Add_Edge(int u,int v)
{edge[Size].to=v;edge[Size].next=Adj[u];Adj[u]=Size++;
}bool vis[maxn];
int top,S[maxn];bool dfs(int x)
{if(vis[x^1]) return false;if(vis[x]) return true;S[top++]=x; vis[x]=true;for(int i=Adj[x];~i;i=edge[i].next){if(!dfs(edge[i].to)) return false;}return true;
}bool SAT2(int n)
{memset(vis,false,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[S[--top]]=false;if(!dfs(i^1)) return false;}}return true;
}int main()
{int n,m,a,b;while(scanf("%d%d",&n,&m)!=EOF){init();while(m--){scanf("%d%d",&a,&b);a--;b--;Add_Edge(a,b^1);Add_Edge(b,a^1);}bool t=SAT2(2*n);if(t){for(int i=0;i<2*n;i++){if(vis[i])printf("%d\n",i+1);}}else puts("NIE");}return 0;
}

HDOJ 1814 Peaceful Commission相关推荐

  1. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)...

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  2. HDU 1814 Peaceful Commission(2-SAT)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1814 题意: 要建立一个和平委员会,要满足以下条件: 每个党派都在委员会中恰有1个代表, ...

  3. HDU 1814 Peaceful Commission

    2-SAT,输出字典序最小的解,白书模板. //TwoSAT输出字典序最小的解的模板 //注意:0,1是一组,1,2是一组..... #include<cstdio> #include&l ...

  4. [HDU 1814] Peaceful Commission

    一.题目 点此看题 二.解法 这就是2-sat\text{2-sat}2-sat带字典序最小解的经典问题,时间复杂度O(n2)O(n^2)O(n2),还是结合代码讲更好: #include <c ...

  5. hdu 1814 Peaceful Commission 题解

    题目传送门 题目大意: 有 nnn 个团队,每个团队两个人,现在要组成一个 nnn 人的组织,要求每个团队中只能有 111 个人在组织里,给出 mmm 组憎恶关系,相互憎恶的两人不能同时在组织里,给出 ...

  6. hdu1814 Peaceful Commission 2-sat

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

  7. hdu1814 Peaceful Commission 2-SAT建图入门

    题面 The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byte ...

  8. HDU1814 Peaceful Commission 2SAT

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

  9. hdu1814 Peaceful Commission

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

最新文章

  1. 国内较强的NLP高校实验室有哪些?
  2. 一文读懂tomcat组件--一个web服务器的架构演化史
  3. 请说明一下Spring中BeanFactory和ApplicationContext的区别是什么?
  4. 禁止修改varchar到int|[运维规范]
  5. docker运行jar包
  6. WPF和Winform中picturebox图片局部放大
  7. vue中this.$set的用法
  8. 最小二乘法与极大似然估计
  9. 微型计算机原理与接口技术课后答案,微机原理与接口技术-课后习题答案
  10. 博科300 java配置,博科300 光纤交换机如何设置为SSH登录?
  11. 高通mtk手机常用指令
  12. 部署 - 前端部署https服务,并配置安全证书
  13. 教务系统mysql注入的原理_SQL注入原理及具体步骤
  14. 1人工智能概述------人工智能发展历程(人工智能的起源、人工智能的发展经历了六个阶段)
  15. 架构师之路(二)-概念架构
  16. 太秀了!程序媛小姐姐写出代码版《本草纲目》,刘畊宏回复:很cool!
  17. 虚拟摄像头/无人直播效果分析,可替抖音/微信
  18. 【网络工程】计算机网络专业术语概论全面整理
  19. python给一个不多于 5 位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字
  20. 什么是Bugzilla?

热门文章

  1. 【环信IM集成指南】iOS端常见问题整理(2)
  2. Python数据分析中数据预处理:编码将文字型数据转换为数值型
  3. 【CSS】CSS 文本样式 ② ( font 字体设置 | CSS 2.0手册使用 | font-weight 字体粗细设置 | font-style 字体斜体设置 | font 字体样式综合写法 )
  4. 2023年中职网络安全技能竞赛网络安全事件响应解析(保姆级)
  5. scandir、direct的用法
  6. idea去掉不想commit的文件
  7. 小散量化炒股记|基于多任务爬虫技术, 实现A股实时行情Level1采样
  8. date_sub() 函数
  9. Debian查看系统版本
  10. 计算机培训软文,软文诊断62期: 学写软文标题,只需懂电脑开机即可