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. 
 

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 mininum number sequence. 
 

Sample Input
3 21 32 4
 

Sample Output
145
 

Source

POI 2001



题意:共和国要组建委员会,现在有N个党派,每个党派中有两人,从1开始每连续两个位一个党派,而且有且只有一人能参加委员会,但是现在m个不同党派间有些人有矛盾,因此这些人是不能同时属于委员会的。现在要你求出每个党派的派出人选,如果有多组解,则输出字典序最小的那组解。


思路:2SAT问题。有N个党派相当于N个变量,每个变量的取值只能2选1。如果两个人相互矛盾,如果第一个党派的第一个人和第二个党派的第一个人有矛盾,那么就从第一个党派的第一个人向第二个党派的第二个人连边,表示,选了这边的一个人就一定要选那边的一个人。然后就跑2-SAT算法,这里我们采用暴力染色的方式,因为这样可以保证字典序最小。 

 对于每一次染色,如果不能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
原创粉丝点击