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. 《PostgreSQL服务器编程》一一1.3 超越简单函数

    本节书摘来自华章计算机<PostgreSQL服务器编程>一书中的第1章,第1.3节,作者:(美)Hannu Krosing, Jim Mlodgenski, Kirk Roybal 著,更 ...

  2. 自动安装带nginx_upstream_check_module模块的Nginx脚本

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #!/bin/bash    useradd -s /sbin/no ...

  3. MYSQL 排序和分组

    一.MYSQL 中有两种排序方式: 1:通过有序索引顺序扫描直接返回有序数据,这种方式在使用explain 分析查询的时候显示为Using Index ,不需要额外的排序,操作效率较高. 2: 是通过 ...

  4. 数据库SQL语言从入门到精通--Part 3--SQL语言基础知识

    数据库从入门到精通合集(超详细,学习数据库必看) 一.关系 单一的数据结构----关系 现实世界的实体以及实体间的各种联系均用关系来表示 逻辑结构----二维表 从用户角度,关系模型中数据的逻辑结构是 ...

  5. ACM卡常处理办法(虽然我到现在没遇到)

    今天做预流推送,一样的代码.别人500MS(OI选手)而我5S,百思不得其解,然后我知道了还有卡常这一说. 我们今天就来看一看吧: 1.循环展开: 在缓存和寄存器允许的情况下一条语句内大量的展开运算会 ...

  6. 在Windows中快速配置vim

    vim原本是在Linux中的编辑器,如果使用熟练写代码速度可以远高于其它编辑器 当然很多OI比赛也会要求在Linux中进行 然而: 想学Linux,首先要有一个Linux,但有了Linux,这个直播间 ...

  7. 使用jQuery完成课工场论坛列表

    1.点击我要发帖 2.显示出form表单,然后我们填入标题和选择板块 3.点击发布,隐藏表单,发帖列表中出现随机头像,刚才填入的标题和板块显示在列表中,其中还显示出了发布消息的时间 4.再一次的点击我 ...

  8. N - Marriage Match II 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 推荐博客:https://www.cnblogs.com/liuxin13/p/4728131. ...

  9. C# 基础至集合-数组、List<T>、ArrayList、LinkedList、HashMap的一些区别

    1:数组 ]; //赋值 strs[] = "; strs[] = "; //修改 strs[] = "burg"; //删除 没法删除 除非转化为可变数组li ...

  10. RHCSA 第一题 修改root密码

    题目要求 请修改系统的root帐号密码为redhat,确保能够使用root帐号登录系统.说明:server0 的IP为172.25.0.11/24desktop0的IP为172.25.0.10/24 ...