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.
InputThe 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.
OutputFirst, 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.
Examplesinput3 3 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate likeoutputYESYESNO1222input8 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 ihateyououtputYESYESYESYESNOYES33112題解:看到那些找相鄰關系,上下關系的,不難想到并查集(dsu).這題的并查集的father數組開兩倍空間。
那么就形成了兩個一樣大小的集合 {1, 2 …… n}, {n+1, ……, n+n} 。
那么對于兩個詞 x, y來說:
如果關系是近義詞,那么就在同一個集合內建樹,也就是fa[find(x)] = find(y),fa[find(x+n)] = find(y+n)。 如果是相反詞,那么就在兩個集合交叉建樹,也就是 fa[find(x)] = find(y + n) ,fa[find(y+n)] = find(x)。
從而可以判斷x和y的關系。
AC代碼:
#include<bits/stdc++.h>using namespace std;const int mod=1e9+7;const int maxn=2e5+7; map<string,int>mp;int fa[2*maxn];int n;void init(){ for(int i=0;i<=n;i++){ fa[i]=i; fa[i+n]=i+n; }}int find(int x){ return fa[x]==x ? fa[x] : fa[x] = find(fa[x]);}int main(){ int m,q; string s; cin>>n>>m>>q; init(); mp.clear(); for(int i=1;i<=n;i++){ cin>>s; mp[s] = i; } int op; string a,b; for(int i=1;i<=m;i++){ cin>>op>>a>>b; int x=mp[a]; int y=mp[b]; if(op==1){ //同義詞 if(find(x) == find(y + n)){ puts("NO"); } else{ puts("YES"); fa[find(y)] = find(x); fa[find(y + n)] = find(x + n); } } else if(op==2){ //反義詞 if(find(x) == find(y)){ puts("NO"); } else{ puts("YES"); fa[find(y)] = find(x + n); fa[find(y + n)] =find(x); } } } for(int i=1;i<=q;i++){ cin>>a>>b; int x=mp[a]; int y=mp[b]; int fa=find(x); int fb=find(y); if(fa==fb){ //同義詞 puts("1"); } else if(find(x+n)==find(y)){ //反義詞 puts("2"); } else puts("3"); } return 0;}
新聞熱點
疑難解答