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 ...
随机推荐
- 转 LIST INCARNATION OF DATABASE
incarnation在英文中是“化身”的意思. 那么在oracle中,它又是什么意思呢?有什么作用呢? 我们看一些基本概念 Current Incarnation(当前化身):数据库当前正在使用的化 ...
- Ignite cahce 存储object类型数据和object类型数据序列化后string存储区别
Ignite cache在存储时 object类型的数据和 序列化该object成string类型 两者存储时间差不多. 但是这两者在读取出来的时候,string类型比object类型快很多. 以下为 ...
- layui 数据返回但是table表格未渲染出来的问题
最近蜂鸟速驾项目用layui框架,table进行表格渲染 控制台打印发现有数据,但是table.render渲染不出来,后来发现是数据已经返回但是页面元素未加载完成的缘故,用setTimeout定时器 ...
- Java Swing笔记
想到了解一下GUI主要是想用来做点小工具,记录一些笔记. 文本框自动换行和滚动条 private static JTextArea addJTextArea(JPanel panel, int x, ...
- oracle 查询及删除表中重复数据
create table test1( id number, name varchar2(20) ); ,'jack'); ,'jack'); ,'peter'); ,'red'); insert i ...
- Java网站开发的一些问题以及解决(cookie消失,上传头像,js等)
1.首先是cookie的问题,很多人都是遇到了将数据存储到cookie中并且add到response之中,但是还有返回其他页面或者刷新页面cookie消失的情况,除了设置cookie的存活时间外, 还 ...
- pat00-自测1. 打印沙漏(20)
00-自测1. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打 ...
- 机器学习——GBDT
基础概念 GBDT(Gradient Boosting Decision Tree) 全称梯度提升决策树,是一种迭代的决策树算法.GBDT是集成学习Boosting的家族成员,GBDT中的树是回归树, ...
- bzoj 5291: [Bjoi2018]链上二次求和
Description 有一条长度为n的链(1≤i<n,点i与点i+1之间有一条边的无向图),每个点有一个整数权值,第i个点的权值是 a_i.现在有m个操作,每个操作如下: 操作1(修改):给定 ...
- 【Ubuntu】Vritual Box 复制方式克隆
重装系统后之前安装的虚拟机的镜像全都不见了 ,因为重装系统盘C盘会全部重新被格式化. VtritualBox如果没有指定虚拟机存放位置,默认是会放在C盘的,C:\Users\Administrator ...