Codeforces Round #396 (Div. 2) D
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discovers a new relation between two words.
He know that if two words have a relation between them, then each of them has relations with the words that has relations with the other. For example, if like means love and love is the opposite of hate, then like is also the opposite of hate. One more example: if love is the opposite of hate and hate is the opposite of like, then love means like, and so on.
Sometimes Mahmoud discovers a wrong relation. A wrong relation is a relation that makes two words equal and opposite at the same time. For example if he knows that love means like and like is the opposite of hate, and then he figures out that hate means like, the last relation is absolutely wrong because it makes hate and like opposite and have the same meaning at the same time.
After Mahmoud figured out many relations, he was worried that some of them were wrong so that they will make other relations also wrong, so he decided to tell every relation he figured out to his coder friend Ehab and for every relation he wanted to know is it correct or wrong, basing on the previously discovered relations. If it is wrong he ignores it, and doesn't check with following relations.
After adding all relations, Mahmoud asked Ehab about relations between some words based on the information he had given to him. Ehab is busy making a Codeforces round so he asked you for help.
The first line of input contains three integers n, m and q (2 ≤ n ≤ 105, 1 ≤ m, q ≤ 105) where n is the number of words in the dictionary, m is the number of relations Mahmoud figured out and q is the number of questions Mahmoud asked after telling all relations.
The second line contains n distinct words a1, a2, ..., an consisting of small English letters with length not exceeding 20, which are the words in the dictionary.
Then m lines follow, each of them contains an integer t (1 ≤ t ≤ 2) followed by two different words xi and yi which has appeared in the dictionary words. If t = 1, that means xi has a synonymy relation with yi, otherwise xi has an antonymy relation with yi.
Then q lines follow, each of them contains two different words which has appeared in the dictionary. That are the pairs of words Mahmoud wants to know the relation between basing on the relations he had discovered.
All words in input contain only lowercase English letters and their lengths don't exceed 20 characters. In all relations and in all questions the two words are different.
First, print m lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print "NO" (without quotes) and ignore it, otherwise print "YES" (without quotes).
After that print q lines, one per each question. If the two words have the same meaning, output 1. If they are opposites, output 2. If there is no relation between them, output 3.
See the samples for better understanding.
3 3 4
hate love like
1 love like
2 love hate
1 hate like
love like
love hate
like hate
hate like
YES
YES
NO
1
2
2
2
8 6 5
hi welcome hello ihateyou goaway dog cat rat
1 hi welcome
1 ihateyou goaway
2 hello ihateyou
2 hi goaway
2 hi hello
1 hi hello
dog cat
dog hi
hi hello
ihateyou goaway
welcome ihateyou
YES
YES
YES
YES
NO
YES
3
3
1
1
2
题意:a和b是近义词,a和c是反义词,那么b和c也是反义词,有时候主角会搞错,造成两个词互相矛盾的情况,问输入的这样数据关系怎么样,有没有错误关系(看样例)
比如 love和like是1关系,love和hate是2关系,然后1 hate like互相矛盾输出NO,接下来问这些正确的关系如何
解法:
1 看到两个关联就用并查集,当然字符串我们标记一下
2 关系是1的我们合并x,y和x+n,y+n,关系是2的我们合并x,y+n和y,x+n
3 一边合并一边判断是不是YES和NO
4 然后查询两个词的关系。。就是把代码复制了一下
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5;
int tree[*maxn];
int Find(int x)
{
if(x==tree[x])
return x;
return tree[x]=Find(tree[x]);
} void Merge(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
tree[fx]=fy;
}
int n,m,d;
string s;
int Num;
map<string,int>Mp;
int main(){
cin>>n>>m>>d;
for(int i=;i<=n;i++){
cin>>s;
if(Mp[s]==){
Mp[s]=Num;
Num++;
}
}
for(int i=;i<=*n;i++){
tree[i]=i;
}
for(int i=;i<=m;i++){
string s1,s2;
int num;
cin>>num>>s1>>s2;
int ans1=Mp[s1];
int ans2=Mp[s2];
int pos1=Find(ans1);
int pos2=Find(ans1+n);
int Pos1=Find(ans2);
int Pos2=Find(ans2+n);
if((pos1==Pos2||pos2==Pos1)&&num==){
cout<<"NO"<<endl;
}else if((pos1==Pos1||pos2==Pos2)&&num==){
cout<<"NO"<<endl;
}else{
cout<<"YES"<<endl;
if(num==){
Merge(pos1,Pos1);
Merge(pos2,Pos2);
}else{
Merge(pos1,Pos2);
Merge(pos2,Pos1);
}
}
}
for(int i=;i<=d;i++){
string s1,s2;
cin>>s1>>s2;
int ans1=Mp[s1];
int ans2=Mp[s2];
int pos1=Find(ans1);
int pos2=Find(ans1+n);
int Pos1=Find(ans2);
int Pos2=Find(ans2+n);
if(pos1==Pos1){
cout<<""<<endl;
}else if(pos1==Pos2){
cout<<""<<endl;
}else{
cout<<""<<endl;
}
}
return ;
}
Codeforces Round #396 (Div. 2) D的更多相关文章
- 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 Round #396 (Div. 2) A,B,C,D,E
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message dp
C. Mahmoud and a Message 题目连接: http://codeforces.com/contest/766/problem/C Description Mahmoud wrote ...
- Codeforces Round #396 (Div. 2) B. Mahmoud and a Triangle 贪心
B. Mahmoud and a Triangle 题目连接: http://codeforces.com/contest/766/problem/B Description Mahmoud has ...
- Codeforces Round #396 (Div. 2) A. Mahmoud and Longest Uncommon Subsequence 水题
A. Mahmoud and Longest Uncommon Subsequence 题目连接: http://codeforces.com/contest/766/problem/A Descri ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
地址:http://codeforces.com/contest/766/problem/E 题目: E. Mahmoud and a xor trip time limit per test 2 s ...
随机推荐
- CORS 理解(不要那么多术语)
摘要 谈到跨域,不论前端还是后端,多少有点谈虎色变,面试中也常会问到这些问题,浏览器和服务器端到底怎么做才能跨域,他们都做了什么? 同源 vs 跨域 同源,字面意义是相同的源头,即同一个web服务器( ...
- poj3349 Snowflake Snow Snowflakes —— 哈希表
题目链接:http://poj.org/problem?id=3349 题意:雪花有6个瓣,有n个雪花,输入每个雪花的瓣长,判断是否有一模一样的雪花(通过旋转或翻转最终一样,即瓣长对应相等).如果前面 ...
- linux应用之nginx的安装及配置(centos)
Ubuntu/CentOS 系统上安装与配置Nginx 一.在线安装: Ubuntu:sudo apt-get install nginx CentOS: sudo yum install nginx ...
- MPEG学习
Mpeg:moving picture experts group 移动图片专家组 导入:Mpeg技术在我理解就是我们对音视频信息的一个输出标准.主要包括MPEG-1.MPEG-2.MPEG-4.MP ...
- vue 路由监听
发现网上其实有很多种答案,但是测试之后发现很多都不行,或者写的不完整. 一.在app.vue组件内,增加监听$route,如下: watch: { $route(to, from) { console ...
- .NET Framework4网站 无法运行,提示找不到网络名,IO错误等解决办法
.NET Framework4网站 无法运行,提示找不到网络名,IO错误等解决办法 我的这个问题解决了,原因是用的远程桌面连接的服务器, 远程桌面中部署网站的文件夹,引用的竟然是连接此服务器的用户的电 ...
- cf 424
Office Keys 首先显然有随人位置的递增,钥匙的位置也要递增,这样考虑两张做法: 1.$f(i,j)$ 表示前i个人,钥匙到第j个最少用的最大时间,然后$O(nK)$ dp 2.二分时间,对于 ...
- Eigen中的noalias(): 解决矩阵运算的混淆问题
作者:@houkai本文为作者原创,转载请注明出处:http://www.cnblogs.com/houkai/p/6349990.html 目录 混淆例子解决混淆问题混淆和component级的操作 ...
- Flutter实战视频-移动电商-21.分类页_类别信息接口调试
21.分类页_类别信息接口调试 先解决一个坑 取消上面的GridVIew的回弹效果.就是在拖这个gridview的时候有一个滚动的效果 physics: NeverScrollableScrollPh ...
- Flutter实战视频-移动电商-58.购物车_删除商品功能制作
58.购物车_删除商品功能制作 主要做购物车后面的删除按钮 删除的方法写在provide里面 provide/cart.dart文件 传入goodsId,循环对比,找到后进行移除 //删除单个购物车商 ...