codeforce 766D Mahmoud and a Dictionary 带权并查集

来源:互联网 发布:php开发实战视频教程 编辑:程序博客网 时间:2024/06/05 16:14

传送门


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 likeand 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 ≤ 1051 ≤ m, q ≤ 105) where nis 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 20characters. 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 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate like
output
YESYESNO1222
input
8 6 5hi welcome hello ihateyou goaway dog cat rat1 hi welcome1 ihateyou goaway2 hello ihateyou2 hi goaway2 hi hello1 hi hellodog catdog hihi helloihateyou goawaywelcome ihateyou
output
YESYESYESYESNOYES33112
题意:给出n个单词,m条定义,每条定义给出两个单词是近义词还是反义词,如果定义不成立就输出NO,否则输出YES,并建立关系。 然后是q次查询,查询给出的两个单词的关系,近义,反义或者未定义关系。

学习带权并查集get√

//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=1e3+10;const int maxx=3e5+100;const double EPS=1e-7;const int mod=10000007;#define mod(x) ((x)%MOD);template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}inline LL Scan(){    int f=1;char C=getchar();LL x=0;    while (C<'0'||C>'9'){if (C=='-')f=-f;C=getchar();}    while (C>='0'&&C<='9'){x=x*10+C-'0';C=getchar();}    x*=f;return x;}//freopen( "in.txt" , "r" , stdin );//freopen( "data.out" , "w" , stdout );//cerr << "run time is " << clock() << endl;map<string,int>mp;int fa[maxx],height[maxx];void init(int n){    for(int i=1;i<=3*n;i++)    {        fa[i]=i;        height[i]=0;    }}int fi(int x){      //路径压缩,把所有元素指向根    return fa[x]==x?x:fi(fa[x]);}void unite(int x, int y){    x = fi(x);    y = fi(y);    if (x == y)        return ;    if (height[x] < height[y])    {        fa[x] = y;    }    else    {        fa[y] = x;        if (height[x] == height[y])            height[x]++;    }}int main(){    int n,m,q,z;    cin>>n>>m>>q;    string s,s1,s2;    for(int i=1;i<=n;i++)    {        cin>>s;        mp[s]=i;    }    init(n);    FOR(1,m,i)    {        cin>>z>>s1>>s2;        int x=mp[s1],y=mp[s2];        if(z==1)        {            if(fi(x+n)==fi(y)||fi(x)==fi(y+n))                printf("NO\n");            else            {                printf("YES\n");                unite(x,y);                unite(x+n,y+n);            }        }        else        {            if(fi(x)==fi(y))                printf("NO\n");            else            {                printf("YES\n");                unite(x,y+n);                unite(x+n,y);            }        }    }    FOR(1,q,i)    {        cin>>s1>>s2;        int x=mp[s1],y=mp[s2];        if(fi(x)==fi(y))            puts("1");        else if(fi(x+n)==fi(y)||fi(x)==fi(y+n))            puts("2");        else puts("3");    }}



阅读全文
0 0