Description
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.
Input
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.
Output
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
Sample Output
1h 2h 3w 4h 5h 6h 7h 8h 9h
思路:妻子和丈夫各拆成两个点。新娘固定为0。
妻子丈夫不在一边,有不良关系的且在新娘对面的建边,跑一边就好了。
#include<cstdio> #include<iostream> #include<cstring> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) using namespace std; const int mp=2e3+9; const int me=2e3+9; class couple { public:char x[5],y[5]; }f[mp]; class Edge { public:int u,v,next; }; class TWO_SAT { public: int e_to[mp],dfn[mp],stack[mp],edge; int head[mp],dfs_clock,bcc,top; int qhead[mp],od[mp],mark[mp],hh,tt,que[mp],cor[mp]; Edge e[mp*mp*2]; void clear() { clr(head,-1);edge=0;clr(qhead,-1); } void add(int u,int v,int*head) { e[edge].u=u; e[edge].v=v;e[edge].next=head[u];head[u]=edge++; } void add_clause(int x,int xval,int y,int yval)///x or y { x=x+x+xval;y=y+y+yval; add(x^1,y,head);add(y^1,x,head); } // void add_my(int x,int xval,int y,int yval) // { // x=x+x+xval;y=y+y+yval; // add(x,y); // } // void add_con(int x,int xval) // { // x=x+x+xval; // add(x^1,x); // } int tarjan(int u) { int lowu,lowv,v; lowu=dfn[u]=++dfs_clock; stack[top++]=u; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(!dfn[v]) { lowv=tarjan(v); lowu=min(lowu,lowv); } else if(e_to[v]==-1) lowu=min(dfn[v],lowu); } if(lowu==dfn[u]) { ++bcc; do { v=stack[--top]; e_to[v]=bcc; }while(v^u); } return lowu; } bool find_bcc(int n) { top=bcc=dfs_clock=0; clr(dfn,0);clr(e_to,-1);clr(od,0); for(int i=0;i<n;++i) if(!dfn[i])tarjan(i); for(int i=0;i<n;i+=2) { mark[ e_to[i] ]=e_to[i^1]; mark[ e_to[i^1] ]=e_to[i]; if(e_to[i]==e_to[i^1]) return 0; } return 1; } void getID(char*s,int&id,int&man) { id=0; for(int i=0;s[i];++i) if(s[i]>='0'&&s[i]<='9') id=id*10+s[i]-'0'; else if(s[i]=='h')man=1; else if(s[i]=='w')man=0; } void build(int n,int m) {int a,b,c,d; clear(); add_clause(0,0,1,1); ///new wife is 0 for(int i=0;i<n;++i) ///husband can't sit with wife {add_clause(i*2,1,i*2+1,1); add_clause(i*2,0,i*2+1,0); } FOR(i,1,m) { getID(f[i].x,a,b);getID(f[i].y,c,d);///husband is 1 wife 0 //add_clause(a*2+b,1,c*2+d,1); add_clause(a*2+b,0,c*2+d,0); } n+=n; if(!find_bcc(n+n))printf("bad luck\n"); else { n+=n; int cnt=edge,u,v; FOR(i,0,cnt-1) { u=e[i].u;v=e[i].v; if(e_to[u]!=e_to[v]) { add(e_to[v],e_to[u],qhead); ++od[ e_to[u] ]; } } ///topsort hh=0,tt=0; FOR(i,1,bcc) if(od[i]==0) { que[tt++]=i; } clr(cor,-1); while(hh<tt) { u=que[hh++]; if(cor[u]==-1) { cor[u]=1;cor[ mark[u] ]=0; } for(int i=qhead[u];~i;i=e[i].next) { v=e[i].v; if(--od[v]==0) que[tt++]=v; } } ///ans output if(cor[ e_to[4] ]) { printf("1w"); } else if(cor[ e_to[6] ]) printf("1h"); for(int j=8,i;j<n;j+=2) { i=j/2; if(cor[ e_to[j] ])///如果标记 选择前面 { if(i%2==0) printf(" %dw",i/2); else printf(" %dh",i/2); } } printf("\n"); } } }two; int n,m; int main() { //freopen("data.in","r",stdin); while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; FOR(i,1,m) { scanf("%s%s",f[i].x,f[i].y); } two.build(n,m); } return 0; } // //10 6 //3h 7h //5w 3w //7h 6w //8w 3w //7h 3w //2w 5h