Wedding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10975   Accepted: 3355   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

Source

分析:2-SAT问题,先搞清楚每个点拆成什么.对于第i对夫妇,i*2表示丈夫坐在新娘这一边,i*2+1表示妻子坐在新娘这一边.这样拆点的话,每对夫妇内部是不需要连边的,因为i*2和i*2+1是互相矛盾的,两个只能取一个.而并不是取了i*2就要取i*2对立的那一个.这是当初令我比较困惑的一个问题.
          主要的就是处理有矛盾的组合了.如果第i对的丈夫坐在了新娘对面,他不能和第j对的妻子坐在同一边,那么它肯定是和第j对的丈夫坐在同一边,以此类推,利用矛盾关系连边.
          最后要输出方案,常用的办法是对缩点后的图反向连边,跑一次拓扑排序,先到达的强连通分量为真,对立的强连通分量为假.
#include <cstdio>
#include <stack>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ;
int n, m, head[maxn], to[maxn], id[maxn], nextt[maxn], tot = , scc[maxn], pre[maxn], low[maxn], dfs_clock, cnt;
int head2[maxn], to2[maxn], nextt2[maxn], tot2 = , du[maxn], ok[maxn];
stack <int> s;
bool flag = true; void init()
{
memset(head, , sizeof(head));
memset(id, , sizeof(id));
tot = ;
memset(scc, , sizeof(scc));
memset(pre, , sizeof(pre));
memset(low, , sizeof(low));
dfs_clock = cnt = ;
memset(head2, , sizeof(head2));
tot2 = ;
memset(ok, , sizeof(ok));
memset(du, , sizeof(du));
flag = true;
} void add(int x, int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void add2(int x, int y)
{
to2[tot2] = y;
nextt2[tot2] = head2[x];
head2[x] = tot2++;
} void tarjan(int u)
{
s.push(u);
pre[u] = low[u] = ++dfs_clock;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (!pre[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else
if (!scc[v])
low[u] = min(low[u], pre[v]);
}
if (low[u] == pre[u])
{
cnt++;
while ()
{
int t = s.top();
s.pop();
scc[t] = cnt;
if (t == u)
break;
}
}
} void topo()
{
queue <int> q;
for (int i = ; i <= cnt; i++)
if (!du[i])
q.push(i);
while (!q.empty())
{
int u = q.front();
q.pop();
if (!ok[u])
{
ok[u] = ;
ok[id[u]] = ;
for (int i = head2[u]; i; i = nextt2[i])
{
int v = to2[i];
if ((--du[v]) == )
q.push(v);
}
}
}
} int main()
{
while (scanf("%d%d", &n, &m) == && (n || m))
{
init();
for (int i = ; i <= m; i++)
{
int a, b;
char c, d;
scanf("%d%c %d%c", &a, &c, &b, &d);
add(c == 'h' ? a * + : a * , d == 'h' ? b * : b * + );
add(d == 'h' ? b * + : b * , c == 'h' ? a * : a * + );
}
add(, );
for (int i = ; i < n * ; i++)
if (!pre[i])
tarjan(i);
for (int i = ; i < n; i++)
{
if (scc[i * ] == scc[i * + ])
{
flag = false;
break;
}
id[scc[i * ]] = scc[i * + ];
id[scc[i * + ]] = scc[i * ];
}
if (!flag)
puts("bad luck");
else
{
for (int i = ; i < * n; i++)
{
for (int j = head[i]; j; j = nextt[j])
{
int v = to[j];
if (scc[v] != scc[i])
{
add2(scc[v], scc[i]);
du[scc[i]]++;
}
}
}
topo();
for (int i = ; i < n; i++)
{
if (ok[scc[i * ]] == )
printf("%dh ", i);
else
printf("%dw ", i);
}
printf("\n");
}
} return ;
}

poj3648 Wedding的更多相关文章

  1. POJ3648 Wedding 【2-sat】

    题目 Up to thirty couples will attend a wedding feast, at which they will be seated on either side of ...

  2. POJ-3648 Wedding 2sat

    题目链接:http://poj.org/problem?id=3648‘ 题意:一对情人举行婚礼,有n对夫妇参加,别人对着坐在一个长桌子的两边,新娘和新郎坐在最后面,新娘只能看见坐在他对面的人.现在, ...

  3. POJ3648:Wedding——题解(配2-SAT简易讲解)

    http://poj.org/problem?id=3648 (在家,而且因为2-SAT写的不明不白的,所以这篇详细写) 题目大意: 有一对新人结婚,邀请了n-1 对夫妇去参加婚礼.婚礼上所有人要坐在 ...

  4. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  5. poj3648

    poj3648 题意 有一对新人结婚,n-1对夫妇去参加婚礼.有一个很长的座子,新娘与新郎坐在座子的两边(相反).接下来n-1对夫妇就坐,其中任何一对夫妇都不能坐在同一边,且(有一些人有奸情)这些有奸 ...

  6. NodeJs 学习笔记(一)Wedding 项目搭建

    说明:Ubuntu16.04 自带的NodeJs版本太低,安装包更新不了,只能编译安装了 一.NodeJs编译安装 下载:https://nodejs.org/en/download/ 修改目录权限: ...

  7. Wedding (poj 3648 2-SAT 输出随意一组解)

    Language: Default Wedding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9004   Accept ...

  8. poj 3648 Wedding 2-SAT问题入门题目

    Description Up to thirty couples will attend a wedding feast, at which they will be seated on either ...

  9. POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)

    Wedding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10427   Accepted: 3170   Specia ...

随机推荐

  1. java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理

    一.问题描述 项目开发使用的是SSM框架,项目那个正常运行,开发一个新功能后,添加了一些配置文件,再重新运行项目抛出异常,找不到name为init的bean. 二.异常信息详细 六月 30, 2018 ...

  2. Java fluent风格(转载)

    转载:java Fluent风格 一.我们先写一个通常的,即不使用fluent风格 1.实体类 package com.xbq.demo.stu; /** * @ClassName: Student ...

  3. 数据时代的的企业管理 记SAP商业同略会

    [PConline 资讯]在2012 SAP中国商业同略会城市论坛深圳站上,自SAP中国的萧洁云总裁和张志琦先生,对SAP中国的战略.SAP的技术战略,以及SAP对于行业趋势分析与媒体进行了沟通,对数 ...

  4. mac 安装配置使用nexus3.x

    一.nexus安装 前置条件 :已经安装了JDK 1:下载nexus(http://www.sonatype.com/download-oss-sonatype) 最新版本3.0,下载目录为/User ...

  5. HttpServlet 详解(基础)

    HttpServlet详解 大家都知道Servlet,但是不一定很清楚servlet框架,这个框架是由两个Java包组成:javax.servlet和javax.servlet.http. 在java ...

  6. 1.airflow的安装

    1.环境准备1.1 安装环境1.2 创建用户2.安装airflow2.1 安装python2.2 安装pip2.3 安装数据库2.4 安装airflow2.4.1 安装主模块2.4.2 安装数据库模块 ...

  7. loadrunner socket协议问题归纳(6)

    首先让我们先看一下loadrunner- winsock 函数 一览表: lrs_accept_connection 接受侦听套接字连接 lrs_close_socket 关闭打开的套接字       ...

  8. “Hello World!”团队第六周的第一次会议

    今天是我们团队“Hello World!”团队第六周召开的第一次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 ...

  9. 软工1816 · Alpha冲刺(6/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 alpha冲刺时间延后一周,重新规划任务安排 完成食堂店铺经纬度标注,以供美食 ...

  10. Java单例模式&static成员变量 区别

    当需要共享的变量很多时,使用static变量占用内存的时间过长,在类的整个生命周期. 而对象只是存在于对象的整个生命周期.   //饿汉式 class Single//类一加载,对象就已经存在了. { ...