POJ3648 Wedding 【2-SAT】
来源:互联网 发布:linux重命名命令 编辑:程序博客网 时间:2024/06/06 02:48
题意:新娘新郎和每对夫妇,都分开坐,新娘不希望看到对面有特殊关系的一对人
思路:可以考虑选一些人坐在新娘对面(也就是新郎这一边),新郎必须选(建边 新娘->新郎),有特殊关系的每一对人建边,反着输出新娘这一边的人
#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<stdlib.h>#include<math.h>#include<vector>#include<list>#include<map>#include<stack>#include<queue>#include<algorithm>#include<numeric>#include<functional>using namespace std;typedef long long ll;const int maxn = 1005;struct edge{int to,next;bool cut;}ed[maxn];int head[maxn], tot; int Low[maxn], DFN[maxn], Stack[maxn]; int Index, top;bool Instack[maxn]; int block,belong[maxn]; int bridge;void init(){memset(head, -1, sizeof(head)); memset(DFN, 0, sizeof(DFN)); memset(Instack, false, sizeof(Instack)); block = tot = 0; Index = top = 0; bridge = 0; }void Tarjan(int u,int pre){ int v; Low[u] = DFN[u] = ++Index; Stack[top++] = u; Instack[u] = true; for(int i = head[u];i != -1;i = ed[i].next) { v = ed[i].to; //if(v == pre)continue; //无向无重边 用,只要不往回走 //if(ed[i].id == pre) continue; //无向图重边判定,不走同一条边 if( !DFN[v] ) { Tarjan(v,u); //Tarjan(v,ed[i].id); //无向图重边判定 if( Low[u] > Low[v] )Low[u] = Low[v]; if(Low[v] > DFN[u]) { bridge++; ed[i].cut = true; ed[i^1].cut = true; } } else if( Instack[v] && Low[u] > DFN[v] ) Low[u] = DFN[v]; } if(Low[u] == DFN[u]) { block++; do { v = Stack[--top]; Instack[v] = false; belong[v] = block; } while( v!=u ); }}//使无向图双连通,(叶子+1) / 2void add(int x,int y){ed[tot].to = y;ed[tot].next = head[x];ed[tot].cut = 0;//ed[tot].id = num; //无向图重边判定 head[x] = tot++;}void SCC(int n) { for(int i = 0; i < n; i++) if(!DFN[i]) Tarjan(i,-1); }int bian(int a,char c,int n){if(c == 'w')return a;elsereturn a+n;}int fan(int a,char c,int n){if(c == 'w')return a+n;elsereturn a;}int main(void){int n,m,a,b;char c,d;while(scanf("%d%d",&n,&m)!=EOF && n+m){init();add(0,n);while(m--){scanf("%d%c%d%c",&a,&c,&b,&d);add(bian(a,c,n),fan(b,d,n));add(bian(b,d,n),fan(a,c,n));}SCC(2*n);int flag = 1;for(int i = 1; i < n; i++){if(belong[i] == belong[i+n]){flag = 0;break;}}if(!flag){printf("bad luck\n");continue;}for(int i = 1; i < n; i++){if(belong[i] > belong[i+n])printf("%dw%c",i,i==n-1?'\n':' ');elseprintf("%dh%c",i,i==n-1?'\n':' ');}}return 0;}
阅读全文
0 0
- POJ3648 Wedding 【2-SAT】
- poj3648 Wedding 2-sat
- 2-SAT——7.0(poj3648 Wedding,完结篇)
- POJ3648.Wedding——2-sat输出任意解
- poj3648 Wedding
- poj3648:Wedding——题解(配2-SAT简易讲解)
- poj3648,2-sat求解
- POJ_3648 Wedding 2-Sat
- UVA11294-Wedding(2-SAT)
- POJ3648——Wedding
- POJ3648-2SAT解的求得
- poj3648 2-sat <输出任意一组解>
- [2-sat]构造一个可行解(poj3648)
- poj 3648 Wedding 2sat
- poj 3648 Wedding 2-SAT
- 2-sat->poj 3648 Wedding
- POJ 3648 Wedding(2-SAT)
- 【POJ】3648 Wedding 2-sat
- mfs分布式存储搭建(二)
- chrome jsonView插件安装
- 直接选择排序
- HDU 6084 寻找母串(卡特兰数)
- mac下环境变量
- POJ3648 Wedding 【2-SAT】
- 第7章 IoC容器 III (Container) -- Spring4.3.8参考文档中文版
- 面试必备:LinkedList源码解析(JDK8)
- jsp跳出循环
- ProgressBar改变成我们想要的加载动画
- ZOJ-3453---Doraemon's Sweet Bullet (线段树)
- python入门(二十二):添加PATH路径
- Java运行时数据区域
- Linux下JDK1.8安装