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.
 

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 

题解:裸的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
原创粉丝点击