zoukankan      html  css  js  c++  java
  • hust 1524 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.

    输入

    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 being0w and 0h. 

    输出

    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".

    样例输入

    10 6
    3h 7h
    5w 3w
    7h 6w
    8w 3w
    7h 3w
    2w 5h
    0 0

    样例输出

    1h 2h 3w 4h 5h 6h 7h 8h 9h

    这是一道2-SAT的题目,每个人可以选或不选,先建立图,再加一条新娘道新郎的边,因为避免他两在一边
    这个题在poj上也有
    #include<map>
    #include<set>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
     
    using namespace std;
     
    const double pi=acos(-1.0);
    const double eps=1e-8;
    typedef pair<int,int>pii;
    const int maxn=1000;
     
    struct TwoSAT
    {
        int n;
        vector<int> G[maxn*2];
        bool mark[maxn*2];
        int S[maxn*2],c;
     
        bool dfs(int x)
        {
            if (mark[x^1]) return false;
            if (mark[x]) return true;
            mark[x]=true;
            S[c++]=x;
            for (int i=0;i<G[x].size();i++)
            if (!dfs(G[x][i])) return false;
            return true;
        }
     
        void init(int n)
        {
            this->n=n;
            for (int i=0;i<=n*2;i++) G[i].clear();
            memset(mark,0,sizeof(mark));
        }
     
        void add_clause(int x,int y)
        {
            G[x].push_back(y);
        }
     
        bool solve()
        {
            for (int i=0;i<n*2;i+=2)
            if (!mark[i] && !mark[i+1])
            {
                c=0;
                if (!dfs(i))
                {
                    while (c>0) mark[S[--c]] = false;
                    if (!dfs(i+1)) return false;
                }
            }
            return true;
        }
    };
     
    TwoSAT twosat;
     
    int main()
    {
        //freopen("in.txt","r",stdin);
        int N,M;
        while(scanf("%d%d",&N,&M)!=EOF && N && M)
        {
             char c1,c2;
             int x,y,x1,x2,y1,y2;
             twosat.init(N);
             while(M--)
             {
                  scanf("%d%c %d%c",&x,&c1,&y,&c2);
                  if (c1=='h') {x1=x*2+1,x2=x*2;}
                  else {x1=x*2;x2=x*2+1;}
                  if (c2=='h') {y1=y*2+1,y2=y*2;}
                  else {y1=y*2;y2=y*2+1;}
                  twosat.add_clause(x1,y2);
                  twosat.add_clause(y1,x2);
             }
             twosat.add_clause(0,1);
             if(!twosat.solve()) printf("bad luck
    ");
             else
             {
                  bool cut=twosat.mark[0];
                  if(twosat.mark[2]==cut)printf("%dw",2/2);
                  else printf("%dh",2/2);
                  for (int i=4;i<N*2;i+=2)
                  if (twosat.mark[i]==cut) printf(" %dw",i/2);
                  else printf(" %dh",i/2);
                  printf("
    ");
             }
        }
        //fclose(stdin);
        return 0;
    }
  • 相关阅读:
    php 工厂模式实例
    nginx多虚拟主机配置
    PHP提高编程效率的方法
    PHP 多态
    锁机制之PHP文件锁
    深入认识javascript中的eval函数(转载)
    PHP&MYSQL 常用的一些性能检测
    寒假作业1:问答题
    软件测试基础知识总结
    七种测试驱动模式
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3799163.html
Copyright © 2011-2022 走看看