uva 11294 - Wedding(TwoSAT)

Problem E: 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 being 0w 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".

Sample Input

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

Possible Output for Sample Input

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

Prabhakar Ragde

题目翻译略坑爹,导致开始题意理解错了,把所有的夫妻都看成新娘新郎了。。。

然后又犯个傻b错误,找了一下午错误,后来找了题目的数据,才ac,解题也不想好好写了,就这样吧。

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 100;

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));
  }

  // x = xval or y = yval
  void add_clause(int x, int xval, int y, int yval) {
    x = x * 2 + xval;
    y = y * 2 + yval;
    G[x^1].push_back(y);
    G[y^1].push_back(x);
  }

  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 solver;

int main(){
    int n,m;
    char s1[10],s2[10];
    int a1,a2;
    while(scanf("%d%d",&n,&m)){
        if(n == 0 && m == 0) break;
        solver.init(2*n);
        solver.mark[1] = true;
        for(int i = 0;i < n;i++){
            solver.add_clause(2*i,1,2*i+1,1);
            solver.add_clause(2*i,0,2*i+1,0);
        }
        while(m--){
            scanf("%d%s%d%s",&a1,s1,&a2,s2);
            int xval,yval,x,y;
            xval = s1[0]=='h'?1:0;
            yval = s2[0]=='h'?1:0;
            x = a1*2+xval; y = a2*2+yval;
            solver.add_clause(x,1,y,1);
        }
        if(!solver.solve()) printf("bad luck\n");
        else{
            for(int i = 2;i < 2*(n-1);i+=2){
                printf("%d",i/2);
                if(solver.mark[2*i+1]) printf("w ");
                else printf("h ");
            }
            printf("%d",n-1);
            if(solver.mark[2*2*(n-1)+1]) printf("w\n");
            else printf("h\n");
        }
    }
    return 0;
}


一些数据

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

10 10
 6h 2w
1h 9w
1w 3w
9w 0h
1h 9h
 4h 1w
7h 2w
1h 0h
0h 9w
0h 3h

10 10
 0h 1w
1w 4h
7w 6w
9h 5h
4w 2h
7h 4w
4w 1w
4h 9h
4h 5w
5w 1h

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

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

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

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

10 10
 7w 4h
 2w 7w
5h 1h
6w 9h
2h 4h
4w 5w
0w 4w
9w 2w
4w 9h
 0w 7w

30 30
 5h 18w
5w 19h
27w 26h
16h 15h
21w 16h
 28h 27h
19w 28w
8w 29w
 27w 19w
16h 1h
6w 18h
1w 3h
 14w 3h
9h 14w
11h 2w
21w 17h
 18h 13w
 6h 11h
26w 27w
16h 12h
 11w 4h
 29h 3w
6h 14w
 29w 15h
11h 10w
 9w 27w
21w 9h
 16w 7w
 7h 18w
8w 2h

30 30
18w 26h
6w 16h
21h 25h
10h 9w
 15h 26w
7w 9h
5w 19h
14w 22w
14h 13w
13w 1h
9h 26w
3w 13h
7w 12h
 26w 20w
2w 29w
14w 26h
23w 11h
22h 2w
23h 5h
 17h 6h
 11h 22w
 23h 22h
 13h 27w
5w 18w
24w 27h
26h 1w
 20h 7h
 16w 18w
5w 27w
17h 0h

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值