http://poj.org/problem?id=3648
Title Description:
There was a couple of wedding ceremonies, and n couples were invited to their wedding, and there was a table where people could only sit on either side of the table. People with M pairs of adultery (male, female, female), require:
1. Couples cannot sit on the same side
2. Two persons in adultery cannot sit opposite the bride.
If there is a set of solutions, the output of the person sitting on the bride's side
Analysis:
Each of the two states, sitting on one side of the table or on the other side, has the M-group contradiction. A set of solutions can be found on the premise that the groom must choose, then the group is the same side with the groom, and the other is the same side of the bride if a set of solutions are found on the premise that the bride must choose.
Using 2-sat to find a set of solutions, divided into two parts with a value of 1 and a value of 0
//224k 0MS C + +#include <cstring>#include <cstdio>#include <queue>#include <vector>#include <iostream>Const intmaxn= .;Const intmaxm=1000010;using namespace STD;intLow[maxn],dfn[maxn],sccno[maxn],scc,dfs_clock,top;intSTACK[MAXN];BOOLINSTACK[MAXN];structEdge {intTo,next;} edge[maxm<<1];intHead[maxn],tot;voidAddedge (intUintV) {edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;}voidInit () {tot=0;memset(Head,0xFF,sizeof(head));}voidDfsintu) {intV Low[u]=dfn[u]=++dfs_clock; instack[u]=1; Stack[top++]=u; for(intI=head[u]; i!=-1; I=edge[i].next) {v=edge[i].to;if(!dfn[v]) {DFS (v);if(Low[u]>low[v]) low[u]=low[v]; }Else if(Instack[v]&&low[u]>dfn[v]) low[u]=dfn[v]; }if(Low[u]==dfn[u]) {scc++; for(;;) {V=stack[--top]; instack[v]=0; SCCNO[V]=SCC;if(V==u) Break; } }}BOOLSolveable (intN) {memset(DFN,0,sizeof(DFN));memset(Instack,false,sizeof(Instack)); Dfs_clock=top=scc=0; for(intI=0; i<n; ++i) {if(!dfn[i]) DFS (i); } for(intI=0; i<n; i+=2) {if(sccno[i]==sccno[i^1])return false; }return true;} Queue<int>Q1; vector<vector<int> >DagCharCOLOR[MAXN];intINDEG[MAXN];intCF[MAXN];voidSolveintN) {dag.assign (scc+1, vector<int>());memset(Indeg,0,sizeof(indeg));memset(Color,0,sizeof(color)); for(intu=0; u<n;++u) { for(inti=head[u];i!=-1; i=edge[i].next) {intv=edge[i].to;if(Sccno[u]!=sccno[v]) {Dag[sccno[v]].push_back (sccno[u]); indeg[sccno[u]]++; } } } for(intI=0; i<n;i+=2) {cf[sccno[i]]=sccno[i^1]; cf[sccno[i^1]]=sccno[i]; } while(!q1.empty ()) Q1.pop (); for(intI=1; i<=scc;++i) {if(indeg[i]==0) Q1.push (i); } while(!q1.empty ()) {intU=q1.front (); Q1.pop ();if(color[u]==0) {color[u]=' R '; color[cf[u]]=' B '; }intSz=dag[u].size (); for(intI=0; i<sz;++i) {indeg[dag[u][i]]--;if(indeg[dag[u][i]]==0) Q1.push (Dag[u][i]); } }}intMain () {#ifndef Online_judgeFreopen ("In.txt","R", stdin);#endif //Online_judge intN,m,u,v;CharS1,S2; while(scanf("%d%d", &n,&m) = =2) {if(n==0&&m==0) Break; Init (); while(m--) {scanf("%d%c%d%c", &U,&S1,&V,&S2); U= (s1==' h ') ? (2*u+1):(2*U); v= (s2==' h ') ? (2*v+1):(2*V);//cout<< "u=" <<u<< "v=" <<v<<endl;//cout<< "s1=" <<s1<< "s2=" <<s2<<endl;Addedge (u,v^1); Addedge (v,u^1); } Addedge (0,1);if(!solveable (2*n))puts("Bad luck");Else{Solve (2*N); for(intI=1; i<n;++i) {if(color[sccno[2*i]]==' R ')printf("%DH", i);Else printf("%DW", i);if(i<n-1)printf(" ");Else printf("\ n"); } } }return 0;}
POJ3648. Wedding--2-sat output Arbitrary solution