Wedding UVA - 11294

Wedding UVA - 11294

图论·2-SAT

题目大意:

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

题解:

看起来很简单的2-SAT问题。
xi表示一个人的状态,0表示在新娘的对面,1表示在新娘的一边。
夫妻i j:
xi OR xj
!xi OR !xj
讨厌i j:
xi OR xj
建边,跑2-SAT即可。

看起来很水,然而蒙蒙调了一节课。。。
1.数组开小,应该是4倍的点。
2.夫妻从0开始编号,且编号为0的是新郎新娘。(蒙蒙的编号一片混乱)
3.注意!我们强制要求新郎在左边,新娘在右边!特判一下!
(因为我们连的边是 讨厌i j:xi OR xj)
4.乱纠结怎么输出。。。

感觉自己弱极了ORZ

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define D(x) cout<<#x<<" = "<<x<<"  "
#define E cout<<endl
using namespace std;

const int N = 205;
const int M = 2005;

inline int ID(int x,int y){ return x*2+y; }
inline int H(int x){ return x*2; }
inline int W(int x){ return x*2+1; }

int n,m; 

struct Edge{
    int to,next;
}e[M*2];
int head[N],ec;
void clear(){ memset(head,0,sizeof(head)); ec=0; }
void add(int a,int b){
    //D(a); D(b); E;
    ec++; e[ec].to=b; e[ec].next=head[a]; head[a]=ec;
} 
void add2(int a,int i,int b,int j){
    //D(a); D(i); D(b); D(j); E;
    add(ID(a,!i),ID(b,j));
    add(ID(b,!j),ID(a,i));
}

bool mark[N]; int S[N]; int c;
bool dfs(int x){
    //D(x); E;
    if(mark[x^1]) return false;    //x^1 ±ØÐë´Ó0¿ªÊ¼±àºÅ 
    if(mark[x]) return true;
    mark[x]=true; 
    S[c++]=x;
    for(int i=head[x];i;i=e[i].next){
        if(!dfs(e[i].to)) return false;
    }
    return true;
}

bool solve(){
    memset(mark,false,sizeof(mark));
    for(int i=0;i<n*4;i+=2){
        //D(i); E;
        if(!mark[i] && !mark[i+1]){
            c=0;
            if(!dfs(i)){
                if(i==0) return false;
                while(c) mark[S[--c]]=false;
                if(!dfs(i+1)) return false;
            }
        }
    }
    return true;
}

void print(int x){
    if((x/2)&1) printf("%dh",x/4);
    else printf("%dw",x/4);
}

int main(){
    freopen("a.in","r",stdin);
    while(~scanf("%d%d",&n,&m) && (n||m)){
        clear();
        for(int i=0;i<n;i++){
            add2(H(i),0,W(i),0);
            add2(H(i),1,W(i),1);
        }
        for(int i=1;i<=m;i++){
            char a[5],b[5];
            scanf("%s%s",a,b);
            int j=0,k=0;
            while(a[j]<='9'&&a[j]>='0')j++;
            while(b[k]<='9'&&b[k]>='0')k++;
            int x=0,y=0;
            for(int d=0;d<j;d++)
                x=x*10+a[d]-'0';
            for(int d=0;d<k;d++)
                y=y*10+b[d]-'0';
            //D(x); D(y); E;
            x=a[j]=='h'?H(x):W(x);
            y=b[k]=='h'?H(y):W(y);
            //D(x); D(y); E;
            add2(x,1,y,1); 
        }
        if(!solve()){
            puts("bad luck");
        }
        else{
//          for(int i=0;i<n*4;i+=2){
//              printf("Pople %d: ",i/2+1);
//              if(mark[i]) puts("N");
//              if(mark[i+1]) puts("Y");
//          }
            for(int i=4;i<n*4;i+=2){
                if(mark[i]){
                    print(i);
                    if(i/4==n-1) putchar('\n');
                    else putchar(' ');
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值