codeforces - 766D【Double 并查集】

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

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

 题意:

规定两个字符串有两种关系,相同或者相反,相反的相反就是相等。

现在一对一对给定两个字符串以及他们的关系,如果他们满足这个关系,那么去合并并输出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 }
View Code

 

posted on 2017-02-08 20:07  Jstyle  阅读(332)  评论(0编辑  收藏  举报

导航