1. 程式人生 > >3648 Wedding

3648 Wedding

Wedding Time Limit: 1000MS Memory Limit: 65536K 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

題意:好可怕的題意。。 思路:2-SAT 模型,夫妻兩個不能同時坐在新娘對面,然後建邊的時候注意新娘新郎可能也得建邊,所以應該把新娘同所有人建邊,這樣才能保證跑出來的是正確的. 模型一:兩者(A,B)不能同時取   那麼選擇了A就只能選擇B’,選擇了B就只能選擇A’   連邊A→B’,B→A’ 程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 200000
#define MAXM 200000
#define mem(a,b) memset(a,b,sizeof(a))

int n,m;
struct node
{
    int v;
    node *next;
};

node edge[MAXM*2];
node *cnt=&edge[0];
node *adj[MAXN];
node edge2[MAXM*2];
node *cnt2=&edge2[0];
node *adj2[MAXN];
int dfn[MAXN],low[MAXN],dcnt;
int stack[MAXN],top;
int Belong[MAXN],Num[MAXN],opp[MAXN],scc;
int In[MAXN],q[MAXN],col[MAXN];
bool Instack[MAXN],ans[MAXN];

inline void Get_int(int &Ret)
{
    char ch;
    bool flag=false;
    for(;ch=getchar(),ch<'0'||ch>'9';)
        if(ch=='-')
            flag=true;
    for(Ret=ch-'0';ch=getchar(),ch>='0'&&ch<='9';Ret=Ret*10+ch-'0');
    flag&&(Ret=-Ret);
}

inline int Get(int x)
{
    if(x%2)
        return x+1;
    return x-1;
}


inline void Addedge(int u,int v)
{
    node *p=++cnt;
    p->v=v;
    p->next=adj[u];
    adj[u]=p;
}

inline void Addedge2(int u,int v)
{
    node *p=++cnt2;
    p->v=v;
    p->next=adj2[u];
    adj2[u]=p;
}

void Read()
{
	n+= n; 
    int i,j,k;
    for(int i = 0;i<= n+n;i++)
    {
    	dfn[i]=low[i]= stack[i]=Belong[i] = Num[i] =opp[i] = In[i]= q[i]= col[i]=Instack[i] = ans[i] = 0;
    	adj[i] = adj2[i] = NULL;
    }
    cnt=&edge[0];
    cnt2=&edge2[0];
    for(i=1;i<=m;i++)
    {
    	int x,y;
    	char c,d;
        scanf("%d%c %d%c",&x,&c,&y,&d);
        x++;
        y++;
        x*= 2;
        y*= 2;
        if(c == 'h') x--;// husband
        if(d == 'h') y--;
     //   cout<<x<<' '<<y<<endl;
        Addedge(x,Get(y));
        Addedge(y,Get(x));
    }
    for(int i = 3;i<= n;i++)
	{
		Addedge(2,Get(i));
        Addedge(i,Get(2));
	}
    return ;
}

void Tarjan(int u)
{
    int v;
    dfn[u]=low[u]=++dcnt;
    stack[++top]=u;
    Instack[u]=true;
    for(node *p=adj[u];p;p=p->next)
    {
        v=p->v;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(Instack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scc++;
        do
        {
            v=stack[top];
            top--;
            Instack[v]=false;
            Belong[v]=scc;
            Num[scc]++;
        }while(v!=u);
    }
}

bool Work()
{
    int i;
    for(i=1;i<=n;i++)
        if(!dfn[i])
            Tarjan(i);
    for(i=1;i<=n;i+=2)
    {
        if(Belong[i]==Belong[i+1])
            return false;
        opp[Belong[i]]=Belong[i+1];
        opp[Belong[i+1]]=Belong[i];
    }
    int u,v;
    for(i=1;i<=n;i++)
        for(node *p=adj[i];p;p=p->next)
        {
            v=p->v;
            if(Belong[i]!=Belong[v])
            {
                Addedge2(Belong[v],Belong[i]);
                In[Belong[i]]++;
            }
        }
    int l=0,r=0;
    for(i=1;i<=scc;i++)
        if(!In[i])
        {
            q[r]=i;
            r++;
        }
    while(l<r)
    {
        u=q[l];
        l++;
        if(!col[u])
        {
            col[u]=1;
            col[opp[u]]=-1;
        }
        for(node *p=adj2[u];p;p=p->next)
        {
            v=p->v;
            In[v]--;
            if(!In[v])
            {
                q[r]=v;
                r++;
            }
        }
    }
    for(i=1;i<=n;i+=2)
        if(col[Belong[i]]==1)
            ans[i]=true;
    return true;
}


void Print()
{
    if(Work())
    {
        int i;
        for(i=3;i< n-1;i+= 2)
        {
        	printf("%d",(i+1)/2-1);
        	if(ans[i]) printf("w ");
        	else printf("h ");
        }
        printf("%d",n/2-1);
        if(ans[n-1]) printf("w\n");
        else printf("h\n");
    }
    else
        printf("bad luck\n");
}

int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		if(n == 0&&m == 0) break;
		dcnt = scc = top = 0;
		
		Read();
    	Print();
	}
	
    return 0;
}