poj3648 2-SAT进阶 记录点拓扑
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 10556 | Accepted: 3220 | 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 one end, opposite each other, and the bride wears an elaborate headdress that keeps her from seeing people on the same side as her. It is considered bad luck to have a husband and wife seated on the same side of the table. Additionally, there are several pairs of people conducting adulterous relationships (both different-sex and same-sex relationships are possible), and it is bad luck for the bride to see both members of such a pair. Your job is to arrange people at the table so as to avoid any bad luck.
Input
The input consists of a number of test cases, followed by a line containing 0 0. Each test case gives n, the number of couples, followed by the number of adulterous pairs, followed by the pairs, in the form "4h 2w" (husband from couple 4, wife from couple 2), or "10w 4w", or "3h 1h". Couples are numbered from 0 to n - 1 with the bride and groom being 0w and 0h.
Output
For each case, output a single line containing a list of the people that should be seated on the same side as the bride. If there are several solutions, any one will do. If there is no solution, output a line containing "bad luck".
Sample Input
10 6
3h 7h
5w 3w
7h 6w
8w 3w
7h 3w
2w 5h
0 0
Sample Output
1h 2h 3w 4h 5h 6h 7h 8h 9h
题意:题意:一对新婚的夫妇邀请(n-1)对夫妇来参加自己的宴会,这对新人以及这些受邀请的夫妇都坐在长桌子的两边,新娘和新郎分别坐在桌子的两侧,新娘不希望看到她邀请来的那些夫妇之中有妻子和丈夫坐在同一边的情况(即妻子和丈夫要分作桌子的两边),在这n对夫妇中有一些男女存在着暧昧的关系,所以新娘也不希望看到有暧昧关系的人坐在她对面的那一侧.求解是否存在一种满足新娘要求的座位分配方案,如果存在的话,那么就输出这方案,否则输出"bad luck".
这个问题本身就有矛盾点了,即男性还是女性坐在女0对面,所以不用拆点。
另外注意加边只要加两条单向边,因为只关注女0的对面有没有奸夫淫妇,所以就是一旦有一个奸夫淫妇中的一个在新娘对面,那么另外一个的配偶必然也会在其对面,所以建边两个,注意没有同性恋有暧昧关系也是一个非常重要的条件
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N=;
vector<int>v[N];
vector<int>g[N];
bool instack[N];
int q[N],low[N],dfn[N],bl[N],con[N],col[N],in[N];
int l,scnt,cnt,n,m;
void init(){
memset(dfn,,sizeof(dfn));
memset(in,,sizeof(in));
memset(col,,sizeof(col));
for(int i=;i<=*n;++i) v[i].clear(),g[i].clear();
l=scnt=cnt=;
}
inline void add(int a,int b){v[a].push_back(b);}
void Tarjan(int u){
instack[u]=;
low[u]=dfn[u]=++cnt;
q[l++]=u;
for(int i=;i<(int)v[u].size();++i) {
int x=v[u][i];
if(!dfn[x]) {
Tarjan(x);
low[u]=min(low[u],low[x]);
}
else if(instack[x]&&dfn[x]<low[u]) low[u]=dfn[x];
}
if(low[u]==dfn[u]){
int t;++scnt;
do{
t=q[--l];
bl[t]=scnt;
instack[t]=;
}while(t!=u);
}
}
void rebuild(){
for(int i=;i<*n;++i) for(int j=;j<(int)v[i].size();++j){
int a=bl[i],b=bl[v[i][j]];
if(a!=b) {
++in[a];
g[b].push_back(a);
}
}
}
void topsort(){
queue<int>Q;
for(int i=;i<=scnt;++i) if(!in[i]) Q.push(i);
while(!Q.empty()){
int u=Q.front();Q.pop();
if(!col[u]) {
col[u]=;
col[con[u]]=;
}
for(int i=;i<(int)g[u].size();++i) {
int x=g[u][i];
--in[x];
if(!in[x]) Q.push(x);
}
}
}
void solve(){
for(int i=;i<*n;++i) if(!dfn[i]) Tarjan(i);
for(int i=;i<*n;i+=) {if(bl[i]==bl[i+]) {puts("bad luck");return;}
int a=bl[i],b=bl[i+];
con[a]=b;con[b]=a;}
rebuild();
topsort();
for(int i=;i<*n;i+=){
if(i!=) printf(" ");
if(col[bl[i]]==col[bl[]]) printf("%dw",i/);
else printf("%dh",i/);
}
puts("");
}
int main(){
while(scanf("%d%d",&n,&m),n+m){
init();
int a,b;
char c,d;
while(m--){
scanf("%d%c %d%c",&a,&c,&b,&d);
if(c=='h') a=*a+;else a=*a;
if(d=='h') b=*b+;else b=*b;
add(a,b^);
add(b,a^);
}
add(,);
solve();
}
}
poj3648 2-SAT进阶 记录点拓扑的更多相关文章
- 总结-一本通提高篇&算竞进阶记录
当一个人看见星空,就再无法忍受黑暗 为了点亮渐渐沉寂的星空 不想就这样退役 一定不会鸽の坑 . 一本通提高篇 . 算竞进阶 . CDQ & 整体二分 . 平衡树 . LCT . 字符串 . 随 ...
- Scut 进阶:网络模型拓扑
处理消息流程: 关于是否能用 json 串作为 response? 在最后写消息的时候要加上控制选项,将Response类型,事直接以字节流,还是转json串再转字节流的方式进行编码了,如果要转jso ...
- [模板][Luogu3387] 缩点 - Tarjan, 拓扑+DP
Description 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次 ...
- 从零开始入门 K8s | 应用存储和持久化数据卷:存储快照与拓扑调度
作者 | 至天 阿里巴巴高级研发工程师 一.基本知识 存储快照产生背景 在使用存储时,为了提高数据操作的容错性,我们通常有需要对线上数据进行 snapshot ,以及能快速 restore 的能力.另 ...
- acwing 848 有向图的拓扑序列
地址 https://www.acwing.com/problem/content/description/850/ 题目描述给定一个n个点m条边的有向图,图中可能存在重边和自环. 请输出任意一个该有 ...
- Toposort(拓扑排序)dfs递归模板
最近刷了几题拓扑排序的题,记录一下拓扑排序 在有向图中,并且按照一定的规则(题目所给的规则)排序.如果图中出现了有向环的话就无法排序了. int gap[maxn][maxn];//记录下有向边 in ...
- ArcGis拓扑——规则、概念与要点
在地理数据库中,拓扑是定义点要素.线要素以及面要素共享重叠几何的方式的排列布置.例如,街道中心线与人口普查区块共享公共几何,相邻的土壤面共享公共边界. 处理拓扑不仅仅是提供一个数据存储机制.在 Arc ...
- OpenCascade拓扑对象之:TopoDS_Shape的三要素
@font-face { font-family: "Times New Roman" } @font-face { font-family: "宋体" } @ ...
- 一文读懂SuperEdge拓扑算法
前言 SuperEdge service group 利用 application-grid-wrapper 实现拓扑感知,完成了同一个 nodeunit 内服务的闭环访问 在深入分析 applica ...
随机推荐
- time wait 整理
目录 状态转换图 1.谁会进入time wait状态: 主动发起断开连接的一方调用close()函数发送FIN并进入FIN WAIT 1状态,当收到对面反馈的ack之后会进入FIN WAIT2状态.之 ...
- Retrofit的文件上传和进度提示
2019独角兽企业重金招聘Python工程师标准>>> 1.写一个上传监听的接口: /** * Created by Zzm丶Fiona on 2017/7/31. */ publi ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
- P1518 两只塔姆沃斯牛 The Tamworth Two(简单的搜索题)
题目描述 两只牛逃跑到了森林里.农夫John开始用他的专家技术追捕这两头牛.你的任务是模拟他们的行为(牛和John). 追击在10x10的平面网格内进行.一个格子可以是: 一个障碍物, 两头牛(它们总 ...
- 图论--2-SAT--POJ Ikki's Story IV - Panda's Trick
Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping ...
- 题目分享I
题意:2*n的地面,q次操作,每次操作将地面翻转,若该地是地面那翻转就成熔岩,如果是熔岩那翻转就成地面,熔岩人不能走,问人是否能从1,1走到2,n (ps:1,1和2,n不会在翻转的范围内,n,q≤1 ...
- 自定义Element父子不关联的穿梭树
Element自身是有一个Transfer穿梭框组件的,这个组件是穿梭框结合checkbox复选框来实现的,功能比较单一,自己想实现这个功能也是很简单的,只是在项目开发中,项目排期紧,没有闲功夫来实现 ...
- POJ3734(矩阵快速幂)
\(假设现在到第i个积木\) \(红绿恰都是偶数a种方案,恰都是奇数为b种方案,一奇一偶为c种方案\) \(由此考虑i+1个积木的情况\) Ⅰ.一奇一偶的方案 \(如果第i层恰是奇数的情况,那么本次只 ...
- C# 数据操作系列 - 0. 序言
0. 前言 在上一个系列中,我们初步浏览了一下C#的基础知识.这句话的意思就是C#基础知识系列完结了,撒花.当然,并不是因为C#已经讲完了.正是因为我们轻轻地叩开了那扇门,才能看到门后面那瑰丽的世界. ...
- 201771010113 李婷华 《面向对象程序设计(Java)》第八周总结
一.理论知识部分 1.Java为了克服单继承的缺点,Java使用了接口,一个类可以实现一个或多个接口. 2.在Java程序设计语言中,接口不是类,而是对类的一组需求描述,由常量和一组抽象方法组成.接口 ...