图论--2-SAT--HDU/HDOJ 1814 Peaceful Commission
Peaceful Commission
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2775 Accepted Submission(s): 865
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
N个党派要成立一个委员会,此委员会必须满足下列条件:
每个党派都在委员会中恰有1个代表,
如果2个代表彼此厌恶,则他们不能都属于委员会。
每个党在议会中有2个代表。代表从1编号到2n。 编号为2i-1和2i的代表属于第I个党派。
现给出M个矛盾关系,问你该委员会能否创立?若不能输出NIE,若能够创立输出字典序最小的解。
最小字典序只能用暴力染色。初始时均没有染色。枚举将党派第一个人染成红色,然后dfs把和它相连的全部染成红色,如果其中有的是蓝色那么矛盾;如果第一种情况矛盾那么dfs第二个人染成红色,如果也矛盾说明无解。
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define MAXN 40000+10
#define MAXM 200000+10
#define INF 1000000
using namespace std;
struct Edge
{
int from, to, next;
}edge[MAXM];
int head[MAXN], edgenum;
stack<int> S;
bool mark[MAXN];//标记是否为真
int N, M;
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
memset(mark, false, sizeof(mark));
}
void addEdge(int u, int v)
{
Edge E = {u, v, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
void getMap()
{
int a, b;
while(M--)
{
scanf("%d%d", &a, &b);
a--, b--;
addEdge(a, b ^ 1);
addEdge(b, a ^ 1);
}
}
bool dfs(int u)
{
if(mark[u ^ 1]) return false;
if(mark[u]) return true;
mark[u] = true;
S.push(u);
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!dfs(v)) return false;
}
return true;
}
bool solve()
{
for(int i = 0; i < 2*N; i+=2)//共N组
{
if(!mark[i] && !mark[i ^ 1])//还没有判定
{
while(!S.empty())//用STL的话 注意清空栈
{
S.pop();
}
if(!dfs(i))
{
while(!S.empty())
{
int v = S.top();
S.pop();
mark[v] = false;
}
if(!dfs(i ^ 1))//矛盾 必无解
return false;
}
}
}
return true;
}
int main()
{
while(scanf("%d%d", &N, &M) != EOF)
{
init();
getMap();
if(solve())
{
for(int i = 0; i < 2*N; i++)
if(mark[i]) printf("%d\n", i+1);
}
else
printf("NIE\n");
}
}
图论--2-SAT--HDU/HDOJ 1814 Peaceful Commission的更多相关文章
- HDOJ 1814 Peaceful Commission
经典2sat裸题,dfs的2sat能够方便输出字典序最小的解... Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Mem ...
- 【HDU】1814 Peaceful Commission
http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:n个2人组,编号分别为2n和2n+1,每个组选一个人出来,且给出m条关系(x,y)使得选了x就不能选 ...
- 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 ...
- hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 Problem Description The Public Peace Commission ...
- 【HDOJ】1814 Peaceful Commission
2-SAT基础题目. /* 1814 */ #include <iostream> #include <vector> #include <algorithm> # ...
- HDU 1814 Peaceful Commission
2-SAT,输出字典序最小的解,白书模板. //TwoSAT输出字典序最小的解的模板 //注意:0,1是一组,1,2是一组..... #include<cstdio> #include&l ...
- 图论--差分约束--HDU\HDOJ 4109 Instrction Arrangement
Problem Description Ali has taken the Computer Organization and Architecture course this term. He le ...
- Peaceful Commission
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- 抓包——HTTP分析
1.什么是HTTP请求(底层使用scoket TCP技术) HTTP是超文本传输协议.底层使用的scoket tcp长连接.基于请求和响应 同步请求. 2.重定向底层: 重定向原理:为什么会产生 ...
- Java 方法之形参和实参 、堆、栈、基本数据类型、引用数据类型
* 形式参数:用于接收实际参数的变量(形式参数一般就在方法的声明上) * 实际参数:实际参与运算的变量 * 方法的参数如果是基本数据类型:形式参数的改变不影响实际参数. * * 基本数据类型:byte ...
- 二、Centos7—U盘启动盘制作
1,准备一个8gU盘. 2.iso系统镜像文件. 3.下载UltraISO软件制作启动盘. 4.运行UltraISO软件 5.在UltraISO软件中打开刚下载的Centos系统安装文件 6.开始 ...
- spark模型运行时无法连接摸个excutors异常org.apache.spark.shuffle.FetchFailedException: Failed to connect to xxxx/xx.xx.xx.xx:xxxx
error:org.apache.spark.shuffle.FetchFailedException: Failed to connect to xxxx/xx.xx.xx.xx:xxxx 定位来定 ...
- 使用docker-compose编写常规的lnmp容器,pdo连接mysql失败。
问题的核心是yii2 是通过pdo的方式去连接数据的.但是我们通过容器去搭建lnmp环境时,nginx , php , mysql 这三个服务是独立的三个容器,彼此隔离.所以在yii2中连接mysql ...
- AJ学IOS 之ipad开发qq空间项目横竖屏幕适配
AJ分享,必须精品 一:效果图 先看效果 二:结构图 如图所示: 其中用到了UIView+extension分类 Masonry第三方框架做子控制器的适配 NYHomeViewController对应 ...
- 【Java】 语言基础习题汇总 [1] 基础概念到数组
1 JDK JRE JVM 三种之间的关系,以及JDK JRE 包含的主要结构有哪些? JDK = JRE + 开发工具 javac.exe java.exe javadoc.exe等等 JRE = ...
- 怎么在执行Python脚本时,密码等敏感信息也不让它出现
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http ...
- redis 分布式锁的 5个坑,真是又大又深
引言 最近项目上线的频率颇高,连着几天加班熬夜,身体有点吃不消精神也有些萎靡,无奈业务方催的紧,工期就在眼前只能硬着头皮上了.脑子浑浑噩噩的时候,写的就不能叫代码,可以直接叫做Bug.我就熬夜写了一个 ...
- 视频+图文 String类干货向总结
目录 一.字符串的介绍及视频讲解 二.字符串的两种创建方式 方法一:通过new创建 方法二:直接创建 三.字符串的长度获取:length()方法 四.使用 == 和equals()方法比较两个字符串 ...