POJ3648 Wedding

POJ3648 Wedding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4751   Accepted: 1435   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
*******************************************************************
题目大意:该题很难理解。有一对新婚夫妇做在一张很长的桌子两边。有n-1对夫妻陪伴,这n-1对夫妻也是分坐在桌子两端的。然后,这些人之间有不正当

关系。新婚妻子不希望看到对面有一对人是有不正当关系的。如果能满足安排就输出新婚妻子这边的人,不能满足安排就输出“bad luck”。

解体思路:有点小恶心,首先,对于新婚丈夫也是有不正当关系的。然后,用2-sat就能过。

#include <stdio.h>
#include <string.h>
#include <vector>
#define N 3005
#define M 100005
#define tk(a) ((a)=='h'?1:0)
using namespace std;

int n,m,eid;
int head[N],ed[M],nxt[M];
int id,now,top;
int dfn[N],low[N],ins[N];
int gid[N],stack[N];
int vis[N],mark[N];

void addedge(int s,int e)
{
    ed[eid]=e;
    nxt[eid]=head[s];
    head[s]=eid++;
}

void tarjan(int s)
{
    dfn[s]=low[s]=++now;
    ins[s]=1;
    stack[++top]=s;
    for(int i=head[s];~i;i=nxt[i])
    {
        int t=ed[i];
        if(!dfn[t])
        {
            tarjan(t);
            low[s]=min(low[s],low[t]);
        }
        else if(ins[t])
            low[s]=min(low[s],dfn[t]);
    }
    if(dfn[s]==low[s])
    {
        id++;
        while(top)
        {
            int t=stack[top--];
            ins[t]=0;
            gid[t]=id;
            if(t==s)break;
        }
    }
}

void dfs(int s)
{
    vis[s]=1;
    for(int i=head[s];~i;i=nxt[i])
        if(!vis[ed[i]])
            dfs(ed[i]);
    stack[++top]=s;
}

void re(void)
{
    eid=0;
    memset(head,-1,sizeof(head));
    for(int i=0;i<m;i++)
    {
        int a,b;
        char c,d;
        scanf("%d%c%d%c",&a,&c,&b,&d);
        a=a*2+tk(c);
        b=b*2+tk(d);
        addedge(a*2+1,b*2);
        addedge(b*2+1,a*2);
    }
    for(int i=0;i<n;i++)
    {
        addedge(i*4,i*4+3);
        addedge(i*4+3,i*4);
        addedge(i*4+1,i*4+2);
        addedge(i*4+2,i*4+1);
    }
    addedge(2,3);
}

void run(void)
{
    id=now=top=0;
    memset(dfn,0,sizeof(dfn));
    memset(ins,0,sizeof(ins));
    for(int i=0;i<4*n;i++)
        if(!dfn[i])
            tarjan(i);
    for(int i=0;i<4*n;i+=2)
        if(gid[i]==gid[i+1])
        {
            puts("bad luck");
            return ;
        }
    top=0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<4*n;i++)
        if(!vis[i])
            dfs(i);
    memset(mark,0,sizeof(mark));
    while(top)
    {
        int t=stack[top--];
        if(mark[t])continue;
        mark[t]=2;
        mark[t^1]=1;
    }
    int flag=0;
    for(int i=4;i<4*n;i+=2)
    {
        if(mark[i]==2)continue;
        if(flag)printf(" ");
        flag=1;
        printf("%d",i/4);
        if(i%4>=2)printf("h");
        else printf("w");
    }
    puts("");
}

int main()
{
    //freopen("/home/fatedayt/in","r",stdin);
    while(scanf("%d%d",&n,&m),n+m)
    {
        re();
        run();
    }
    return 0;
}

 


posted on 2011-11-23 21:52  Fatedayt  阅读(648)  评论(0编辑  收藏  举报

导航