POJ 3648 2-SAT 输出方案

                                                                                                      Wedding













Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7264Accepted: 2215Special
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
解题思路:根据矛盾关系建图,tarjin缩点判断,假设不符合直接退出,否则进行根据缩点建立反图,拓扑排序染色,然后输出答案。
代码:
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<queue>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<math.h>
using namespace std;
const int maxn=5000;
int head[maxn],head1[maxn],tol,tot,Stack[maxn],top,indexx,scc,instack[maxn],belong[maxn],cf[maxn],col[maxn],in[maxn],n,low[maxn],dfn[maxn];
struct node
{
        int next,to;
}edge[maxn*100],edge1[100*maxn];
void add(int u,int v)
{
        edge[tol].to=v;
        edge[tol].next=head[u];
        head[u]=tol++;
}
void add1(int u,int v)
{
        edge1[tot].to=v;
        edge1[tot].next=head1[u];
        head1[u]=tot++;
}
void tarjin(int u)
{
        low[u]=dfn[u]=++indexx;
        Stack[top++]=u;instack[u]=1;
        int i,v;
        for(i=head[u];i!=-1;i=edge[i].next)
        {
                v=edge[i].to;
                if(!dfn[v])
                {
                        tarjin(v);
                        if(low[u]>low[v])low[u]=low[v];
                }
                else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
        }
        if(low[u]==dfn[u])
        {
                scc++;
                do
                {
                        v=Stack[--top];
                        instack[v]=0;
                        belong[v]=scc;
                }while(u!=v);
        }
}
void get_map(int m)
{
    tol=0;
    memset(head,-1,sizeof(head));
    int x1,x2;
    char c1,c2;
    add(0,1);
    while(m--)
    {
        scanf("%d%c %d%c",&x1,&c1,&x2,&c2);
        x1<<=1;
        x2<<=1;
        if (c1=='h') x1++;
        if (c2=='h') x2++;
        if (x1==(x2^1)) continue;
        add(x1,x2^1);
        add(x2,x1^1);
    }
}
void solve()
{
      int i,j,u,v;
      memset(dfn,0,sizeof(dfn));
      memset(belong,0,sizeof(belong));
      memset(instack,0,sizeof(instack));
      indexx=scc=top=0;
      for(i=0;i<2*n;i++)if(!dfn[i])tarjin(i);
      int flag=1;
      memset(cf,0,sizeof(cf));
      memset(head1,-1,sizeof(head));tot=0;
      memset(in,0,sizeof(in));
      memset(col,0,sizeof(col));
      for(i=0;i<n;i++)
      {
              if(belong[2*i]==belong[2*i+1])
              {
                      flag=0;break;
              }
              cf[belong[2*i]]=belong[2*i+1];
              cf[belong[2*i+1]]=belong[2*i];
      }
      if(flag==0)
      {
              puts("bad luck");return;
      }
      for(u=0;u<2*n;u++)
      {
              for(i=head[u];i!=-1;i=edge[i].next)
              {
                      v=edge[i].to;
                     if(belong[u]!=belong[v])
                     {
                             in[belong[u]]++;
                             add1(belong[v],belong[u]);
                     }
              }
      }
      queue<int> que;
      for(i=1;i<=scc;i++)if(in[i]==0)que.push(i);
      while(!que.empty())
      {
              int cur=que.front();que.pop();
              if(col[cur]==0)
              {
                      col[cur]=1;
                      col[cf[cur]]=-1;
              }
              for(i=head1[cur];i!=-1;i=edge1[i].next)
              {
                      v=edge1[i].to;
                      if(--in[v]==0)que.push(v);
              }
      }
    flag=false;
    for (int i=3; i<2*n; i+=2)
    {
        if (!flag) flag=true;
        else printf(" ");
        if(col[belong[i]]==1)printf("%dw",i/2);
        else printf("%dh",i/2);
    }
    printf("\n");
}
int main()
{
        int i,j,k,m,p,q,g,h;
        while(~scanf("%d%d",&n,&m))
        {
                if(n==0&&m==0)break;
                get_map(m);
                solve();
        }
        return 0;
}
posted @ 2013-09-04 15:26  线性无关  阅读(140)  评论(0)    收藏  举报