poj 3648 Wedding
来源:互联网 发布:剑侠情缘三捏脸数据 编辑:程序博客网 时间:2024/05/29 16:57
新郎和新娘分别做到桌子的两边,夫妻不能同时坐到一边,不正当关系的两个人也不能同时坐到新娘对面,输出坐到新娘对面的一组可行方案。
一对夫妻选择一个人坐到对面,很明显2-sat结构。第i对夫妻用2*i和2*i+1表示妻子和丈夫,对于每个不正当关系,把不正当关系中的a-b',b-a‘连边。然后逆序建图,拓扑排序染色,输出结果。
#include <iostream>#include <cstdio>#include <vector>#include <cmath>#include <stack>#include <map>#include <set>#include <queue>#include <cstring>#include <algorithm>using namespace std;#define L(t) t<<1#define R(t) t<<1|1#define eps 1e-7typedef long long LL;const int maxn=5050;const int maxm=100005;const int MOD=1000000007;int head[maxn],next[maxm],to[maxm],tot;int dfn[maxn],low[maxn],stk[maxn],scc[maxn],cnt,top,id,f[maxn];bool vis[maxn];int color[maxn],ind[maxn];vector<int> dag[maxn];void addEdge(int u,int v){ next[tot]=head[u];to[tot]=v;head[u]=tot++;}void init(){ tot=id=top=cnt=0; memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(vis,0,sizeof(vis)); memset(ind,0,sizeof(ind)); memset(color,0,sizeof(color));}void tarjan(int v){ dfn[v]=low[v]=++cnt; vis[v]=1; stk[++top]=v; int u; for(int i=head[v];i!=-1;i=next[i]) { u=to[i]; if(!dfn[u]) { tarjan(u); low[v]=min(low[v],low[u]); } else if(vis[u]) { low[v]=min(low[v],dfn[u]); } } if(dfn[v]==low[v]) { id++; while(1) { u=stk[top--]; scc[u]=id; vis[u]=0; if(u==v) break; } }}void BuildDag(int n){ for(int u=0;u<2*n;u++) { for(int i=head[u];i!=-1;i=next[i]) { int v=to[i]; if(scc[u]!=scc[v]) { dag[scc[v]].push_back(scc[u]); ind[scc[u]]++; } } }}void topsort(){ queue<int>q; for(int i=1;i<=id;i++) { if(ind[i]==0) q.push(i); } while(!q.empty()) { int u=q.front(); q.pop(); if(!color[u]) color[u]=1,color[f[u]]=2; int d=dag[u].size(); for(int i=0;i<d;i++) { int v=dag[u][i]; ind[v]--; if(!ind[v]) q.push(v); } }}void solve(int n){ for(int i=0;i<2*n;i++) if(!dfn[i])tarjan(i); for(int i=0;i<n;i++) { if(scc[2*i]==scc[2*i+1]) {// cout<<scc[2*i]<<" "<<scc[2*i+1]<<endl; puts("bad luck"); return; } else f[scc[2*i]]=scc[2*i+1],f[scc[2*i+1]]=scc[2*i]; } BuildDag(n); topsort(); for(int i=2;i<2*n;i+=2) { if(i!=2) printf(" ");// cout<<color[i]<<" "<<color[0]<<endl;; if(color[scc[i]]==color[scc[0]]) printf("%dw",i/2); else printf("%dh",i/2); } printf("\n");}int main(){ //freopen("in","r",stdin); int n,m,x1,x2,y1,y2; char c1,c2; while(scanf("%d%d",&n,&m)&&(n+m)) { int x,y; init(); for(int i=0;i<=2*n;i++) dag[i].clear(); for(int i=0;i<m;i++) { scanf("%d%c%d%c",&x,&c1,&y,&c2); if(c1=='h') x1=2*x,x2=2*x+1; else x1=2*x+1,x2=2*x; if(c2=='h') y1=2*y,y2=2*y+1; else y1=2*y+1,y2=2*y; addEdge(x2,y1); addEdge(y2,x1); } addEdge(0,1); solve(n); } return 0;}
- POJ 3648 Wedding
- poj 3648 Wedding
- poj 3648 wedding
- poj 3648 Wedding
- poj 3648 Wedding
- poj 3648 Wedding
- POJ 3648 Wedding
- poj 3648 Wedding 2sat
- poj 3648 Wedding 2-SAT
- 2-sat->poj 3648 Wedding
- POJ 3648 Wedding(2-SAT)
- 【POJ】3648 Wedding 2-sat
- UVA 11294 POJ 3648 Wedding
- poj 3648 Wedding 2-sat
- POJ 3648 Wedding 2-SAT
- poj 3648 Wedding 2_SAT
- POJ 3648Wedding 2-sat
- |poj 3648|2-SAT|Wedding
- 钓鱼记
- BeesCMS去掉底部版权信息
- 《windows核心编程系列》五谈谈线程基础
- 数学-三分->讲解
- hdu 4021 24 Puzzle ( 逆序数判断是否可解 )
- poj 3648 Wedding
- C#SQL数据库操作
- 《OpenGL超级宝典》笔记(一)
- vc2010中设置ipch与sdf的位置
- 面向对象的设计原则
- android中通过contentprovider访问通讯录代码实现(两种方法实现)
- 将运算交给每一个人?
- Android UDP收发开发及问题解决
- java设计原则之开闭原则 (转载)