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(' '); } } } }}
- Wedding UVA
- UVa 11450 - Wedding shopping
- UVa 11294 - Wedding
- UVA 11294 Wedding
- uva 11294 - Wedding(TwoSAT)
- uva 11450 Wedding Shopping (DP)
- UVa 11450 Wedding shopping (DP)
- UVA 11294 POJ 3648 Wedding
- UVA 11450 Wedding shopping(DP)
- UVA - 11294 Wedding(2-SAT)
- Uva 11294 Wedding(2-SAT)
- UVA 11294 Wedding(2-sat)
- UVA 12582 Wedding of Sultan 解题报告
- UVa 11294 - Wedding(2-SAT)
- UVa 11294 Wedding (two SAT 输出解)
- Wedding shopping - UVa 11450 背包+滚动数组
- UVA 11294 - Wedding(Two-Set)
- uva 11294 - Wedding(2 sat)
- MOB无GUI短信验证DEMO(记录帖)
- 码农小汪-Spring MVC -DispatcherServlet 详解
- 97. Interleaving String
- (译)JVM Concurrent Mark Sweep (CMS) Collector 1.8
- Android Handler的基本使用
- Wedding UVA
- 用python实现简单的遗传算法
- struts2应用及配置01
- Make sure you have the 32 bits Oracle Client installed
- 需求分析图形演示
- Mac 重装系统 U盘制作OS启动盘
- Xamarin中wcf的引用
- STM32学习笔记一一独立看门狗
- 运行时压缩&调试UPX压缩的notepad