Codeforces_766_D_(并查集)

时间:2022-12-22 08:44:05
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 likeopposite 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.

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

 题意:n个单词。m种两两关系要么word1和word2为同义词,要么为反义词;若word1和word2为同义词,word1和word3为反义词,那么word2和word3也为反义词;若word1和word2为反义词,word1和word3也为反义词,那么word2和word3为同义词;若第i个关系使得两个单词既为同一次也为反义词,那么这个关系为错,并将其抛弃,继续判断后面的关系,最后建立好所有关系。q次查询,判断word1和word2的关系。

 

思路:最初想的是使用两个并查集来做,近义词一个,反义词一个;随后发现若两个单词为反义词将其合并的做法不对,抛弃。

然后第二个想法是,在father[i]=i的结点上保存该集合的反义词集合,最后发现是正解。

需将普通并查集代码进行一些改变。

 

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
#define N 100005

struct Node
{
    int fa,ant;
} father[N];

int Find(int x)
{
    if(father[x].fa!=x)
        father[x].fa=Find(father[x].fa);
    return father[x].fa;
}

void Merge(int x,int y)  //改进:合并时,将两同义词的反义词合并
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        father[fx].fa=fy;
        int antfx=father[fx].ant;
        int antfy=father[fy].ant;
        if(antfx!=antfy&&antfx>0&&antfy>0)  
        {
            Merge(antfx,antfy);
            father[fy].ant=Find(antfx);    //更新同义词集合的反义词
            father[Find(antfx)].ant=fy;    //更新反义词集合反义词
        }
        else if(antfx>0||antfy>0)
        {
            if(antfx>0)
            {
                father[fy].ant=antfx;
                father[Find(antfx)].ant=fy;
            }
            else
            {
                father[fy].ant=antfy;
                father[Find(antfx)].ant=fy;
            }
        }
    }
}

map<string,int> index;
int ant[N];

int main()
{
    int fa1[N];
    int n,m,q;

    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            char word[25];
            scanf("%s",word);
            index[word]=i;
            father[i].fa=i;
            father[i].ant=0;
        }
        for(int i=0; i<m; i++)
        {
            int re;
            char word1[25],word2[25];
            scanf("%d%s%s",&re,word1,word2);
if(re==1) //指定关系为同义词 { int f1=Find(index[word1]); int f2=Find(index[word2]); if(f1==f2) //已经是同义词 printf("YES\n"); else if(father[f1].ant!=f2) //两词不为反义词,合并 { Merge(index[word1],index[word2]); printf("YES\n"); } else //出现矛盾 printf("NO\n"); } else if(re==2) //指定关系为反义词 { int f1=Find(index[word1]); int f2=Find(index[word2]); if(f1!=f2) //不是近义词 { if(father[f1].ant>0) //word1有反义词,合并word1的反义词和word2 Merge(father[f1].ant,f2); if(father[f2].ant>0) //word2有反义词,合并word2的反义词和word1 Merge(father[f2].ant,f1); father[Find(f1)].ant=Find(f2); //更新word1所属集合的反义词集合 father[Find(f2)].ant=Find(f1); //更新word2所属集合的反义词集合 printf("YES\n"); } else printf("NO\n"); } } for(int i=0; i<q; i++) { char word1[25],word2[25]; scanf("%s%s",word1,word2); if(Find(index[word1])==Find(index[word2])) printf("1\n"); else if(father[Find(index[word1])].ant==Find(index[word2])) printf("2\n"); else printf("3\n"); } } return 0; }