Hdu 1814 Peaceful Commission(2-sat+输出字典序最小方案)
来源:互联网 发布:java dom4j解析xml文件 编辑:程序博客网 时间:2024/05/23 20:45
题目链接
Peaceful Commission
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2018 Accepted Submission(s): 585
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
题解:裸的2-sat问题,要输出字典序最小的方案,用O(n*m)的算法来做就行了。
代码如下:
#include<stdio.h>#include<iostream>#include<algorithm>#include<stack>#include<string.h>#define inff 0x3fffffff#define nn 16100using namespace std;int n,m;struct node{ int st,en,next;}E[nn*10];int p[nn],num;void init(){ memset(p,-1,sizeof(p)); num=0;}void add(int st,int en){ E[num].en=en; E[num].next=p[st]; p[st]=num++;}int zh(int x){ if(x%2) return x+1; else return x-1;}bool use[nn];int vis[nn],ls;bool dfs(int x){ vis[ls++]=x; use[x]=true; int i,w; for(i=p[x];i+1;i=E[i].next) { w=E[i].en; if(use[w]) continue; else if(use[zh(w)]) return false; else if(!dfs(w)) return false; } return true;}int main(){ int i; int u,v; while(scanf("%d%d",&n,&m)!=EOF) { init(); memset(use,false,sizeof(use)); for(i=1;i<=m;i++) { scanf("%d%d",&u,&v); add(u,zh(v)); add(v,zh(u)); } for(i=1;i<=n;i++) { if(!use[2*i]&&!use[2*i-1]) { ls=0; if(dfs(2*i-1)) { continue; } else { for(int j=0;j<ls;j++) use[vis[j]]=false; ls=0; if(dfs(2*i)) { continue; } else break; } } } if(i<=n) { puts("NIE"); } else { for(i=1;i<=2*n;i++) { if(use[i]) { printf("%d\n",i); } } } } return 0;}
0 0
- Hdu 1814 Peaceful Commission(2-sat+输出字典序最小方案)
- HDU 1814Peaceful Commission (最小字典序 2-SAT)
- hdu 1814 Peaceful Commission (2-sat 输出字典序最小路径)
- hdu 1814 Peaceful Commission(2-SAT 输出字典序最小的解)
- HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方案)
- HDOJ 1814 - Peaceful Commission 输出字典序最小的2-sat方案...暴力2-sat
- HDU 1814 Peaceful Commission(2-SAT:最小字典序)
- hdu 1814 Peaceful Commission 2-sat 按字典序输出
- HDU 1814 Peaceful Commission 2-sat(字典序最小的解)
- HDU 1814 Peaceful Commission(2-sat 输出字典序最小解 )
- HDU 1814 Peaceful Commission(2-sat)
- HDU 1814 Peaceful Commission (2-SAT)
- hdu 1814 Peaceful Commission (2-SAT)
- HDU 1814 Peaceful Commission (2-SAT)
- HDU 1814:Peaceful Commission(2-SAT的字典序最小解)
- HDU1814.Peaceful Commission——2-sat输出字典序最小的解
- [2-SAT 字典序最小解 暴力dfs 模板题] HDU 1814 Peaceful Commission
- 【HDU】1814 Peaceful Commission 2-sat
- leetcode:Find Minimum in Rotated Sorted Array II
- 8.25在操作UI时不让定时器停止的方法 模态
- 五子棋(一)
- 基于第三方QQ授权登录和新浪微博授权登录的iOS代码分析
- 【android】应用架构一一一一一Activity和Fragment的对比分析
- Hdu 1814 Peaceful Commission(2-sat+输出字典序最小方案)
- 如何更改AVD的默认路径
- Jquery,图片依次进入内容区域,幻灯片
- do you know why localhost=127.0.0.1
- hdu 4770 Lights Against Dudely
- 见机行事【Nhibernate懒加载】
- Android 开源框架ActionBarSherlock 和 ViewPager 仿网易新闻客户端
- Java 1028 多态
- 如何为JOPtionPane的showConfirmDialog对话框中的按钮设置监听