Codeforces 766D 并查集

来源:互联网 发布:linux安装tomcat8 编辑:程序博客网 时间:2024/05/21 23:53

Mahmoud and a Dictionary
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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: iflove 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 meanslike, 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 ≤ 1051 ≤ 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 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate like
output
YESYESNO1222
input
8 6 5hi welcome hello ihateyou goaway dog cat rat1 hi welcome1 ihateyou goaway2 hello ihateyou2 hi goaway2 hi hello1 hi hellodog catdog hihi helloihateyou goawaywelcome ihateyou
output
YESYESYESYESNOYES33112

题意:n个单词,m个条件,q次询问,每个条件是  a x y ,如果a是1,代表xy是同义词,否则是反义词。

判断这个条件是否与以前的冲突,如果不冲突则列为已知条件并输出yes,否则输出no。

每次询问,询问两个单词的关系,如果没有关系,输出3。


题解:带位移的并查集。

先将字符串扔到map里。

在普通并查集的基础上加个relation,0代表与父节点为同义词,1代表为反义词。

然后处理压缩路径时,则a与新根节点的关系更新就为,a和现在结点的关系+这个结点与根结点的关系 mod 2

举个栗子  c-a-b  c与a是同义词  0  a与b为同义词  0+0 mod 2 = 0   所以c和b是同义词  如果a与b是反义词  0+1 mod 2 = 1  c与b是反义词 

相当于数学上的向量  ac=ab+bc

物理上的  人对船的速度=人对地的速度+地对船的速度

注意细节

ps:POJ 1182 这里讲的更详细

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>using namespace std;string c,s;map<string,int>sp;struct node{int parent,relation;}e[200005];int find(int x){if(e[x].parent==x)return x;int temp=e[x].parent;//路径压缩e[x].parent=find(temp);e[x].relation=(e[x].relation+e[temp].relation)%2;//关系更新return e[x].parent;}    int main(){int x,i,j,n,m,q;scanf("%d%d%d",&n,&m,&q);for(i=1;i<=n;i++){cin>>c;sp[c]=i;e[i].parent=i;e[i].relation=0;//自己和自己就是同义词}for(i=1;i<=m;i++){scanf("%d",&x);x--;//这里x--是因为下面定义0是同义词  1是反义词cin>>c>>s;int r1=sp[c],r2=sp[s];int root1=find(r1);int root2=find(r2);if(root1!=root2){printf("YES\n");e[root1].parent=r2;e[root1].relation=(e[r1].relation+x)%2;r1的根结点和r2的关系为r1根节点对r1的关系+r1对r2的关系 mod 2}else{if(x==0){if(e[r1].relation!=e[r2].relation)printf("NO\n");//如果x y是同义词  判断r1与根节点的关系是否等于r2与根节点的关系else printf("YES\n");}else{if((e[r1].relation+e[r2].relation)%2!=x)printf("NO\n");//算出r1与r2的关系是否等于xelse printf("YES\n");}}}for(i=1;i<=q;i++){cin>>c>>s;int r1=sp[c],r2=sp[s];int root1=find(r1),root2=find(r2);if(root1!=root2)printf("3\n");//如果r1和r2没关系else printf("%d\n",(e[r1].relation+e[r2].relation)%2+1);//+1是因为题中1是同义词  2是 反义词}return 0;}


0 0