POJ-3648 Wedding 2sat

时间:2024-01-09 12:06:08

  题目链接:http://poj.org/problem?id=3648

  题意:一对情人举行婚礼,有n对夫妇参加,别人对着坐在一个长桌子的两边,新娘和新郎坐在最后面,新娘只能看见坐在他对面的人。现在,n对夫妇中有两两通奸了,新娘不希望看到这种情况,先给出通奸的人,求是否存在可行的排位方法。

  有两种建立2sat模型的方法,第一种是对每个人的坐的方向来建立,0和1分别表示坐在左边和右边。但是可以不考虑坐方向,只考虑他们能不能坐在同一边,需要考虑新郎的一边,因为新娘那边得不出限制条件。

 //STATUS:C++_AC_16MS_180KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int first[N*],next[N*N*],vis[N*],S[N*];
int n,m,mt,cnt; struct Edge{
int u,v;
}e[N*N*]; void adde(int a,int b)
{
e[mt].u=a,e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
} int dfs(int u)
{
if(vis[u^])return ;
if(vis[u])return ;
int i;
vis[u]=;
S[cnt++]=u;
for(i=first[u];i!=-;i=next[i]){
if(!dfs(e[i].v))return ;
}
return ;
} int Twosat()
{
int i,j;
for(i=;i<n;i+=){
if(vis[i] || vis[i^])continue;
cnt=;
if(!dfs(i)){
while(cnt)vis[S[--cnt]]=;
if(!dfs(i^))return ;
}
}
return ;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,b,x,y,flag;
char c1,c2;
while(~scanf("%d%d",&n,&m) && (n||m))
{
n<<=;
mem(first,-);
mem(vis,);mt=;
adde(,);
for(i=;i<m;i++){
scanf("%d%c%d%c",&a,&c1,&b,&c2);
x=(a<<)+(c1=='h'?:);
y=(b<<)+(c2=='h'?:);
// printf("%d %d %d %d\n",x,y,x^1,y^1);
adde(x,y^);
adde(y,x^);
} if(Twosat()){
// for(i=0;i<n;i+=2)
// printf("%d %d\n",vis[i],vis[i+1]); printf("%d%c",,vis[]==vis[]?'h':'w');
for(i=;i<n;i+=){
printf(" %d%c",i/,vis[i]==vis[]?'h':'w');
}
}
else printf("bad luck");
putchar('\n');
}
return ;
}