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:
- Each party has exactly one representative in the Commission,
- 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:
- reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,
- decides whether it is possible to establish the Commission, and if so, proposes the list of members,
- 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 any of them.
Sample Input
3 2
1 3
2 4
Sample Output
1
4
5
最近看了2篇关于2-SAT问题的IOI论文,对2-SAT问题的O(m)时间复杂度的解法也有了一定的了解,找了道POI 2001的题来做,在WA了无数次之后终于过了,跑了1.44s,效率还可以。
2篇论文分别是<<由对称性解2-SAT问题>>和<<2-SAT解法浅析>>。
//2-SAT问题
//求出所有强连通分量,如果有矛盾点同处于一个连通分量则无解
//缩点,将原图反向建立DAG图
//按拓扑排序着色,找一个未着色点x,染成红色
//将与x矛盾的顶点及其子孙染为蓝色
//直到所有顶点均被染色,红色即为2-SAT的一组解
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 16010;//2*8000
char color[MAXN];//染色
bool visit[MAXN];
queue<int> q1,q2;
vector< vector<int> > adj; //原图
vector< vector<int> > radj;//逆向图
vector< vector<int> > dag; //缩点后的逆向DAG图
int n,m,cnt,id[MAXN],order[MAXN],ind[MAXN];//强连通分量,访问顺序,入度
void dfs(int u){
visit[u]=true;
int i,len=adj[u].size();
for(i=0;i<len;i++)
if(!visit[adj[u][i]])
dfs(adj[u][i]);
order[cnt++]=u;
}
void rdfs(int u){
visit[u]=true;
id[u]=cnt;
int i,len=radj[u].size();
for(i=0;i<len;i++)
if(!visit[radj[u][i]])
rdfs(radj[u][i]);
}
void korasaju(){
int i;
memset(visit,false,sizeof(visit));
for(cnt=0,i=1;i<=2*n;i++)
if(!visit[i]) dfs(i);
memset(id,0,sizeof(id));
memset(visit,false,sizeof(visit));
for(cnt=0,i=2*n-1;i>=0;i--)
if(!visit[order[i]])
cnt++,rdfs(order[i]);
}
bool solvable(){
for(int i=1;i<=n;i++)
if(id[2*i-1]==id[2*i])
return false;
return true;
}
void topsort(){
int i,j,len,now,p,pid;
while(!q1.empty()){
now=q1.front();
q1.pop();
if(color[now]!=0) continue ;
color[now]='R';
ind[now]=-1;
for(i=1;i<=2*n;i++){
if(id[i]==now){
p=(i%2)?i+1:i-1;
pid=id[p];
q2.push(pid);
while(!q2.empty()){
pid=q2.front();
q2.pop();
if(color[pid]=='B') continue ;
color[pid]='B';
int len=dag[pid].size();
for(j=0;j<len;j++)
q2.push(dag[pid][j]);
}
}
}
len=dag[now].size();
for(i=0;i<len;i++){
ind[dag[now][i]]--;
if(ind[dag[now][i]]==0) q1.push(dag[now][i]);
}
}
}
int main(){
int i,j,x,y,xx,yy,len;
while(scanf("%d %d",&n,&m)!=EOF){
adj.assign(2*n+1,vector<int>());
radj.assign(2*n+1,vector<int>());
for(i=0;i<m;i++){
scanf("%d %d",&x,&y);
xx=(x%2)?x+1:x-1;
yy=(y%2)?y+1:y-1;
adj[x].push_back(yy);
adj[y].push_back(xx);
radj[yy].push_back(x);
radj[xx].push_back(y);
}
korasaju();
if(!solvable()) puts("NIE");
else{
dag.assign(cnt+1,vector<int>());
memset(ind,0,sizeof(ind));
memset(color,0,sizeof(color));
for(i=1;i<=2*n;i++){
len=adj[i].size();
for(j=0;j<len;j++)
if(id[i]!=id[adj[i][j]]){
dag[id[adj[i][j]]].push_back(id[i]);
ind[id[i]]++;
}
}
for(i=1;i<=cnt;i++)
if(ind[i]==0) q1.push(i);
topsort();
for(i=1;i<=n;i++){
if(color[id[2*i-1]]=='R') printf("%d\n",2*i-1);
else printf("%d\n",2*i);
}
}
}
return 0;
}