Mahmoud and a Dictionary CodeForces - 766D

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, 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.

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
题目大意:给出n个词,m个单词之间的关系(关系包括近义词和反义词),你需要根据已有的单词之间的关系判断所给的这m行关系正确与否(正确输出YES反之输出NO),接下来让你输出接下来q行,询问的单词之间的关系(同义词、反义词、没关系)。

分析:首先,能够很容易想到本题考查的是并查集,那么接下来我们要考虑本题怎么建立并查集。本题与普通的并查集不同的地方在于 节点与节点之间存在存在确定的关系,因此我们在建立常规并查集的同时(这里建树比较随意,让给定的关系中一个为父亲节点一个是儿子节点即可),需要建立节点之间关系的并查集。这个关系怎么确立呢,我们需要找到所有子节点与祖先的关系,这个关系怎么找呢。这里,我们把近义词设为0,反义词设为1,(这个把关系之间设成0、1 其实是种类并查集的套路,如果不了解的:传送门)我们知道的是相邻的两节点的关系(比如 父亲节点和子节点,爷爷节点和父亲节点),那么我们如何得到儿子节点和爷爷节点的关系呢,列举一下会发现,与爷爷节点的关系=(父亲节点+儿子节点)%2。 这样我们就找到在同一个树内,各节点的关系。下面还有一个问题,当两个节点本属于两个树,现在要建立关系,也就是要建立那个树之间祖先的关系那么怎么找到这个关系呢,下面我们设两节点为x、y,其祖先节点为fx、fy,现在我们知道的关系是x~y,x~fx,y~fy,要求的关系是fy~fx.那么fy~fx=(fy~y)+(y~x)+(x~fx)。   下面上代码:

#include <iostream>
#include <cstring>
#include <map>
using namespace std;
int f[100010] , root[100010] , n , m;
void init()
{
    for(int i = 1 ; i <= n ; i++)
    {
        f[i] = i ;
        root[i] = 0;
    }
}
int find(int x)//找到各点的跟节点,并把各压缩到跟节点上
{
    if(x == f[x])
        return x;
    int tmp = find(f[x]);
    root[x] = (root[x] + root[f[x]]) % 2;//建立各点与跟节点的关系
    return f[x] = tmp;
}
string s , s1 , s2;
map<string , int>mp;
int main()
{
    int x , y , num , q;
    cin >> n >> m >> q;
    init();
    for(int i = 0 ; i < n ; i++)
    {
        cin >> s;
        mp[s] = i + 1;
    }
    for(int i = 1 ; i <= m ; i++)
    {
        cin >> num >> s1 >> s2;
        x = mp[s1] ; y = mp[s2];
        int t1 = find(x) , t2 = find(y);
        if(num == 1)
        {
            if(t1 == t2)
            {
                if(root[x] != root[y])//若两个的关系不相等,则证明为反义词,则所给题设为假
                {
                    cout << "NO" << endl;
                }
                else
                {
                    cout << "YES" << endl;
                }
            }
            else
            {
                cout << "YES" << endl;
                f[t1] = t2;
                root[t1] = (root[y] + root[x]+0) % 2;//建立两个树的祖先节点之间的关系
            }
        }
        else
        {
            if(t1 == t2)
            {
                if(root[x] == root[y])//若两个的关系相等,则证明为近义词,则所给题设为假

                {
                    cout << "NO" << endl;
                }
                else
                {
                    cout << "YES" << endl;
                }
            }
            else
            {
                cout << "YES" << endl;
                f[t1] = t2;
                root[t1] = (root[y] + root[x] + 1) % 2;//建立两个树的祖先节点之间的关系
            }
        }
    }
    while(q--)
    {
        cin >> s1 >> s2;
        x = mp[s1] ; y = mp[s2];
        int t1 = find(x) , t2 = find(y);
        if(t1 == t2)
        {
            if(root[x] == root[y])
            {
                cout << 1 << endl;
            }
            else
            {
                cout << 2 << endl;
            }
        }
        else
        {
            cout << 3 << endl;
        }
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值