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 ...
随机推荐
- SpringCloudAlibaba实战教程系列
一.简介 Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开 ...
- jdbc批量插入数据
//插入很多书(批量插入用法) public void insertBooks(List<Book> book) { final List<Book> tempBook=b ...
- 从蓝瘦“想哭”到 SELinux 看操作系统安全何在
最近一周,来自网络的"想哭"勒索病毒(Wannacry Ransomware)在世界各地同时上演了一部绑匪大片,台词华丽,演技出色,当仁不让地新晋世界第一网红.全球各国除了默默忙于 ...
- 以内存级速度实现存储?XPoint正是我们的计划
随着计算能力虚拟化技术的普及,存储机制在速度上远逊于内存这一劣势开始变得愈发凸显. 这一巨大的访问速度鸿沟一直是各项存储技术想要解决的核心难题:纸带.磁带.磁盘驱动器乃至闪存记忆体等等,而如今最新一代 ...
- 替换input单选框的样式
实现效果:. css的input单选框的样式很丑,有时候不想使用原生的样式,如上照片,可以使用下面的方法. 思路是,给inpu加visibility:hidden隐藏,然后使用不同的图片绝对定位覆盖在 ...
- 扩欧(exgcd讲解)
注意本文的证明都来源于这位大大大大大大大牛 知识点.扩展欧几里得求逆元 看完下面的证明后建议联系一下这题同余方程 可以对exgcd的用途和写法有有初步了解. \(问题描述:对于三个自然数 a,b,c ...
- python学习之if条件句的使用
if循环 if 条件: 代码块 运行 if else的用法 if elseif else用法 if 条件1: elif 条件2: elif条件3: else:
- hadoop文件系统常用操作
详细可参考hadoop官方文档filesystem shell一节 使用hadoop离不开文件系统,比如hdfs,我们可能需要从hdfs中读取文件作为输入,并将输出保存到hdfs上某个文件中 首先创建 ...
- MySQL 主从复制原理及过程讲解
mysql主从原理描述,摘自老男孩. 下面简 单描述下 MySQL Replication 复制的原理及过程 . 1.在 Slave 服务器上执行 start slave 命令开启主从复制开关,主从复 ...
- 【hdu1024】简单dp
http://acm.hdu.edu.cn/showproblem.php?pid=1024 最大m字段和,题目就不多说了,经典dp 这题坑爹...首先不说明m的范围(n<=1000000),还 ...