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.ios
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.app
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.ide
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.spa
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.code
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.orm
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.blog
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.three
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.ci
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
題意:
規定兩個字符串有兩種關係,相同或者相反,相反的相反就是相等。
如今一對一對給定兩個字符串以及他們的關係,若是他們知足這個關係,那麼去合併並輸出YES,不然忽略並輸出NO;
最後在給定q組字符串,若是知足相同輸出1,相反輸出2,不要緊輸出3;
題解:
秒出並查集解法,可是細想一下很難處理。那麼咱們能夠創建兩個fa數組來維護這樣的關係,分別是某個字符串的fa以及該字符串敵對關係的_fa;
這樣子就很好維護了,相同關係知足: y不在x的敵對圈子裏。不一樣知足: x != y;
初始化全部的 _fa = 0, 每次分別去維護 fa 和 _fa 數組,這裏能夠好好想一下怎麼維護,具體看代碼;
代碼:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <bitset> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <cmath> 10 #include <list> 11 #include <set> 12 #include <map> 13 #define rep(i,a,b) for(int i = a;i <= b;++ i) 14 #define per(i,a,b) for(int i = a;i >= b;-- i) 15 #define mem(a,b) memset((a),(b),sizeof((a))) 16 #define FIN freopen("in.txt","r",stdin) 17 #define FOUT freopen("out.txt","w",stdout) 18 #define IO ios_base::sync_with_stdio(0),cin.tie(0) 19 #define mid ((l+r)>>1) 20 #define ls (id<<1) 21 #define rs ((id<<1)|1) 22 #define N 100005 23 #define INF 0x3f3f3f3f 24 #define INFF ((1LL<<62)-1) 25 using namespace std; 26 typedef long long LL; 27 typedef pair<int, int> PIR; 28 const double eps = 1e-8; 29 30 int n, m, q, op, _fa[N], f[N]; 31 string s1, s2, s[N]; 32 map <string, int> ID; 33 int getfa(int x){ 34 return f[x] == x ? x : f[x] = getfa(f[x]); 35 } 36 int main() 37 {IO; 38 FIN; 39 while(cin >> n >> m >> q){ 40 rep(i, 0, N-1) f[i] = i; 41 ID.clear(); 42 mem(_fa, 0); 43 44 rep(i, 1, n){ 45 cin >> s[i]; 46 ID[s[i]] = i; 47 } 48 rep(i, 1, m){ 49 cin >> op >> s1 >> s2; 50 51 int x = getfa(ID[s1]), y = getfa(ID[s2]); 52 int _x = getfa(_fa[x]), _y = getfa(_fa[y]); 53 if(op == 1){ 54 if(_x == y) cout << "NO" << endl; 55 else{ 56 cout << "YES" << endl; 57 f[x] = y; 58 if(_x && _y) f[_x] = _y; //維護字符串圈 59 if(!_y) _fa[y] = _x; //維護敵對圈 60 } 61 } 62 else{ 63 if(x == y) cout << "NO" << endl; 64 else{ 65 cout << "YES" << endl; 66 if(_x) f[y] = _x; //維護字符串和敵對圈,分狀況討論 67 else 68 _fa[x] = y; 69 if(_y) f[x] = _y; 70 else 71 _fa[y] = x; 72 } 73 } 74 } 75 rep(i, 1, q){ 76 cin >> s1 >> s2; 77 int x = getfa(ID[s1]), y = getfa(ID[s2]); 78 int _x = getfa(_fa[x]), _y = getfa(_fa[y]); 79 if(x == y) cout << 1 << endl; 80 else{ 81 if(_x == y || x == _y) cout << 2 << endl; 82 else 83 cout << 3 << endl; 84 } 85 } 86 } 87 return 0; 88 }