• POJ 3648 Wedding(2SAT)


    Wedding
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5880   Accepted: 1802   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
    

    Source

     
     
    2-SAT。
    【题意】:有一对新人结婚,邀请n对夫妇去参加婚礼。
    有一张很长的桌子,人只能坐在桌子的两边,还要满
    足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇
    之中可能有通奸关系(包括男男,男女,女女),有通
    奸关系的不能同时坐在新娘的对面,可以分开坐,可以
    同时坐在新娘这一侧。如果存在一种可行的方案,输出
    与新娘同侧的人。
     
    求解的时候去选择和新郎同一侧的人,输出的时候换一下就是新娘同一侧的人。
    如果i和j有奸情,则增加一条i到j',j到i'的边,
    同时增加一条新娘到新郎的边,表示必须选新郎。
     
    本题直接选新娘一边的容易错。因为新娘也可能有奸情,需要排除,具体可以见discuss
     
    写了两个代码:一个是输出任意一组解,一个输出字典序最小的解。
    /*
    POJ 3648
    
    【题意】:有一对新人结婚,邀请n对夫妇去参加婚礼。
    有一张很长的桌子,人只能坐在桌子的两边,还要满
    足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇
    之中可能有通奸关系(包括男男,男女,女女),有通
    奸关系的不能同时坐在新娘的对面,可以分开坐,可以
    同时坐在新娘这一侧。如果存在一种可行的方案,输出
    与新娘同侧的人。
    
    
    输出任意一组解,点的编号从0~2n-1
    
    AC
    G++ 16ms  724K
    取和新郎同一侧的,输出的时候反一下就变成和新娘同一侧的了
    
    */
    
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<string.h>
    using namespace std;
    
    const int MAXN=200;//
    char color[MAXN];//染色
    bool visit[MAXN];
    queue<int>q1,q2;
    //vector建图方法很妙
    vector<vector<int> >adj; //原图    //中间一定要加空格把两个'>'隔开
    vector<vector<int> >radj;//逆图
    vector<vector<int> >dag;//缩点后的逆向DAG图
    int n,m,cnt;
    
    int id[MAXN],order[MAXN],ind[MAXN];//强连通分量,访问顺序,入度
    
    void dfs(int u)
    {
        visit[u]=true;
        int i,len=adj[u].size();
        for(i=0;i<len;i++)
          if(!visit[adj[u][i]])
            dfs(adj[u][i]);
        order[cnt++]=u;
    }
    void rdfs(int u)
    {
        visit[u]=true;
        id[u]=cnt;
        int i,len=radj[u].size();
        for(i=0;i<len;i++)
          if(!visit[radj[u][i]])
            rdfs(radj[u][i]);
    }
    void korasaju()
    {
        int i;
        memset(visit,false,sizeof(visit));
        for(cnt=0,i=0;i<2*n;i++)
          if(!visit[i])
            dfs(i);
        memset(id,0,sizeof(id));
        memset(visit,false,sizeof(visit));
        for(cnt=0,i=2*n-1;i>=0;i--)
          if(!visit[order[i]])
          {
              cnt++;//这个一定要放前面来
              rdfs(order[i]);
          }
    }
    bool solvable()
    {
        for(int i=0;i<n;i++)
          if(id[2*i]==id[2*i+1])
            return false;
       return true;
    }
    void topsort()
    {
        int i,j,len,now,p,pid;
        while(!q1.empty())
        {
            now=q1.front();
            q1.pop();
            if(color[now]!=0)continue;
            color[now]='R';
            ind[now]=-1;
            for(i=0;i<2*n;i++)
            {
                if(id[i]==now)
                {
                    //p=(i%2)?i+1:i-1;//点的编号从0开始以后这一定要修改
                    p=i^1;
                    pid=id[p];
                    q2.push(pid);
                    while(!q2.empty())
                    {
                        pid=q2.front();
                        q2.pop();
                        if(color[pid]=='B')continue;
                        color[pid]='B';
                        len=dag[pid].size();
                        for(j=0;j<len;j++)
                            q2.push(dag[pid][j]);
                    }
                }
            }
            len=dag[now].size();
            for(i=0;i<len;i++)
            {
                ind[dag[now][i]]--;
                if(ind[dag[now][i]]==0)
                  q1.push(dag[now][i]);
            }
        }
    }
    
    int main()
    {
        int x,y;
        char c1,c2;
        int i,j;
        int len;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==0&&m==0)break;
            adj.assign(2*n,vector<int>());
            radj.assign(2*n,vector<int>());
            while(m--)
            {
                scanf("%d%c%d%c",&x,&c1,&y,&c2);
                if(c1=='w')x=2*x;
                else x=2*x+1;
                if(c2=='w')y=2*y;
                else y=2*y+1;
                if(x!=(y^1))
                {
                    adj[x].push_back(y^1);
                    adj[y].push_back(x^1);
                    radj[y^1].push_back(x);
                    radj[x^1].push_back(y);
                }
    
            }
            adj[0].push_back(1);
            radj[1].push_back(0);
            //加一条新娘到新郎的边。
            //表示选了新娘必选新郎,这样如果选了新娘就会判断无解。
            //这样选出来的组合必定是有新郎的,即和新郎坐在同一侧的
            korasaju();
            if(!solvable())printf("bad luck\n");
            else
            {
                dag.assign(cnt+1,vector<int>());
                memset(ind,0,sizeof(ind));
                memset(color,0,sizeof(color));
                for(i=0;i<2*n;i++)
                {
                    len=adj[i].size();
                    for(j=0;j<len;j++)
                      if(id[i]!=id[adj[i][j]])
                      {
                          dag[id[adj[i][j]]].push_back(id[i]);
                          ind[id[i]]++;
                      }
                }
                for(i=1;i<=cnt;i++)
                  if(ind[i]==0)
                     q1.push(i);
                topsort();
                for(i=1;i<n;i++)//小心别写错,是color[id[
                {
                    if(i>1)printf(" ");
                    if(color[id[2*i]]=='R')printf("%dh",i);//选取的是和新郎同一侧的,输出和新娘同一侧的
                                                 //所以输出的时候h和w换一下
                    else printf("%dw",i);
                }
                printf("\n");
            }
        }
        return 0;
    }
    /*
    POJ3648
    
    【题意】:有一对新人结婚,邀请n对夫妇去参加婚礼。
    有一张很长的桌子,人只能坐在桌子的两边,还要满
    足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇
    之中可能有通奸关系(包括男男,男女,女女),有通
    奸关系的不能同时坐在新娘的对面,可以分开坐,可以
    同时坐在新娘这一侧。如果存在一种可行的方案,输出
    与新娘同侧的人。
    
    
    
    
    求字典序最小的解,点的编号从0~2n-1
    G++ 0ms
    */
    #include<iostream>
    #include<stdio.h>
    using namespace std;
    const int MAXN=20000;
    const int MAXM=100000;//这个必须开大一点
    struct Node
    {
        int a,b,pre,next;
    }E[MAXM],E2[MAXM];//原图和逆图
    int _n,n,m,V[MAXN],ST[MAXN][2],Q[MAXN],Q2[MAXN],vst[MAXN];
    bool res_ex;
    void init_d()//初始化
    {
        for(int i=0;i<n;i++)//相当于建出双重邻接表的头结点
           E[i].a=E[i].pre=E[i].next=E2[i].a=E2[i].pre=E2[i].next=i;
        m=n;//m是建造双重邻接表时结点的编号
    }
    void add_edge(int a,int b)//加入边(a,b),需要在原图和逆图中添加边
    {//实际上建造出的是循环状的图
        E[m].a=a;E[m].b=b;E[m].pre=E[a].pre;E[m].next=a;E[a].pre=m;E[E[m].pre].next=m;
        E2[m].a=b;E2[m].b=a;E2[m].pre=E2[b].pre;E2[m].next=b;E2[b].pre=m;E2[E2[m].pre].next=m++;
    }
    void solve()
    {
        for(int i=0;i<n;i++)
        {
            V[i]=0;
            vst[i]=0;
        }
        res_ex=1;
        int i,i1,i2,j,k,len,front,rear,front2,rear2;
        bool ff;
        for(int _i=0;_i<_n;_i++)
        {
            if(V[_i<<1]==1||V[(_i<<1)+1]==1)  continue;//找一对未被确定的点
            i=_i<<1;len=0;
            if(!V[i])
            {
                ST[len][0]=i;ST[len++][1]=1;
                if(!V[i ^ 1])
                {
                    ST[len][0]=i^1;
                    ST[len++][1]=2;
                }
                Q[front=rear=0]=i;
                vst[i]=i1=n+i;
                Q2[front2=rear2=0]=i^1;
                vst[i^1]=i2=(n<<1)+i;
                //i1,i2为标志量,这样设置标志量使得每次都不一样,省去了初始化
                ff=1;
                for(;front<=rear;front++)
                {
                    j=Q[front];
                    for(int p=E[j].next;p!=j;p=E[p].next)
                    {
                        k=E[p].b;
                        if(V[k]==2||vst[k]==i2||V[k^1]==1||vst[k^1]==i1)
                        {ff=0;break;}
                        if(vst[k]!=i1)
                        {
                            Q[++rear]=k;vst[k]=i1;
                            if(!V[k])
                            {
                                ST[len][0]=k;
                                ST[len++][1]=1;
                            }
                        }
                        if(vst[k^1]!=i2)
                        {
                            Q2[++rear2]=k^1;vst[k^1]=i2;
                            if(!V[k])
                            {
                                ST[len][0]=k^1;
                                ST[len++][1]=2;
                            }
                        }
                    }
                    if(!ff) break;
                }
                if(ff)
                {
                    for(;front2<=rear2;front2++)
                    {
                        j=Q2[front2];
                        for(int p=E2[j].next;p!=j;p=E2[p].next)
                        {
                            k=E2[p].b;
                            if(V[k]==1||vst[k]==i1)
                            {
                                ff=0;
                                break;
                            }
                            if(vst[k]!=i2)
                            {
                                vst[k]=i2;Q2[++rear]=k;
                                if(!V[k])
                                {
                                    ST[len][0]=k;
                                    ST[len++][1]=2;
                                }
                            }
                        }
                        if(!ff) break;
                    }
                    if(ff)
                    {
                        for(int j=0;j<len;j++) V[ST[j][0]]=ST[j][1];
                        continue;
                    }
                }
            }
            i=(_i<<1)+1;len=0;
            if(!V[i])
            {
                ST[len][0]=i;ST[len++][1]=1;
                if(!V[i ^ 1])
                {
                    ST[len][0]=i^1;
                    ST[len++][1]=2;
                }
                Q[front=rear=0]=i;
                vst[i]=i1=n+i;
                Q2[front2=rear2=0]=i^1;
                vst[i^1]=i2=(n<<1)+i;
                ff=1;
                for(;front<=rear;front++)
                {
                    j=Q[front];
                    for(int p=E[j].next;p!=j;p=E[p].next)
                    {
                        k=E[p].b;
                        if(V[k]==2||vst[k]==i2||V[k^1]==1||vst[k^1]==i1)
                        {ff=0;break;}
                        if(vst[k]!=i1)
                        {
                            Q[++rear]=k;vst[k]=i1;
                            if(!V[k])
                            {
                                ST[len][0]=k;
                                ST[len++][1]=1;
                            }
                        }
                        if(vst[k^1]!=i2)
                        {
                            Q2[++rear2]=k^1;vst[k^1]=i2;
                            if(!V[k])
                            {
                                ST[len][0]=k^1;
                                ST[len++][1]=2;
                            }
                        }
                    }
                    if(!ff) break;
                }
                if(ff)
                {
                    for(;front2<=rear2;front2++)
                    {
                        j=Q2[front2];
                        for(int p=E2[j].next;p!=j;p=E2[p].next)
                        {
                            k=E2[p].b;
                            if(V[k]==1||vst[k]==i1)
                            {
                                ff=0;
                                break;
                            }
                            if(vst[k]!=i2)
                            {
                                vst[k]=i2;Q2[++rear]=k;
                                if(!V[k])
                                {
                                    ST[len][0]=k;
                                    ST[len++][1]=2;
                                }
                            }
                        }
                        if(!ff) break;
                    }
                    if(ff)
                    {
                        for(int j=0;j<len;j++) V[ST[j][0]]=ST[j][1];
                        continue;
                    }
                }
            }
            if(V[_i<<1]+V[(_i<<1)+1]!=3){res_ex=0;break;}
        }
    }
    int main()
    {
        int _m,a,b;
        char ch1,ch2;
        while(scanf("%d%d",&_n,&_m)!=EOF)//_n是点的对数,_m是冲突的点个数
        {
            if(_n==0&&_m==0)break;
            n=_n<<1;
            init_d();
            for(int i=0;i<_m;i++)
            {
                scanf("%d%c%d%c",&a,&ch1,&b,&ch2);
                if(ch1=='w')  a=2*a;
                else a=2*a+1;
                if(ch2=='w')  b=2*b;
                else  b=2*b+1;
                if(a!=(b^1))
                {
                    add_edge(a,b^1);//选a,必选b^1,
                    add_edge(b,a^1);//选b,必选a^1,
                }
            }
            add_edge(0,1);//加一条新娘到新郎的边。
            //表示选了新娘必选新郎,这样如果选了新娘就会判断无解。
            //这样选出来的组合必定是有新郎的,即和新郎坐在同一侧的
            solve();
            bool first=false;
            if(res_ex)
            {
                for(int i=0;i<n;i++)
                {
                  if(V[i]==1&&i!=1)
                  {
                      if(first)printf(" ");
                      else first=true;
                      printf("%d%c",i/2,i%2==0?'h':'w');//选取的是和新郎同一侧的,输出和新娘同一侧的
                                                 //所以输出的时候h和w换一下
                  }
                }
                printf("\n");
            }
            else printf("bad luck\n");
        }
        return 0;
    }
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    docker pull 报错解决办法
    cobbler自动装机!
    centos7 日志定期清理
    【转载】python 模块
    0603 python 基础02
    0527 python 基础01
    0520 python
    160519 如何做自动化测试?
    SourceTree&Git部分名词解释
    sourceTree安装与使用
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2713589.html
Copyright © 2020-2023  润新知