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\n");
         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("\n");
         }
    }
    //fclose(stdin);
    return 0;
}

 

转载于:https://www.cnblogs.com/chensunrise/p/3799163.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值