国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary (并查集)

2019-11-08 03:08:13
字體:
來源:轉載
供稿:網友

D. Mahmoud and a Dictionarytime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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: iflove 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 meanslike, 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.

Examplesinput
3 3 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate likeoutput
YESYESNO1222input
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 ihateyououtput
YESYESYESYESNOYES33112題解:看到那些找相鄰關系,上下關系的,不難想到并查集(dsu).

這題的并查集的father數組開兩倍空間。

那么就形成了兩個一樣大小的集合 {1, 2 …… n}, {n+1, ……, n+n} 。

那么對于兩個詞 x, y來說:

如果關系是近義詞,那么就在同一個集合內建樹,也就是fa[find(x)] = find(y),fa[find(x+n)] = find(y+n)。 如果是相反詞,那么就在兩個集合交叉建樹,也就是 fa[find(x)] = find(y + n) ,fa[find(y+n)] = find(x)。

從而可以判斷x和y的關系。

AC代碼:

#include<bits/stdc++.h>using namespace std;const int mod=1e9+7;const int maxn=2e5+7; map<string,int>mp;int fa[2*maxn];int n;void init(){	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(){	int m,q;	string s;	cin>>n>>m>>q;	init();	mp.clear();	for(int i=1;i<=n;i++){		cin>>s;		mp[s] = i;	}	int op;	string a,b;	for(int i=1;i<=m;i++){		cin>>op>>a>>b;		int x=mp[a];		int y=mp[b];		if(op==1){ //同義詞 			if(find(x) == find(y + n)){				puts("NO");			}			else{				puts("YES");				fa[find(y)] = find(x);				fa[find(y + n)] = find(x + n);			}		}		else if(op==2){ //反義詞 			if(find(x) == find(y)){				puts("NO");			}			else{				puts("YES"); 				fa[find(y)] = find(x + n);				fa[find(y + n)] =find(x);			}		}	}		for(int i=1;i<=q;i++){		cin>>a>>b;		int x=mp[a];		int y=mp[b];		int fa=find(x);		int fb=find(y);		if(fa==fb){  //同義詞 			puts("1");		} 		else if(find(x+n)==find(y)){ //反義詞 			puts("2");		}		else puts("3");	}	return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 启东市| 江孜县| 温泉县| 凤台县| 江川县| 石泉县| 康马县| 宾川县| 泰州市| 乌鲁木齐县| 汶川县| 康平县| 博兴县| 巴里| 绥江县| 鱼台县| 应用必备| 乌什县| 吴桥县| 永吉县| 松潘县| 黎城县| 泸州市| 兴义市| 府谷县| 湖口县| 湟中县| 太保市| 五寨县| 团风县| 景东| 祁连县| 岐山县| 肥乡县| 塘沽区| 滨海县| 杂多县| 平安县| 吉木萨尔县| 阿鲁科尔沁旗| 巴里|