Codeforces Round #396 D (带权并查集)

来源:互联网 发布:mac预览图片 拖动 编辑:程序博客网 时间:2024/06/04 19:16

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.

题目意思:
给你n个字符串,再给你m个字符的关系 1代表两者相同 0代表两者相反。如果两个关系已经确定但又出现了和这个关系矛盾的条件就把这个条件省略掉。再出q个提问,求提问中两者的关系,1相同,2相反,3不知道。
题目解析:
带权并查集,然后的话我发现string这个东西是真的好用,尤其是多个字符串中。string可以用sort排序,还可以用lower_bound()函数,美滋滋。如果用char的话。。就自己写快速排序和二分把。(刚开始就是这样写的,还错了!欸嘿?)

#include<bits/stdc++.h>using namespace std;const int MAX_N=101000;string name[MAX_N];int fat[MAX_N];int ran[MAX_N];void init(int n){    for(int i=1;i<=n;i++)    {        fat[i]=i;        ran[i]=0; //0同义词 1反义词    }    sort(name+1,name+n+1);}int cheak(string pname,int n){    return (lower_bound(name,name+n,pname)-name);}int Find(int x) //寻找祖宗{    if(x==fat[x])    {        return x;    }    int y = Find(fat[x]);    ran[x] = (ran[x] + ran[fat[x]])%2;    return fat[x]=y; //路径压缩}bool mix(int t,int x,int y){    int res;    int fx = Find(x);    int fy = Find(y);    if(fx==fy)    {        res=(ran[x]+ran[y])%2;        if(res==t)        {            return true;        }        else return false;    }    fat[fx] = fy;    res=(t+ran[x]+ran[y])%2;    ran[fx] = res;    return true;}int main(){    int n,m,q;    scanf("%d%d%d",&n,&m,&q);    for(int i=1;i<=n;i++)    {        cin>>name[i];    }    init(n);    for(int i=0;i<m;i++)    {        int t,x,y;        string temp;        scanf("%d",&t);        cin>>temp;        x=cheak(temp,n);        cin>>temp;        y=cheak(temp,n);        bool ans=mix(t-1,x,y);        if(ans) printf("YES\n");        else printf("NO\n");    }    for(int i=0;i<q;i++)    {        int x,y;        string temp;        cin>>temp;        x=cheak(temp,n);        cin>>temp;        y=cheak(temp,n);        if(Find(x)==Find(y))        {            int res = (ran[x]+ran[y])%2+1;            printf("%d\n",res);        }        else printf("3\n");    }    return 0;}
阅读全文
0 0
原创粉丝点击