【2-sat】 POJ3648 Wedding

版权声明:本文为博主原创文章,未经博主允许必须转载。 https://blog.csdn.net/C20181220_xiang_m_y/article/details/82559042

Wedding

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

分析:

          条件一开始没看懂。。。原来新郎新娘只有一对,其他是来参加的夫妇。。。

         要求  1:夫妻分在两边坐(包括新的)    2:新娘对面不能有通奸关系的一对(新郎也可能通奸)

         那么设每对夫妻,妻子在新娘这边为状态0,在新郎边为状态1,保证新娘新郎为状态0

          这样定义之后,3w  5h 就可以表示为第三对和第五对不能为 (1,0),每一对要么为1,要么为0,2-sat。

         

拓扑排序的时候注意把缩点之后的对应点存下来(代码中的cf数组)。

#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
#define maxn 100
using namespace std;
int n,m;
int fir[maxn],nxt[maxn*maxn],to[maxn*maxn],tot;
bool flag;
void read(int &a,bool &b)
{
    char c;a=0;
    while(!isdigit(c=getchar()));
    while(isdigit(c)) a=a*10+c-'0',c=getchar();
    b=(c=='w');
}
void line(int x,int y)
{
    nxt[++tot]=fir[x];
    fir[x]=tot;
    to[tot]=y;
}
void add(int u,bool x,int v,bool y)
{
    u=u*2+x,v=v*2+y;
    line(u,v^1);
    line(v,u^1);
}
int dfn[maxn],low[maxn],stk[maxn],tp,scc[maxn],scnt,tim;
void tarjan(int u)
{
    dfn[u]=low[u]=++tim;
    stk[++tp]=u;
    for(int i=fir[u];i;i=nxt[i])
        if(!dfn[to[i]]) tarjan(to[i]),low[u]=min(low[u],low[to[i]]);
        else if(!scc[to[i]]) low[u]=min(low[u],dfn[to[i]]);
    if(dfn[u]==low[u])
    {
        ++scnt;
        do
        {
            if(scc[stk[tp]^1]==scnt) flag=1;
            scc[stk[tp]]=scnt;
        }while(stk[tp--]!=u);
    }
}
int Q[maxn],in[maxn],cf[maxn];
vector<int>e[maxn];
bool mark[maxn];
void Topsort()
{
    int h=1,t=0,u;
    for(int i=1;i<=scnt;i++)
        if(!in[i]) Q[++t]=i;
    while(h<=t)
    {
        u=Q[h++];
        if(!mark[cf[u]]) mark[u]=1;
        for(int i=e[u].size()-1;i>=0;i--)
            if((--in[e[u][i]])==0) Q[++t]=e[u][i];
    }
}
void init()
{
    tim=scnt=tot=flag=tp=0;
    memset(in,0,sizeof in);
    memset(cf,0,sizeof cf);
    memset(fir,0,sizeof fir);
    memset(dfn,0,sizeof dfn);
    memset(scc,0,sizeof scc);
    memset(mark,0,sizeof mark);
    for(int i=1;i<=2*n;i++) e[i].clear();
}
int main()
{
    int x,y;
    bool u,v;
    while(scanf("%d%d",&n,&m),n)
    {
        init();
        line(1,0);
        for(int i=1;i<=m;i++)
        {
            read(x,u);
            read(y,v);
            add(x,u,y,v);
        }
        n*=2;
        for(int i=0;i<n&&!flag;i++)
            if(!dfn[i]) tarjan(i);
        if(flag) {puts("bad luck");continue;}
        for(int u=0;u<n;u++)
            for(int i=fir[u];i;i=nxt[i])
                if(scc[u]!=scc[to[i]]) e[scc[to[i]]].push_back(scc[u]),in[scc[u]]++;
        for(int i=0;i<n;i++)
            cf[scc[i]]=scc[i^1];
        Topsort();
        for(int i=2;i<n;i+=2)
        {
            if(i>2) putchar(' ');
            if(mark[scc[i]]) printf("%dw",i/2);
            else printf("%dh",i/2);
        }
        putchar('\n');
    }
}

猜你喜欢

转载自blog.csdn.net/C20181220_xiang_m_y/article/details/82559042
今日推荐