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 ...
随机推荐
- 线性时间构造普吕弗(Prüfer)序列
tree -> sequence 首先预处理数组 deg[N], deg[i]表示编号为i的节点的度数,我们每次要删除的节点肯定是 满足deg[i]=1 的编号最小节点, 首先找到所有叶子并选出 ...
- android图标设计事宜
1.Launcher图标 图标的最佳宽高是48x48 dp. ldpi:36*36px,0.75倍密度,一般不用提供,系统会从hdpi取图缩小1倍. mdpi:48*48px, 1倍密度 hdpi:7 ...
- Thinkphp显示系统常量信息的方法(php的用法)
输入 :public function Main() { dump(get_defined_constants(true)); }显示系统信息, 其中: 'APP_PATH' ...
- 常用文件的文件头(附JAVA测试类)
1. MIDI (mid),文件头:4D546864 2. JPEG (jpg),文件头:FFD8FF 3. PNG (png),文件头:89504E47 4. GIF (gif),文件头:47494 ...
- JSON解析---初识
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 全然独立于语言的文本格式 易于人阅读和编写 易于解析和生成 (网络传输速度快) JSON语法规则 数据在 ...
- [RxJS] Logging a Stream with do()
To help understand your stream, you’ll almost always want to log out some the intermediate values to ...
- [置顶] VB6基本数据库应用(三):连接数据库与SQL语句的Select语句初步
同系列的第三篇,上一篇在:http://blog.csdn.net/jiluoxingren/article/details/9455721 连接数据库与SQL语句的Select语句初步 ”前文再续, ...
- CodeManage 源代码管理器v2.0发布
下载地址 欢迎大家提出宝贵的意见和bug
- SqlDbHelper备份,做项目时方便应用(目前不太全,把自己项目中的逐渐转移过来)
****************************************** 这是官网新闻左侧类别那部分用到的 **************************************** ...
- (转).net程序员转战android第三篇---登录模块之静态登录
这一篇我将分2个部分记录登录界面,第一部分是静态登录, 这部分将如何从界面布局.控件使用.文件关系.数据验证.登陆实现等5小块记录. 第二部分是动态登录,这块会基于上面的4小块,在数据验证不是静态数据 ...