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.

Input

The first line of input contains three integers nm 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.

Output

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.

Examples
input
3 3 4
hate love like
1 love like
2 love hate
1 hate like
love like
love hate
like hate
hate like
output
YES
YES
NO
1
2
2
2
input
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
output
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的更多相关文章

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

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

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

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

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

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

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

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

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

随机推荐

  1. Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理

    题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...

  2. Zookeeper实现负载均衡原理

    先玩个正常的,好玩的socket编程: 服务端: 首先公共的这个Handler: package com.toov5.zkDubbo; import java.io.BufferedReader; i ...

  3. IPFS - 可快速索引的版本化的点对点文件系统(草稿3)

    摘要 星际文件系统是一种点对点的分布式文件系统, 旨在连接所有有相同的文件系统的计算机设备.在某些方面, IPFS类似于web, 但web 是中心化的,而IPFS是一个单一的Bittorrent 群集 ...

  4. hdu-5747 Aaronson(水题)

    题目链接: Aaronson Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others ...

  5. June 25,2014---->Binder(IPC),Dalvik ,DEX/ODEX

    1.Binder(IPC) Linux进程之间要能够互相通信,从而共享资源和信息.所以,操作系统内核必须提供进程间的通信机制(IPC,Inter-Process Communication). IPC ...

  6. Android设备管理器 DevicePolicyManager

    设备管理器有个特点,你注册了之后如果不解除注册就会难以卸载带有设备管理器的应用,目前4.3版本仍未提示用户如何卸载,maybe later. 在「设定-安全」你可以看见「设备管理器」,它提供一些高级功 ...

  7. jquery data 选择器 表格序列化serialize()

    data()在元素上存放或者读取数据,返回jquery对象. demo: <div data-obj="{'name':'zhangsan','age':20}">&l ...

  8. 页面跳转问题,多次 push 到新的页面的问题的解决方法

    今日在做一个扫一扫的功能,突然发现多次点击了扫一扫的图片后,造成多次触发轻拍手势,就多次push到新的页面,本想在轻拍手势内对push的进行拦截,但是又觉得如果有好多的地方都要实现对该问题的解决岂不是 ...

  9. Java SE ,Java EE和Java ME 的区别

    JAVA 语言版本  Java SE (J2SE)(Java2 Platform Standard Edition,java平台标准版): 包含标准的 JDK.开发工具.运行时环境和类库.适合开发桌面 ...

  10. Zeppelin推荐

    1.官网 Zeppelin官网,我们可以通过官网了解并获取Zeppelin,Zeppelin既有编译好可以直接运行的文件:也有源码文件. 官网下载网址 http://zeppelin.apache.o ...