嘟嘟嘟

大佬们都说这是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. Vue添加新的响应式属性

    vm.userProfile = Object.assign({}, vm.userProfile, { age: , favoriteColor: 'Vue Green' })

  2. zabbix 监控ipmi

    一,配置ipmi yum -y install OpenIPMI OpenIPMI-devel ipmitool freeipmi 登入IDARAC 在服务端测试是否可以获得数据 ipmitool - ...

  3. linux中Python源码安装和配置

    安装 首先获取安装包,此处版本为3.7 wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz 解压 tar xvf Python- ...

  4. 【CAD】自定义实体的步骤(转)

    本文介绍了构造自定义实体的步骤.必须继承的函数和必须注意的事项 1.新建一个从AcDbEntity继承的类,如EntTest,必须添加的头文件: "stdarx.h"," ...

  5. oem的使用

    1 浏览器输入下面的网址: 虚拟机[安装orcale的机器]:http://localhost:1158/em/ 本机:http://192.168.47.10:1158/em/ 192.168.47 ...

  6. 一个典型案例为你解读TDSQL 全时态数据库系统

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯技术工程官方号发表在腾讯云+社区 经典案例 增量抽取.增量计算等都是T-TDSQL的经典案例.如下以增量计算为例,来分析T-TDS ...

  7. [转]ASP.NET Core / MVC 6 HttpContext.Current

    本文转自:http://www.spaprogrammer.com/2015/07/mvc-6-httpcontextcurrent.html As you know with MVC 6, Http ...

  8. avalon实现日期联动

    前言 上一篇我们说了下Avalon的一些概念,以及一些主要特性,至于Avalon的一些基础教程,正美老师已经做了非常全面的讲述,参见:http://www.cnblogs.com/rubylouvre ...

  9. Checkbox与foreach循环

    呈现形态&控件语法 <span style=”display:inline-block;”> <input id=”checkBox2” type=”checkBox” na ...

  10. 继承Application管理生命周期

    继承Application实现Android数据共享 http://www.jianshu.com/p/75a5c24174b2 jessyan提出一个思路,用Application + 接口来管理扩 ...