poj3648 wedding 2-SAT


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
#include<vector>
#define N 8200
int e[N],v[N],ne[N];
int nn;
void add(int x,int y){
ne[++nn]=e[x],e[x]=nn,v[nn]=y;
}
int e2[N],nn2,v2[N],ne2[N];
void add2(int x,int y){
ne2[++nn2]=e2[x],e2[x]=nn2,v2[nn2]=y;
}
int stn,st[N];
int been[N];
int low[N],dfn[N];
int in[N];
int ru[N],tot,bn;
vector<int> jj[N];
int col[N];
void cl(){
memset(col,0,sizeof(col));
memset(been,0,sizeof(been));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(e,0,sizeof(e));
memset(e2,0,sizeof(e2));
memset(ru,0,sizeof(ru));
nn=0,nn2=0;
bn=0;
tot=0;
}
int qu[N],he,bo;
void dfs(int x){
low[x]=dfn[x]=++tot;
st[++stn]=x;
been[x]=1;
for(int i=e[x];i;i=ne[i]){
if(!dfn[v[i]]){
dfs(v[i]);
low[x]=min(low[x],low[v[i]]);
}else if(been[v[i]])low[x]=min(low[x],dfn[v[i]]);
}
if(dfn[x]==low[x]){
bn++;
do{
in[st[stn]]=bn;
been[st[stn]]=0;
}while(st[stn--]!=x);
}

}
int main(){
while(scanf("%d%d",&n,&m),n){
cl();
for(int i=1;i<=m;i++){
int a,b;
char c,d;
scanf("%d",&a);
scanf("%c",&c);
scanf("%d",&b);
scanf("%c",&d);
a++,b++;
if(c==‘h‘){
if(d==‘h‘)add(a,b+n),add(b,a+n);
else add(a,b),add(b+n,a+n);
}
if(c==‘w‘){
if(d==‘h‘)add(a+n,b+n),add(b,a);
else add(a+n,b),add(b+n,a);
}
}
add(1+n,1);
for(int i=1;i<=n*2;i++)if(!dfn[i])dfs(i);

for(int i=1;i<=n;i++)if(in[i]==in[i+n]){
printf("bad luck\n");
goto loop;
}
for(int i=1;i<=n*2;i++){
for(int j=e[i];j;j=ne[j])if(in[i]!=in[v[j]]){
add2(in[v[j]],in[i]);
ru[in[i]]++;
}
}
for(int i=1;i<=bn;i++)jj[i].clear();
for(int i=1;i<=n;i++){
jj[in[i]].push_back(in[i+n]);
jj[in[i+n]].push_back(in[i]);
}
bo=1;
he=0;
for(int i=1;i<=bn;i++){
if(!ru[i])qu[++he]=i;
}
while(he>=bo){
int x=qu[bo++];

if(col[x])continue;
for(int j=0;j<jj[x].size();j++){
col[jj[x][j]]=1;
}
for(int i=e2[x];i;i=ne2[i]){
ru[v2[i]]--;
if(!ru[v2[i]])qu[++he]=v2[i];
}

}
for(int i=2;i<=n;i++)if(col[in[i]]){
printf("%dh ",i-1);
}else{
printf("%dw ",i-1);
}
printf("\n");
loop:;
}
}

poj3648 wedding 2-SAT

时间: 2024-12-21 13:59:32

poj3648 wedding 2-SAT的相关文章

POJ3648 Wedding

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10128   Accepted: 3094   Special Judge 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

POJ3648 Wedding 【2-sat】

题目 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

poj3648:Wedding——题解(配2-SAT简易讲解)

http://poj.org/problem?id=3648 (在家,而且因为2-SAT写的不明不白的,所以这篇详细写) 题目大意: 有一对新人结婚,邀请了n-1 对夫妇去参加婚礼.婚礼上所有人要坐在一张很长的桌子的两边.所有的夫妇不能坐在同一边.还有m 对人,这对人不能同时坐在新郎一边,但可以同时坐在新娘这边或是分两边坐. 以这道题为2-SAT讲解模板题. (请先了解2-SAT是干什么的再往下看) 首先判断:打眼一看一定是2-SAT. 然后建图,a到b表示如果选了a就一定选b的意思. 那么对于

poj 3648 Wedding 2-SAT问题入门题目

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

LA 3211 飞机调度(2—SAT)

https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间为E,晚着陆时间为L,不得在其他时间着陆.你的任务是为这些飞机安排着陆方式,使得整个着陆计划尽量安全.换句话说,如果把所有飞机的实际着陆时间按照从早到晚的顺序排列,相邻两个着陆时间间隔的最小值. 思路: 二分查找最大值P,每次都用2—SAT判断是否可行. 1 #include<iostream>

poj 3648 Wedding

每对夫妻恰有一人坐在新娘对面,两个关系不正常的人不能都在新娘对面 问,是否有解 #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include&

WEDDING SHOP OPENCART 自适应主题模板 ABC-0663

WEDDING SHOP OPENCART 自适应主题模板 ABC-0663 Features :? Responsive theme,? Compatible with the most common browsers: FireFox 1.5-3.5.6+, IE9+, Safari 3.2 +, Opera, Google Chrome.? Compatible with Open Cart Version v2.x,? Made with HTML 5 and CSS 3? Design

UVA 11294 - Wedding(Two-Set)

UVA 11294 - Wedding 题目链接 题意:有n对夫妻,0号是公主.如今有一些通奸关系(男男,女女也是可能的)然后要求人分配在两側.夫妻不能坐同一側.而且公主对面一側不能有两个同奸的人,问方案 思路:2-set,建图.一共2n个人,设偶数是丈夫,奇数是妻子.左側为false,右側为true,然后丈夫妻子建一条true false 或 false true的边,然后然公主在左側.那么同奸的一对至少一个为false,建一条边,然后2-set判定就可以 代码: #include <cstd

UVA 11450 Wedding shopping(DP)

One of our best friends is getting married and we all are nervous because he is the first of us who is doing something similar. In fact, we have never assisted to a wedding, so we have no clothes or accessories, and to solve the problem we are going