嘟嘟嘟

大佬们都说这是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的更多相关文章

  1. UVA-11294 Wedding (2-SAT)

    题目大意:一张长桌,n对夫妻,编号为0~n,这些人要坐在长桌两侧,每对夫妻不能坐在同一侧.其中,有2*m个人相互讨厌,编号为0的夫妻中的妻子不愿意让对面那一侧中有两个相互吵过架的人,找一种排座位方案. ...

  2. 【UVA11294】Wedding (2-SAT)

    题意: 有N-1对夫妻参加一个婚宴,所有人都坐在一个长长的餐桌左侧或者右侧,新郎和新娘面做面坐在桌子的两侧.由于新娘的头饰很复杂,她无法看到和她坐在同一侧餐桌的人,只能看到对面餐桌的人.任意一对夫妻不 ...

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

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

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

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

  5. poj3648 Wedding

    Wedding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10975   Accepted: 3355   Specia ...

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

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

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

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

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

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

  9. POJ3648 Wedding 【2-sat】

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

随机推荐

  1. curl -w函数

    url_effective 最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形. http_code http状态码,如200成功,301转向,404未找到, ...

  2. HTML问题 | 两个Input在同一行连着不留缝隙

    方法1:让两个 input 连在一起写 不换行 <div class="inputDiv"> <input type="text" place ...

  3. (转)Cobbler无人值守批量安装Linux系统

    本文目录: 1.1 pxe安装系统 1.2 cobbler基本介绍 1.3 安装和配置cobbler 1.3.1 安装cobbler 1.3.2 配置dhcp和tftp 1.4 cobbler从本地光 ...

  4. Python快速入门_1

    注释 # 用#号字符开头注释单行 """ 三个引号可以注释多行 三个引号可以注释多行 三个引号可以注释多行 """ 原始数据类型和运算符 ( ...

  5. SQL脚本整理系列一 表分区

    表分区的目的: 1.把历史数据放到另外一个表里面 可以提高查询效率 当然如果经常查询历史数据和新数据的合并结果集这样做就大大的不好了 2.通过把一个表放到不同的文件,不同的文件再存储到不同的磁盘列阵中 ...

  6. 项目视图 Project Browser

    项目视图 在这个视图,你可以访问.管理你当前项目资源. 项目视图左侧面板显示项目的文件夹结构的分层列表,当从列表中单击一个文件夹,其内容会显示在面板的右边.你可以点击小三角展开或折叠文件夹,显示他包含 ...

  7. ThreadPoolExecutor(下篇)

    上篇写到了ThreadPoolExecutor构造方法前4个参数int corePoolSize.int maximumPoolSize,.long keepAliveTime.TimeUnit un ...

  8. OLEDB 静态绑定和数据转化接口

    OLEDB 提供了静态绑定和动态绑定两种方式,相比动态绑定来说,静态绑定在使用上更加简单,而在灵活性上不如动态绑定,动态绑定在前面已经介绍过了,本文主要介绍OLEDB中的静态,以及常用的数据类型转化接 ...

  9. Win10系统安装iis的方法【图文教程】

    1.在win10系统中的开始按钮点击右键,选择控制面板: 2.从控制面板选择“程序”: 然后选择“启用或关闭windows功能” 3.从列表中选择Internet Infomation Service ...

  10. js实现手风琴效果

    之前在慕课网上有练习手风琴效果,但是老师使用jquery简简单单的两三行实现了,今天自己用js练习一下效果 <div id="divbox"> <ul> & ...