D. Mahmoud and a Dictionary 题解

http://codeforces.com/contest/766/problem/D

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 nm and q (2 ≤ n ≤ 105, 1 ≤ m, q ≤ 105) where n is the number of words in the dictionary, mis 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


题意: 就是给定一个文本, 然后每次输入两个词以及两个词之间的关系(1表示为近义词,2表示为相反词),在输入的过程中如果出现关系矛盾,那么这次的输入无效并且输出NO 否则就输出YES
然后就是询问两个词之间的关系, 同样两个词近义词输出1, 相反词输出2,没有任何关系输出3

思路:并查集。对于并查集的father数组开两倍空间, 那么就形成了两个一样的集合 {1, 2 …… n} {n+1, ……, n+n} 对于两个词 x, y 如果关系是近义词,那么就在同一个集合内建树 也就是fa[find(x)] = find(y)
fa[find(x+n)] = find(y+n) 如果是相反词,那么就交叉建树,也就是 fa[find(b)] = find(a + n) fa[find(b+n)] = find(a)

#include <iostream>
#include <string.h>
#include <algorithm>
#include <map>
#include <string>
using namespace std;

const int maxn = 2e5+2;
int fa[maxn+maxn];
map<string, int>HASH;

void init(int n) {
    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() {
    ios::sync_with_stdio(false);
    int n, m, q; cin >> n >> m >> q;
    init(n);
    for (int i = 1; i <= n; ++i) {
        string s; cin >> s;
        HASH[s] = i;
    }
    while (m--) {
        int op; string a, b; cin >> op >> a >> b;
        int ha = HASH[a], hb = HASH[b];
        if (op == 1) {
            if (find(ha) == find(hb + n)) {
                cout << "NO" << endl;
            }
            else {
                cout << "YES" << endl;
                fa[find(hb)] = find(ha);
                fa[find(hb + n)] = find(ha + n);
            }
        }
        else {
            if (find(ha) == find(hb)) {
                cout << "NO" << endl;
            }
            else {
                cout << "YES" << endl;
                fa[find(hb)] = find(ha + n);
                fa[find(hb + n)] = find(ha);
            }
        }
    } 
    while (q--) {
        string a, b; cin >> a >> b;
        int ha = HASH[a], hb = HASH[b];
        int fa = find(ha), fb = find(hb);
        if (fa == fb) {
            cout << 1 << endl;
        }
        else if (find(ha + n) == find(hb)) {
            cout << 2 << endl;
        }
        else {
            cout << 3 << endl;
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/boson-is-god/p/6408643.html