UVA11294 Wedding
大佬们都说这是2-SAT入门题,然而对于刚学2_SAT的本菜鸡来说半天才理解……
题面:新娘和新郎不能坐在同一侧,妻子和丈夫不能坐在同一侧,有**关系的两个人必须至少一个坐在新娘一侧,问方案。
对于有**关系的两个人x, y,如果x坐在新郎一侧,那么y必须坐在新娘一侧,从而得出y的妻子(丈夫)必须坐在新郎一侧。那么令x表示妻子和新郎坐在同侧,x + n表示丈夫和新郎坐在同一侧。比如一对关系(3w, 5h)那么就连这么两条边:(3, 5)和((5 + n), (3 + n))。剩下同理。
有点坑儿的地方:
1.点从0开始。
2.别忘了新娘和新郎,所以连边(1, n + 1).
3.注意格式。
4.因为有spj,所以样例不过都可能AC。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e3 + ;
const int maxm = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), las = ' ';
while(!isdigit(ch)) las = ch, ch = getchar();
while(isdigit(ch)) ans = ans * + ch - '', ch = getchar();
if(las == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, m;
struct Edge
{
int to, nxt;
}e[maxm << ];
int head[maxn << ], ecnt = ;
void addEdge(int x, int y)
{
e[++ecnt].to = y;
e[ecnt].nxt = head[x];
head[x] = ecnt;
} stack<int> st;
bool in[maxn << ];
int dfn[maxn << ], low[maxn << ], cnt = ;
int col[maxn << ], ccol = ;
void tarjan(int now)
{
dfn[now] = low[now] = ++cnt;
st.push(now); in[now] = ;
for(int i = head[now]; i; i = e[i].nxt)
{
if(!dfn[e[i].to])
{
tarjan(e[i].to);
low[now] = min(low[now], low[e[i].to]);
}
else if(in[e[i].to]) low[now] = min(low[now], dfn[e[i].to]);
}
if(dfn[now] == low[now])
{
int x; ++ccol;
do
{
x = st.top(); st.pop();
col[x] = ccol; in[x] = ;
}while(x != now);
}
} int num(int x)
{
return x > n ? x - n : x + n;
} void init()
{
while(!st.empty()) st.pop();
for(int i = ; i <= (n << ); ++i)
head[i] = dfn[i] = low[i] = col[i] = in[i] = ;
ecnt = cnt = ccol = ;
} int main()
{
while(scanf("%d%d", &n, &m), n > || m > )
{
init();
for(int i = ; i <= m; ++i)
{
int x, y;
char c1, c2;
scanf("%d%c %d%c", &x, &c1, &y, &c2);
x++; y++;
if(c1 == 'h') x += n;
if(c2 == 'h') y += n;
addEdge(x, num(y));
addEdge(y, num(x));
}
addEdge(, + n);
for(int i = ; i <= (n << ); ++i) if(!dfn[i]) tarjan(i);
bool flg = ;
for(int i = ; i <= n && flg; ++i) if(col[i] == col[i + n]) flg = ;
if(!flg) {puts("bad luck"); continue;}
for(int i = ; i <= n; ++i) printf("%d%c%c", i - , col[i] > col[i + n] ? 'w' : 'h', " \n"[i == n]);
if(n < ) enter;
}
return ;
}
UVA11294 Wedding的更多相关文章
- UVA-11294 Wedding (2-SAT)
		题目大意:一张长桌,n对夫妻,编号为0~n,这些人要坐在长桌两侧,每对夫妻不能坐在同一侧.其中,有2*m个人相互讨厌,编号为0的夫妻中的妻子不愿意让对面那一侧中有两个相互吵过架的人,找一种排座位方案. ... 
- 【UVA11294】Wedding (2-SAT)
		题意: 有N-1对夫妻参加一个婚宴,所有人都坐在一个长长的餐桌左侧或者右侧,新郎和新娘面做面坐在桌子的两侧.由于新娘的头饰很复杂,她无法看到和她坐在同一侧餐桌的人,只能看到对面餐桌的人.任意一对夫妻不 ... 
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
		题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ... 
- NodeJs 学习笔记(一)Wedding 项目搭建
		说明:Ubuntu16.04 自带的NodeJs版本太低,安装包更新不了,只能编译安装了 一.NodeJs编译安装 下载:https://nodejs.org/en/download/ 修改目录权限: ... 
- poj3648 Wedding
		Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10975 Accepted: 3355 Specia ... 
- Wedding (poj 3648 2-SAT 输出随意一组解)
		Language: Default Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9004 Accept ... 
- poj 3648 Wedding 2-SAT问题入门题目
		Description Up to thirty couples will attend a wedding feast, at which they will be seated on either ... 
- POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)
		Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10427 Accepted: 3170 Specia ... 
- POJ3648 Wedding  【2-sat】
		题目 Up to thirty couples will attend a wedding feast, at which they will be seated on either side of ... 
随机推荐
- VMware虚拟网卡设置问题
			具体操作过程如下: (1)为虚拟机添加虚拟网卡 (2)添加后会自动分配子网ip,不用修改.点击应用,确定. (3)添加完成后本机的网络上会多出一个网络适配器,根据虚拟机器中的ip设置此ip地址, 这里 ... 
- python读取excel表格生成sql语句 第一版
			由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦 作为程序猿当然要想办法解决,用Python写一个程序解决 需要用到 xlrd li ... 
- 1.2 js基础
			1.onchange 99%用到select上边. 2.js是干什么的,修改css样式和属性 3.选项卡步骤 1.获取元素 2.循环给按钮加自定义属性 3.循环给按钮加事件 4.封装 ... 
- netstat参数
			1.功能与说明 netstat 用于显示linux中各种网络相关信息.如网络链接.路由表.接口状态链接.多播成员等等. 定义:Netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TC ... 
- vim脚本语言
			转自:http://man.chinaunix.net/newsoft/vi/doc/usr_41.html#usr_41.txt Vim 脚本语言在很多地方用到,包括 vimrc 文件, 语法文件, ... 
- ACdream 1098——圆有点挤——————【数学计算】
			圆有点挤 Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Status Pr ... 
- 2017 年 9 月 27 日 js(1.两个select 内容互换  2.单选按钮  同意可点击下一步 3. 全选框)
			1.两个select 内容互换 <!DOCTYPE html><html> <head> <meta charset="UTF- ... 
- linux基础-wget、apt-get、yum的区别
			Linux操作系统下安装与下载软件是Linux非常基本也非常重要的命令,分清wget.apt-get.yum的区别很重要. Linux操作系统主要分为两大类: RedHat系列:Redhat.Cent ... 
- BZOJ2535: [Noi2010]Plane 航空管制2(拓扑排序 贪心)
			题意 题目链接 Sol 非常妙的一道题. 首先不难想到拓扑排序,但是直接对原图按\(k\)从小到大拓扑排序是错的.因为当前的\(k\)大并不意味着后面的点\(k\)也大 但是在反图上按\(k\)从大到 ... 
- stroke和fill顺序对绘图的影响
			用canvas绘制线条和填充,fill()和stroke()调用顺序直接影响绘制的结构 先调用stroke在调用fill,绘制的效果看上去lineWidth只绘制出来一半,还以为是个大问题. < ... 
