Peaceful commissiontime limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1948 accepted submission (s): 560
Problem descriptionthe Public Peace Commission shocould 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.
Inputin 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.
Outputthe text file SPO. out shoshould 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 shoshould 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 shoshould 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
Sourcepoi 2001
Now there are n parties and each party has 2 representatives. We need to select one representative from each party to form a legislative committee for N people. however, some representatives may hate each other, so they cannot appear in the Legislative Council at the same time. ask if you have a reasonable plan and output the minimum lexicographic results of all possible legislative committees.
Idea: The Naked 2-Sat question is mainly about the output path, creating a Graph Based on the given contradiction, and finally outputting the path based on whether Mark is marked.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 8005#define MAXN 400005#define OO (1<<31)-1#define mod 1000000007#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;struct TwoSAT{ int n; vector<int>g[maxn*2]; bool mark[maxn*2]; int S[maxn*2],c; bool dfs(int x) { if(mark[x^1]) return false ; if(mark[x]) return true ; mark[x]=true ; S[c++]=x; for(int i=0; i<g[x].size(); i++) { if(!dfs(g[x][i])) return false ; } return true ; } void init(int n) { this->n=n; for(int i=0; i<n*2; i++) g[i].clear(); memset(mark,0,sizeof(mark)); } void add_clause(int x,int xval,int y,int yval) { x=x*2+xval; y=y*2+yval; g[x^1].push_back(y); g[y^1].push_back(x); } bool solve() { for(int i=0; i<n*2; i+=2) { if(!mark[i]&&!mark[i+1]) { c=0; if(!dfs(i)) { while(c>0) mark[S[--c]]=false ; if(!dfs(i+1)) return false ; } } } return true ; }};int n,m,sum;int age[maxn];TwoSAT ts;int main(){ int i,j; while(~scanf("%d%d",&n,&m)) { ts.init(n); int u,v,x,y; for(i=1; i<=m; i++) { scanf("%d%d",&u,&v); u--; v--; ts.g[u].push_back(v^1); ts.g[v].push_back(u^1); } if(ts.solve()) { for(i=0; i<n*2; i+=2) { if(ts.mark[i]) printf("%d\n",i+1); else printf("%d\n",i+2); } } else puts("NIE"); } return 0;}
HDU 1814 peaceful Commission (2-Sat output lexicographic minimum path)