UESTC_王之盛宴 2015 UESTC Training for Graph Theory<Problem K>
K - 王之盛宴
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
王征战回来了,为了庆祝胜利,王准备请大家吃饭!
于是n个人来到了一家豪华餐厅,这家餐厅有一张长————————长的桌子,每个人只能坐在桌子的南北两侧
一行人中,有p对A关系,m对B关系,如果u和v有A关系,则u和v必须坐在不同侧,如果u和v有B关系,则u和v必须坐在同侧
如果一种座位安排既满足所有A关系也满足所有B关系,则这种安排是和谐的。
王将会选一侧坐下,然后其他人再坐下,现在王想知道,是否存在一种和谐的座位安排。
Input
第一行是描述中的三个整数n,p,m(2≤n≤104,0≤p≤104,0≤m≤104)
接下去n行每行一个字符串,表示参加宴会的人的名字,字符串保证不重复,不含空格且长度不超过10。
接下去p行每行两个字符串x和y,表示x和y具有A关系,x和y用空格隔开
接下去m行每行两个字符串x和y,表示x和y具有B关系,x和y用空格隔开
最后一行一个字符c,c=N表示王坐在北侧,c=S表示王坐在南侧
王的名字总是King
Output
如果存在和谐的座位安排,则第一行输出Yes
然后接下去n行每行一个字符串s和大写英文字母c,
表示名字为s的人坐的位置,c=N表示坐在北侧,c=S表示坐在南侧
座位安排可以按任意次序输出
如果不存在这样的座位安排,输出No
Sample input and output
| Sample Input | Sample Output |
|---|---|
4 3 1 |
No |
4 3 1 |
Yes |
解题思路:
1.将拥有 B 关系的看作连通分量(求连通分量)
2.跑A关系,保证连通分量中不存在A关系
3.连通分量缩点,进行二染色
注意到B关系我们建边后,跑连通分量可以直接用并查集维护,然后A关系我们依次检查每条边,如果拥有A关系的两个点在一个连通分量中肯定无法构造出解.
之后对连通分量跑二染色即可
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#define pb push_back /*
解题报告:
1.将拥有 B 关系的看作连通分量(求连通分量)
2.跑A关系,保证连通分量中不存在A关系
3.连通分量缩点,进行二染色
*/ typedef long long ll;
using namespace std; typedef pair < ll , ll > strhash;
typedef pair < ll , ll > spj;
map<strhash,int>new_hash;
set<spj>s; const int maxn = 3e4 + ;
const ll p1 = ;
const ll p2 = ;
const ll mod1 = 1e9 + ;
const ll mod2 = 1e9 + ;
const char * kingname = "King"; inline strhash GetHashVal(const char * beg)
{
int len = strlen(beg);
strhash res;
ll x1 = ;
ll x2 = ;
for(int i = ; i < len ; ++ i)
{
x1 = (x1 * p1 + beg[i]) % mod1;
x2 = (x2 * p2 + beg[i]) % mod2;
}
res.first = x1 , res.second = x2;
return res;
} vector<int>EA[maxn] , EB[maxn] , EC[maxn]; int n,p,m,set_un[maxn],tot = , colour[maxn];
char kingpos[];
char kc,oc;
char name[maxn][]; void dfs(int cur)
{
set_un[cur] = tot;
for(int i = ; i < EB[cur].size() ; ++ i)
{
int nextnode = EB[cur][i];
if (set_un[nextnode] == -)
dfs(nextnode);
}
} bool colourmap(int cur)
{
for(int i = ; i < EC[cur].size() ; ++ i)
{
int nextnode = EC[cur][i];
if (colour[cur] == colour[nextnode])
return false;
if (!colour[nextnode])
{
colour[nextnode] = - colour[cur];
if (!colourmap(nextnode))
return false;
}
}
return true;
} int main(int argc,char *argv[])
{
scanf("%d%d%d",&n,&p,&m);
memset(set_un,-,sizeof(set_un));
for(int i = ; i < n ; ++ i)
{
scanf("%s",name[i]);
strhash temp = GetHashVal(name[i]);
new_hash[GetHashVal(name[i])] = i;
}
for(int i = ; i < p ; ++ i)
{
char bf1[] , bf2[];
scanf("%s%s",bf1,bf2);
int p1 = new_hash[GetHashVal(bf1)] , p2 = new_hash[GetHashVal(bf2)];
EA[p1].pb(p2) , EA[p2].pb(p1);
}
for(int i = ; i < m ; ++ i)
{
char bf1[] , bf2[];
scanf("%s%s",bf1,bf2);
int p1 = new_hash[GetHashVal(bf1)] , p2 = new_hash[GetHashVal(bf2)];
EB[p1].pb(p2) , EB[p2].pb(p1);
}
scanf("%s",kingpos);
for(int i = ; i < n ; ++ i) // 对 B 关系跑连通分量
if (set_un[i] == -)
{
dfs(i);
tot++;
}
int check = ;
for(int i = ; i < n ; ++ i)
{
for(int j = ; j < EA[i].size() ; ++ j)
{
int nextnode = EA[i][j];
if (set_un[i] == set_un[nextnode])
{
check = ;
break;
}
else
{
int p1 = set_un[i] , p2 = set_un[nextnode];
if (p1 > p2)
swap(p1,p2);
spj temp(p1,p2);
if (s.count(temp))
continue;
EC[p1].pb(p2) , EC[p2].pb(p1) ;
s.insert(temp);
}
}
if (!check) //连通分量中存在 A 关系
{
printf("No\n");
return ;
}
}
memset(colour,,sizeof(colour));
for(int i = ; i < tot ; ++ i)
{
if (!colour[i])
{
colour[i] = ;
if (!colourmap(i))
{
printf("No\n");
return ;
}
}
}
printf("Yes\n");
kc = kingpos[];
if (kc == 'N')
oc = 'S';
else
oc = 'N';
int kingcolour = colour[set_un[new_hash[GetHashVal(kingname)]]];
for(int i = ; i < n ; ++ i)
{
char out;
if (colour[set_un[i]] == kingcolour)
out = kc;
else
out = oc;
printf("%s %c\n",name[i],out);
}
return ;
}
UESTC_王之盛宴 2015 UESTC Training for Graph Theory<Problem K>的更多相关文章
- UESTC_树上的距离 2015 UESTC Training for Graph Theory<Problem E>
E - 树上的距离 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 262143/262143KB (Java/Others) Subm ...
- UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>
L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_小panpan学图论 2015 UESTC Training for Graph Theory<Problem J>
J - 小panpan学图论 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) S ...
- UESTC_排名表 2015 UESTC Training for Graph Theory<Problem I>
I - 排名表 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>
H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Others) Subm ...
- UESTC_传输数据 2015 UESTC Training for Graph Theory<Problem F>
F - 传输数据 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- UESTC_邱老师的脑残粉 2015 UESTC Training for Graph Theory<Problem D>
D - 邱老师的脑残粉 Time Limit: 12000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥与时空漫游 2015 UESTC Training for Graph Theory<Problem C>
C - 秋实大哥与时空漫游 Time Limit: 4500/1500MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Su ...
- UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>
B - 秋实大哥带我飞 Time Limit: 300/100MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
随机推荐
- Codeforce 215 div1
C 把每个qi看成点,则问题转化为:求一个最大的k,遍历k个点的完全图需要的最小步数+1不超过n, (这里+1的原因是把起点加进去) 讨论k的奇偶: k为奇数,每个点度数为偶数,这是一个欧拉回路,步数 ...
- Exchange Server 2010/2013功能差异
- 《Learn python the hard way》Exercise 48: Advanced User Input
这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...
- 设置MATLAB中figure的背景为白色
matlab的图形窗口每次背景都是灰色的,而我希望每次都是白色的背景,方便用图: 每次总是需要添加figure('color','w');或者figure('color',[1 1 1])或者set( ...
- grub2手动引导ubuntu
測试机OS为ubuntu 14.04.1 LTS x86_64 磁盘分区情况为: Filesystem 1K-blocks Used Available Use% Mounted on ...
- python应用之文件属性浏览
import time,os def showFilePROPERTIES(path): for root,dirs,files in os.walk(path,True): print('位置:' ...
- openssl AES加密算法API的使用示例
openssl为用户提供了丰富的指令,同时也提供了供编程调用的API,本文以使用128位aes算法的ecb模式进行加密和解密验证,如下所示 第一种方法,直接使用aes算法提供的api进行调用,代码如下 ...
- css_day7
- python - 类的字段
一.静态字段:保存在类里面 1.创建静态字段: class Foo: CC = 123 # 字段(静态字段),保存在类里 def __init__(self): self.name = 'alex' ...
- C#中Property和Attribute的区别
C#中Property和Attribute的区别 Attribute 字段Property 属性(get;set;) 属性的正常写: private string name; public strin ...