【2-sat】 POJ3648 Wedding

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');
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值