hdu1814 Peaceful Commission(2-sat)
来源:互联网 发布:牛津简明英语词典 知乎 编辑:程序博客网 时间:2024/06/05 15:04
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.
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.
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 21 32 4
Sample Output
145
Source
POI 2001
题意:共和国要组建委员会,现在有N个党派,每个党派中有两人,从1开始每连续两个位一个党派,而且有且只有一人能参加委员会,但是现在m个不同党派间有些人有矛盾,因此这些人是不能同时属于委员会的。现在要你求出每个党派的派出人选,如果有多组解,则输出字典序最小的那组解。
思路:
对于每一次染色,如果不能true,我们将把颜色染回来;
每次循环top归0,是对于之前成立的我们无需重新染色;
代码:
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int M=40005;const int N=20005;struct node{ int now,next;}str[M];int head[N],s[N];bool vis[N];int tot,top;void add(int x,int y){ str[tot].now=y; str[tot].next=head[x]; head[x]=tot++;}bool dfs(int u){ if(vis[u^1]) return false; //该党派已有人派出,产生矛盾 if(vis[u]) return true; //此人已经派出 vis[u]=true; s[top++]=u; //记录该次搜索的点 for(int i=head[u];i!=-1;i=str[i].next) { int v=str[i].now; if(!dfs(v)) return false; } return true;}bool judge(int n){ for(int i=0;i<2*n;i+=2) { if(!vis[i]&&!vis[i+1]) //剩下的未染色的党派 { top=0; //对于之前成立的,用vis记录的我们无需重置 if(!dfs(i)) //如果存在矛盾 { while(top>0) //将颜色染回来 vis[s[--top]]=false; if(!dfs(i+1)) return false; } } } return true;}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { tot=0; memset(vis,false,sizeof(vis)); memset(head,-1,sizeof(head)); for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); x--;y--; //x--有利于异或运算; add(x,y^1); add(y,x^1); } if(!judge(n)) puts("NIE"); else { for(int i=0;i<2*n;i++) if(vis[i]) printf("%d\n",i+1); } } return 0;}
0 0
- hdu1814 Peaceful Commission,2-sat
- hdu1814 Peaceful Commission 2-sat
- hdu1814 Peaceful Commission(2-sat)
- hdu1814 Peaceful Commission 2-SAT
- [HDU1814]Peaceful Commission(2-SAT)
- hdu1814 Peaceful Commission(2-sat)
- 【2-SAT字典序最小解】HDU1814[Peaceful Commission]题解
- HDU1814 Peaceful Commission —— 2-sat 染色法
- HDU1814 Peaceful Commission
- hdu1814 Peaceful Commission--dfs
- HDU1814.Peaceful Commission——2-sat输出字典序最小的解
- 2-sat hoj 和平委员会 Peaceful Commission
- [hit 1917] Peaceful Commission(2-sat)
- 【HDU】1814 Peaceful Commission 2-sat
- 【2-sat】 HDOJ 1814 Peaceful Commission
- HDU 1814 Peaceful Commission(2-sat)
- hdu 1814 Peaceful Commission(2-sat)
- hdu 1814 Peaceful Commission (2-SAT)
- 定理大全
- CSS定位
- java中Scanner
- maven下载jar包
- IOS app中有两个UITabBarController,即在第一个页面有4个按钮,进入子页面底部也有几个按钮
- hdu1814 Peaceful Commission(2-sat)
- 解决Cannot change version of project facet Dynamic web module to 3.0
- xcode 8.0以上ipa闪屏图设置
- 活用SVN-trunk/branches/tags
- Edit remote files with local Vim
- Linux下查看和停止所有java进程
- 【已解决】tomcat启动不成功(点击startup.bat闪退)的解决办法
- java编程思想-第五章-初始化与清理
- java编程思想第4版第15章通配符笔记及练习