K - 王之盛宴

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

王征战回来了,为了庆祝胜利,王准备请大家吃饭!

于是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
King
A
B
C
A B
B C
A C
King A
N
No
4 3 1
King
A
B
C
A B
A C
King A
B C
N
Yes
King N
A S
B N
C N

解题思路:

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>的更多相关文章

  1. UESTC_树上的距离 2015 UESTC Training for Graph Theory<Problem E>

    E - 树上的距离 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Subm ...

  2. UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>

    L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. 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 ...

  4. UESTC_排名表 2015 UESTC Training for Graph Theory<Problem I>

    I - 排名表 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  5. UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>

    H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Others) Subm ...

  6. UESTC_传输数据 2015 UESTC Training for Graph Theory<Problem F>

    F - 传输数据 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  7. UESTC_邱老师的脑残粉 2015 UESTC Training for Graph Theory<Problem D>

    D - 邱老师的脑残粉 Time Limit: 12000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  8. UESTC_秋实大哥与时空漫游 2015 UESTC Training for Graph Theory<Problem C>

    C - 秋实大哥与时空漫游 Time Limit: 4500/1500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  9. UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>

    B - 秋实大哥带我飞 Time Limit: 300/100MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit ...

随机推荐

  1. [Qt] fontawesome图标

    fontawesome图标 fontawesome是一个图标的集合,里面有好多的图标,使用起来也还是非常方便的. 图标信息可以到官网去查:http://fontawesome.io/cheatshee ...

  2. Python安装MySQLdb并连接MySQL数据库

    当然了,前提是你已经安装了Python和MySQL.我的Python是2.6版本的. Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/fo ...

  3. (转)android客户端从服务器端获取json数据并解析的实现代码

    今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下       首先客户端从服务器端获取json数据 1.利用HttpUrlConnection   复制代码 ...

  4. Rect

    判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数   BOOL contains = CGRectContainsPoint(CGRect rect, CGPo ...

  5. c#打开指定设备程序以及网址

    //打开计算器 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = @"C:\WINDOWS\ ...

  6. (原)python中使用plt.show()时显示图像

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6039667.html 参考网址: http://matplotlib.org/users/shell. ...

  7. winPcap_1_开篇

    什么是WinPcap WinPcap是一个基于Win32平台的,用于捕获网络数据包并进行分析的开源库. 因为有些应用程序需要直接访问网络中的数据包.也就是说,那些应用程序需要访问原始数据包,即没有被操 ...

  8. 慕课linux学习笔记(二)Xshell与虚拟机的连接

    选择使用的是Xshell5 新建连接 连接成功 修改编码方式,字号,颜色 PS: 连接过程中遇到了很多问题,虚拟机的网络连接我最初选择的是桥连,虚拟机和主机相互之间都能ping通但Xshell就是连接 ...

  9. php 对问卷结果进行统计

    背景: 由于具体工作的原因,我做了一份纸质的问卷调查表,调查表的主要内容是让用户对10项要求(编号为A,B....)进行优先级排序,所以我得到的结果是好几百份类似于A>I>H>G&g ...

  10. 保存BASE64编码图片

    1.前端上传用户图片时,一些K数较小图片,头像图标等 .以bass64编码后的字符串传到服务器. 2.服务器接收并保留到本地. // 页面上点击保存 $.post('/imgupload/save', ...