Wedding
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进阶 记录点拓扑的更多相关文章

  1. 总结-一本通提高篇&算竞进阶记录

    当一个人看见星空,就再无法忍受黑暗 为了点亮渐渐沉寂的星空 不想就这样退役 一定不会鸽の坑 . 一本通提高篇 . 算竞进阶 . CDQ & 整体二分 . 平衡树 . LCT . 字符串 . 随 ...

  2. Scut 进阶:网络模型拓扑

    处理消息流程: 关于是否能用 json 串作为 response? 在最后写消息的时候要加上控制选项,将Response类型,事直接以字节流,还是转json串再转字节流的方式进行编码了,如果要转jso ...

  3. [模板][Luogu3387] 缩点 - Tarjan, 拓扑+DP

    Description 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次 ...

  4. 从零开始入门 K8s | 应用存储和持久化数据卷:存储快照与拓扑调度

    作者 | 至天 阿里巴巴高级研发工程师 一.基本知识 存储快照产生背景 在使用存储时,为了提高数据操作的容错性,我们通常有需要对线上数据进行 snapshot ,以及能快速 restore 的能力.另 ...

  5. acwing 848 有向图的拓扑序列

    地址 https://www.acwing.com/problem/content/description/850/ 题目描述给定一个n个点m条边的有向图,图中可能存在重边和自环. 请输出任意一个该有 ...

  6. Toposort(拓扑排序)dfs递归模板

    最近刷了几题拓扑排序的题,记录一下拓扑排序 在有向图中,并且按照一定的规则(题目所给的规则)排序.如果图中出现了有向环的话就无法排序了. int gap[maxn][maxn];//记录下有向边 in ...

  7. ArcGis拓扑——规则、概念与要点

    在地理数据库中,拓扑是定义点要素.线要素以及面要素共享重叠几何的方式的排列布置.例如,街道中心线与人口普查区块共享公共几何,相邻的土壤面共享公共边界. 处理拓扑不仅仅是提供一个数据存储机制.在 Arc ...

  8. OpenCascade拓扑对象之:TopoDS_Shape的三要素

    @font-face { font-family: "Times New Roman" } @font-face { font-family: "宋体" } @ ...

  9. 一文读懂SuperEdge拓扑算法

    前言 SuperEdge service group 利用 application-grid-wrapper 实现拓扑感知,完成了同一个 nodeunit 内服务的闭环访问 在深入分析 applica ...

随机推荐

  1. 在Spring Boot中加载初始化数据

    文章目录 依赖条件 data.sql文件 schema.sql 文件 @sql注解 @SqlConfig 注解 在Spring Boot中加载初始化数据 在Spring Boot中,Spring Bo ...

  2. SQLite使用(二)

    sqlite3_exec虽然好用,但是一般不推荐直接使用. 常用的一组操作是: 关于sqlite3_exec和sqlite3_prepare_v2的使用场景,建议如下: 一个小DEMO: #inclu ...

  3. YOLACT : 首个实时one-stage实例分割模型,29.8mAP/33.5fps | ICCV 2019

    论文巧妙地基于one-stage目标检测算法提出实时实例分割算法YOLACT,整体的架构设计十分轻量,在速度和效果上面达到很好的trade-off.   来源:[晓飞的算法工程笔记] 公众号 论文: ...

  4. HashMap面试知识点总结

    主要参考 JavaGuide 和 敖丙 的文章, 其中也有参考其他的文章, 但忘记保存链接了, 文中图片也是引用别的大佬的, 请见谅. 新手上路, 若有问题, 欢迎指正. 背景 HashMap 的相关 ...

  5. TransactionHelper

    public class TransactionHelper { public static OracleTransaction ora_Transaction = null; public stat ...

  6. Collections集合工具类常用的方法

    java.utils.Collections //是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Collection& ...

  7. 李婷华 201771010113 《面向对象程序设计(java)》 第二周学习总结

    第一部分:理论知识学习部分 第三章 java的基本程序设计结构 本章主要学习数据类型.变量.运算符.类型转换.字符串.输入输出.控制流程.大数值.数组等内容. 1.基本知识 (1)标识符:由字母.下划 ...

  8. CTR预估模型演变及学习笔记

    [说在前面]本人博客新手一枚,象牙塔的老白,职业场的小白.以下内容仅为个人见解,欢迎批评指正,不喜勿喷![握手][握手] [再啰嗦一下]如果你对智能推荐感兴趣,欢迎先浏览我的另一篇随笔:智能推荐算法演 ...

  9. Redis 学习笔记(一) 字符串 SDS

    SDS 简单动态字符串. SDS的结构: struct sdshdr{ int len;//记录BUF数组中已使用字节的数量 ,等于SDS所八寸字符串的长度 int free;//记录BUF数组中未使 ...

  10. 【Hadoop离线基础总结】Apache Hadoop的三种运行环境介绍及standAlone环境搭建

    Apache Hadoop的三种运行环境介绍及standAlone环境搭建 三种运行环境 standAlone环境 单机版的hadoop运行环境 伪分布式环境 主节点都在一台机器上,从节点分开到其他机 ...