Poj 3648 Wedding【2-Sat--------Tarjan强连通+缩点染色+拓扑排序】

来源:互联网 发布:php电影网站免费源码 编辑:程序博客网 时间:2024/05/21 09:55

Wedding

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 9536

 

Accepted: 2897

 

Special Judge

Description

Up to thirty couples will attend a wedding feast, at which they will be seated on either side of a long table. The bride and groom sit at one end, opposite each other, and the bride wears an elaborate headdress that keeps her from seeing people on the same side as her. It is considered bad luck to have a husband and wife seated on the same side of the table. Additionally, there are several pairs of people conducting adulterous relationships (both different-sex and same-sex relationships are possible), and it is bad luck for the bride to see both members of such a pair. Your job is to arrange people at the table so as to avoid any bad luck.

Input

The input consists of a number of test cases, followed by a line containing 0 0. Each test case gives n, the number of couples, followed by the number of adulterous pairs, followed by the pairs, in the form "4h 2w" (husband from couple 4, wife from couple 2), or "10w 4w", or "3h 1h". Couples are numbered from 0 to n - 1 with the bride and groom being 0w and 0h.

Output

For each case, output a single line containing a list of the people that should be seated on the same side as the bride. If there are several solutions, any one will do. If there is no solution, output a line containing "bad luck".

Sample Input

10 6

3h 7h

5w 3w

7h 6w

8w 3w

7h 3w

2w 5h

0 0

Sample Output

1h 2h 3w 4h 5h 6h 7h 8h 9h

Source

Waterloo Local Contest, 2007.9.29

 

题目大意:

将n对夫妇,按照如图所示的分配座位,要求输出ow一侧的可行解,其中约束条件为:

对于ow对面的那一列中:

1、不能有一对夫妇出现。

2、不能有通奸的夫妇出现(输入的边的内容就是通奸夫妇的编号)



思路:


1、首先对于能否有解的判断:

输入的边就是矛盾边,给出(u,v)有矛盾,那么建边(u,!v)和(v,!u),构成图,然后求一遍强连通,对于一对夫妇如果属于一个强连通分量,那么就是没有解的情况,否则有解。


2、对于可行解的选择:

①求完强连通之后,缩点染色,将图转化为一个DAG(有向无环图)图。

②然后反向建边,对于拓扑排序排序出来的点,如果当前点没有颜色,那么对其染色为1,同时,对其矛盾点(夫妇点)染色为2,那么染色为1的点,就是和0h一边的点,那么染色为2的点,也就是和0w一边的点。

③遍历每一对夫妇,如果妻子和0w是同颜色的点,那么选择这个妻子坐在和0w一边,否则选择丈夫和0w一边。


Ac代码:


#include<stdio.h>#include<queue>#include<string.h>#include<iostream>#include<vector>using namespace std;int head[100000];struct EdgeNode{    int to;    int w;    int next;}e[1000000];int n,m,cont,sig,cnt,tt;int conflict[100000];vector<int >mp[100000];int degree[100000];int col[100000];int vis[100000];int low[100000];int dfn[100000];int color[100000];int stack[100000];void add(int from,int to){    e[cont].to=to;    e[cont].next=head[from];    head[from]=cont++;}void Tarjan(int u){    vis[u]=1;    dfn[u]=low[u]=cnt++;    stack[++tt]=u;    for(int i=head[u];i!=-1;i=e[i].next)    {        int v=e[i].to;        if(vis[v]==0)Tarjan(v);        if(vis[v]==1)low[u]=min(low[u],low[v]);    }    if(dfn[u]==low[u])    {        sig++;        do        {            vis[stack[tt]]=-1;            color[stack[tt]]=sig;        }        while(stack[tt--]!=u);    }}void top(){    queue<int >s;    memset(col,0,sizeof(col));    for(int i=1;i<=sig;i++)    {        if(degree[i]==0)        {            s.push(i);            degree[i]=-1;        }    }    while(!s.empty())    {        int u=s.front();        s.pop();        if(col[u]==0)        {            col[u]=1;            col[conflict[u]]=2;        }        for(int i=0;i<mp[u].size();i++)        {            int v=mp[u][i];            degree[v]--;            if(degree[v]==0)            {                degree[v]=-1;                s.push(v);            }        }    }}int Slove(){    sig=0;    cnt=1;    tt=-1;    memset(stack,0,sizeof(stack));    memset(color,0,sizeof(color));    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(vis,0,sizeof(vis));    for(int i=1;i<=n*2;i++)    {        if(vis[i]==0)Tarjan(i);    }    memset(conflict,0,sizeof(conflict));    for(int i=1;i<=n*2;i+=2)    {        if(color[i]==color[i+1])return 0;        int u=color[i],v=color[i+1];        conflict[u]=v;        conflict[v]=u;    }    memset(degree,0,sizeof(degree));    for(int i=1;i<=n*2;i++)mp[i].clear();    for(int i=1;i<=n*2;i++)    {        for(int j=head[i];j!=-1;j=e[j].next)        {            int v=e[j].to;            if(color[i]!=color[v])            {                mp[color[v]].push_back(color[i]);                degree[color[i]]++;            }        }    }    top();    for(int i=4;i<=2*n;i+=2)    {        if(i!=4)printf(" ");        if(col[color[i]]==col[color[2]])        {            printf("%dw",(i-1)/2);        }        else printf("%dh",(i-1)/2);    }    printf("\n");    return 1;}int main(){    while(~scanf("%d%d",&n,&m))    {        cont=0;        memset(head,-1,sizeof(head));        if(n+m==0)break;        for(int i=0;i<m;i++)        {            char c1,c2;            int d1,d2;            scanf("%d%c %d%c",&d1,&c1,&d2,&c2);            int u=d1*2+1;int v=d2*2+1;            if(c1=='w')u+=1;            if(c2=='w')v+=1;            int uu,vv;            if(u%2==1)uu=u+1;else uu=u-1;            if(v%2==1)vv=v+1;else vv=v-1;            add(u,vv);            add(v,uu);        }        add(2,1);        int judge=Slove();        if(judge==0)        {            printf("bad luck\n");        }    }}




0 0
原创粉丝点击