Wedding UVA

来源:互联网 发布:新申请的域名注册商 编辑:程序博客网 时间:2024/05/22 06:26

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<<endlusing 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(' ');                }            }        }    }}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 50天婴儿不拉屎怎么办 六个月宝宝吃米粉便秘怎么办 纯母乳喂养的宝宝便秘了怎么办 换奶粉拉绿色是怎么办 2个月宝宝发烧怎么办 小宝绿色稀大便怎么办 新生儿吃奶粉大便干燥怎么办 婴儿拉绿色奶瓣怎么办 宝宝吃奶粉大便干燥怎么办 婴儿吃奶粉不大便怎么办 孩子喝奶粉拉大便干怎么办 吃奶粉拉不出来怎么办 一岁半宝宝睡觉抱着睡放不下怎么办 3月大婴儿拉奶瓣怎么办 70多天宝宝便秘怎么办 宝宝喝奶粉上火便秘怎么办 a2奶粉吃了便秘怎么办 两个月宝宝两天没有大便怎么办 婴儿一直拉肚子吃药也不好怎么办 吃奶粉的婴儿便秘怎么办 一个多月小狗便便不成形怎么办 3个月宝宝不拉屎怎么办 好几天不拉大便怎么办 20个月宝宝大便干燥怎么办 两月宝宝三天没大便怎么办 六个月宝宝大便干燥怎么办 五个月宝宝拉肚子怎么办拉水样 2个月婴儿拉稀水怎么办 两个月的宝宝拉肚子怎么办 两天了不拉大便怎么办 25天的宝宝便秘怎么办 2个月宝宝便秘怎么办 猫拉绿色稀水怎么办 50天的宝宝便秘怎么办 人工喂养大便次数少怎么办 4个月宝宝肠炎怎么办? 三个月的宝宝拉肚子有奶瓣怎么办 吃米粉后不拉屎怎么办 宝宝拉绿色稀水怎么办 3个月宝宝肠胃炎怎么办 两个月婴儿拉水怎么办