首页 > 代码库 > HDU 1814 Peaceful Commission
HDU 1814 Peaceful Commission
Peaceful Commission
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1973 Accepted Submission(s): 572
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 2
1 3
2 4
Sample Output
1
4
5
给出 2*i-1 与 2*i 只能选一个 。
然后m对数存在矛盾关系 。
要求输出2-sat 可行解的最小序 , 如果不存在就输出NIE 。
那么直接每次求到满足解时 ,存入ans数组 。
一旦无法满足跳出输出NIE即可。
#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <map>#include <cmath>#include <vector>#include <algorithm>using namespace std;const int N = (1<<14);int n , m , st[N<<1] ,top , ans[N<<1] , tail ;int eh[N] , et[N<<2] , nxt[N<<2] , tot ;bool mark[N<<2];struct node{ int x , y ;} key[N<<1] , door[N<<1];void init(){ tot = 0 ; memset( eh , -1 , sizeof eh ); memset( mark ,false , sizeof mark );}void addedge( int u , int v ){ et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot++ ; et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot++ ;}bool dfs( int u ){ if( mark[u] ) return true; if( mark[u^1] ) return false ; mark[u] = true ; st[top++] = u ; for( int i = eh[u] ; ~i ; i = nxt[i] ){ int v = et[i]; if( !dfs(v^1) ) return false; } return true;}bool solve(){ tail = 0 ; for( int i = 0 ; i < 2 * n ; i += 2 ){ if( !mark[i] && !mark[i+1] ) { top = 0 ; if( !dfs(i) ){ while( top > 0 ) mark[ st[--top] ] = false ; if( !dfs(i+1) ) return false; } } if( mark[i] ) ans[tail++] = i + 1 ; else ans[tail++] = i + 2 ; } return true;}void run(){ int x, y ; init(); for( int i = 0 ; i < m ; ++i ){ scanf("%d%d",&x,&y); x-- , y -- ; addedge(x,y) ; } if( !solve() )puts("NIE"); else { for( int i = 0 ; i < tail ;++i ) printf("%d\n",ans[i]); }}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL while( ~scanf("%d%d",&n,&m) ) run();}
HDU 1814 Peaceful Commission
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。