Peaceful Commission

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

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*n个人,第i个和第i+1个人属于一组,有m对人互相讨厌(不会是同一组的),问能否让每一组都有且只有一个人参加派对且派对上没有相互讨厌的人。字典序输出参加的人的编号。
代码:
//2-sat模板,白书323页。点的编号从0开始
//输出字典序最小的结果,模板跑出来就是字典序最小的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=;
/********************** 2-sat模板 **********************/
struct Twosat{
int n;
vector<int> g[maxn*];
bool mark[maxn*];
int s[maxn*],c;
bool dfs(int x){
if(mark[x^]) return false;
if(mark[x]) return true;
mark[x]=true;
s[c++]=x;
for(int i=;i<(int)g[x].size();i++)
if(!dfs(g[x][i])) return false;
return true;
}
void init(int n){
this->n=n;
for(int i=;i<n*;i++) g[i].clear();
memset(mark,,sizeof(mark));
}
void add_clause(int x,int y){//这个函数随题意变化
g[x].push_back(y^);//选了x就必须选y^1
g[y].push_back(x^);
}
bool solve(){
for(int i=;i<n*;i+=)
if(!mark[i]&&!mark[i+]){
c=;
if(!dfs(i)){
while(c>) mark[s[--c]]=false;
if(!dfs(i+)) return false;
}
}
return true;
}
};
/*********************** 2-sat模板 ************************/
int main(){
int n,m,a,b;
Twosat solver;
while(~scanf("%d%d",&n,&m)){
solver.init(n);
for(int h=;h<m;h++){
scanf("%d%d",&a,&b);//a,b不能同时选
a--;b--;
solver.add_clause(a,b);
}
if(solver.solve()){
for(int i=;i<n*;i++)
if(solver.mark[i]) printf("%d\n",i+);
}
else printf("NIE\n");
}
return ;
}

HDU1814 2-sat 模板的更多相关文章

  1. 2 - sat 模板(自用)

    2-sat一个变量两种状态符合条件的状态建边找强连通,两两成立1 - n 为第一状态(n + 1) - (n + n) 为第二状态 例题模板 链接一  POJ 3207 Ikki's Story IV ...

  2. HDU1814(Peaceful Commission) 【2-SAT DFS暴力求最小字典序的模板】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:给出一个数n,代表有n个党派,每个党派要求派出其中一个人去参加会议,且只能派出一人.给出m ...

  3. 2-SAT模板

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAABcAAAAXCAYAAADgKtSgAAAFCUlEQVR42n2VeVCUdRzGf3n0T/80ld

  4. TwoSAT算法模板

    该模板来自大白书 [解释] 给多个语句,每个语句为“ Xi为真(假) 或者 Xj为真(假)” 每个变量和拆成两个点 2*i为假, 2*i+1为真 “Xi为真 或 Xj为真”  等价于 “Xi为假 –& ...

  5. 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 ...

  6. C++ 模板基础

    我们学习使用C++,肯定都要了解模板这个概念.就我自己的理解,模板其实就是为复用而生,模板就是实现代码复用机制的一种工具,它可以实现类型参数化,即把类型定义为参数:进而实现了真正的代码可重用性.模版可 ...

  7. HDU3062&&HDU1814

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

  8. (模板)poj2947(高斯消元法解同余方程组)

    题目链接:https://vjudge.net/problem/POJ-2947 题意:转换题意后就是已知m个同余方程,求n个变量. 思路: 值得学习的是这个模板里消元用到lcm的那一块.注意题目输出 ...

  9. 虚拟化技术之kvm镜像模板制作工具virt-sysprep

    virt-sysprep这个工具来自libguest-tools这个工具包,它能够把kvm虚拟机对应的磁盘文件做成一个模板,后续我们启动虚拟机就可以基于这个镜像模板启动:什么是镜像模板呢?所谓模板就是 ...

随机推荐

  1. 关于TensorFlow的GPU设置

    摘自:https://blog.csdn.net/byron123456sfsfsfa/article/details/79811286 1.  在使用GPU版的TensorFlow跑程序的时候,如果 ...

  2. Paper Reading - Learning like a Child: Fast Novel Visual Concept Learning from Sentence Descriptions of Images ( ICCV 2015 )

    Link of the Paper: https://arxiv.org/pdf/1504.06692.pdf Innovations: The authors propose the Novel V ...

  3. Python中如何Getting Help

    在Python中Gettting Help有如下两种方法: 1 使用dir函数,dir的参数可以是一个真正的对象实例,也可以是一个数据类型,无论哪种情形,dir函数都返回与这个对象或者数据类型相关联的 ...

  4. 11.22Daily Scrum

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.979 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.978 实现视频浏览的功能 王 ...

  5. Git使用笔记一(关于如何设置密钥及提交)(Windows)

    如何设置密钥 ssh-keygen -t rsa或ssh-keygen -t rsa -C ‘邮箱’ (注意 1.-t前有一个空格:2.keygen是key generate的缩写:3.而后连续输入三 ...

  6. joomla 出现The file Cache Storage is not supported on this platform;

    错误提示:The file Cache Storage is not supported on this platform:在这个平台上不支持文件缓存存储 出现这样的原因很简单,有两个文件夹不可写,这 ...

  7. iOS-明杰解决字段冲突,及数组映射

    /** 替换关键字的属性名 */ + (NSDictionary *)mj_replacedKeyFromPropertyName{ return @{@"UUID":@" ...

  8. Debian 7 amd64问题

    Debian 7 发布了有1段时间,最近才在自己的电脑硬盘安装,用户体验还算可以.在安装Debian的过程中,有问题还是要记录一下的. 注意:遇到的问题跟硬件体系相关,可能在个别电脑没法重现. 1.默 ...

  9. sublime Remote_encoding cp1252

    "remote_encoding": "cp1252",才能连接远程ftp

  10. js alert()后进行跳转的方法

    如果alert()之后再进行跳转本页,按以下方法你将等不到alert(),浏览器就本身刷新本页面了 <script type="text/javascript"> al ...