CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合
题目链接:https://cn.vjudge.net/problem/CodeForces-766D
题意
写词典,有些词是同义词,有些是反义词,还有没关系的词
首先输入两个词,需要判断是同义还是是反义,若没关系就按操作归为同义或反义
思路
经典并查集的动物园问题
维护两个并查集,find(a)表示a的同类代表元,find(a+maxn)表示异类代表元
find(a)find(b) && find(a+maxn)find(b+maxn) 就是说ab同类
find(a+maxn)find(b) && find(a)find(b+maxn) 就是说ab异类
其他就是没关系,需要合并
提交过程
WA | 输出问题,NO写成ON,老毛病 |
AC |
代码
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int maxn=1e5+20;
int pre[maxn*2];
map<string, int> toid;
int find(int x){
return (x==pre[x])?x:(pre[x]=find(pre[x]));
}
int join(int a, int b){
a=find(a); b=find(b);
if (a!=b) pre[a]=b;
}
int main(void){
int n, m, q;
char str[100], bstr[100];
while (scanf("%d%d%d", &n, &m, &q)==3 && n){
toid.clear();
for (int i=1; i<=n; i++){
scanf("%s", str);
toid[string(str)]=i;
}
int arg, a, b;
for (int i=1; i<=maxn*2; i++) pre[i]=i;
for (int i=0; i<m; i++){
scanf("%d%s%s", &arg, str, bstr);
a=toid[string(str)];
b=toid[string(bstr)];
if (find(a)==find(b) && find(a+maxn)==find(b+maxn)){
if (arg==1) printf("YES\n");
else printf("NO\n");
}else if (find(a)==find(b+maxn) && find(a+maxn)==find(b)){
if (arg==2) printf("YES\n");
else printf("NO\n");
}else{
printf("YES\n");
if (arg==1) {join(a, b); join(a+maxn, b+maxn);}
else {join(a, b+maxn); join(a+maxn, b);}
}
}
while (q--){
scanf("%s%s", str, bstr);
a=toid[string(str)];
b=toid[string(bstr)];
if (find(a)==find(b) && find(a+maxn)==find(b+maxn)){
printf("1\n");
}else if (find(a)==find(b+maxn) && find(a+maxn)==find(b)){
printf("2\n");
}else printf("3\n");
}
}
return 0;
}
Time | Memory | Length | Lang | Submitted |
---|---|---|---|---|
732ms | 8380kB | 1328 | GNU G++ 5.1.0 | 2018-07-28 14:45:06 |
CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合的更多相关文章
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- Codeforces 766D Mahmoud and a Dictionary 2017-02-21 14:03 107人阅读 评论(0) 收藏
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...
- CodeForces 766D Mahmoud and a Dictionary
并查集. 将每一个物品拆成两个,两个意义相反,然后并查集即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #i ...
- Codeforces.1051G.Distinctification(线段树合并 并查集)
题目链接 \(Description\) 给定\(n\)个数对\(A_i,B_i\).你可以进行任意次以下两种操作: 选择一个位置\(i\),令\(A_i=A_i+1\),花费\(B_i\).必须存在 ...
- Codeforces.1027F.Session in BSU(思路 并查集)
题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(So ...
- hihoCoder #1291 : Building in Sandbox 逆向处理+并查集维护
/** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照 ...
- Codeforces325 D【并查集维护连通性】
参考:大牛blog 思路: 因为是环,所以可以复制一下图,先判断一下和他是不是和与他相邻的8个之一的一个障碍使得构成了一个环,环就是一个连通,用并查集维护即可: 如果没有就ans++,然后并把这个点加 ...
随机推荐
- ZBrush破解版真的好用么?
安装ZBrush®的时候是不是经常出现各种奇葩问题,使用ZBrush时候是不是经常出现停止工作状况,究其原因,原来都是破解搞的鬼.ZBrush破解版你还敢用么? 随着国人对版权的重视,越来越多的制作商 ...
- Pyhton学习——Day34
# 任何语言都会发生多线程,会出现不同步的问题,同步锁.死锁.递归锁# 异步: 多任务, 多个任务之间执行没有先后顺序,可以同时运行,执行的先后顺序不会有什么影响,存在的多条运行主线# 同步: 多任务 ...
- 2、Attentive Group Recommendation----注意力集中的群组推荐
1.摘要: 采用attention和NCF结合解决群组偏好融合的问题. 贡献: 第一个使用神经网络学习融合策略的组推荐. 进一步整合用户-项目交互改进组推荐,减轻冷启动问题. 2.方法: 模型AGRE ...
- python中方法与函数的区别与联系
今天huskiesir在对列表进行操作的时候,用到了sorted()函数,偶然情况下在菜鸟教程上看到了内置方法sort,同样都可以实现我对列表的排序操作,那么方法和函数有什么区别和联系呢? 如下是我个 ...
- Django REST Framework 认证 - 权限 - 限制
一. 认证 (你是谁?) REST framework 提供了一些开箱即用的身份验证方案,并且还允许你实现自定义方案. 自定义Token认证 第一步 : 建表>>>> 定义一个 ...
- 洛谷 P1147 连续自然数和 (滑动窗口)
维护一个滑动窗口即可 注意不能有m到m的区间,因为区间长度要大于1 #include<cstdio> #define _for(i, a, b) for(int i = (a); i &l ...
- Spring Security中的MD5盐值加密
在 spring Security 文档中有这么一句话: "盐值的原理非常简单,就是先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算,这样一来,就算密码是一个很常见的字 ...
- Myeclipse学习总结(9)——MyEclipse2014安装插件的几种方式(适用于Eclipse或MyEclipse其他版本)
众所周知MyEclipse是一个很强大的Java IDE,而且它有许多开源免费又好用的插件,这些插件给我们开发过程中带来了许多方便.插件具有针对性,例如,你如果做安卓开发,可能需要一个ADT(Andr ...
- [terry笔记]python三级菜单
把三级菜单输出,选择后逐层显示,”b“返回上一级菜单. menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村': ...
- Ubuntu 常用快捷键
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50285313 1 桌面 快捷键 作用 ...