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

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

CodeForces - 766D Mahmoud and a Dictionary (并查集)

2019-11-08 18:39:53
字體:
來源:轉載
供稿:網友

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.

ExampleInput
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
YESYESYESYESNOYES3311

2

題目大意:一些小寫字母拼成的單詞,有一些是相同意思,有一些是相反意思的,然后現在讓你判斷這些單詞

的意思劃分是否符合規矩,以免造成反義的反義還是它的反義類似這種情況。所以讓你判斷,最后讓給你兩個

單詞,讓你判斷他們的關系,是近義還是反義,還是沒有關系。

題目分析:這是一個并查集的問題,不過對于我來說,因為涉及到字符串的問題,所以感覺有些吃力吧,后來在別人的博客里面看到了map這個玩意,才減輕了很大的

負擔,map在另外一篇轉載的博客里面有詳細的介紹,而且這種輸入很多字符串的問題,我發現用cin的確很方便,不用去糾結空格和換行的問題,所以轉為cin了。但

是因為有時間限制的緣故,又是在別人的博客里,看到了那個std::ios::sync_with_stdio(false);其實就是減少cin時間的東東,又學到了新技能,很不錯。

最后就是這個題目的并查集了。因為用map<string,int>mp,所以每個字符都匹配唯一的數字,這樣就可以把字符轉化為數字了,很方便。然后首先是那個find函數,我居然

又發現,我一直用的那個find函數書寫方式在這個題目里面會超時,而且蜜汁超時,實在不知道是怎么回事,好氣呀。然后就是有個at數組,是用來存儲相反意思的,每次

輸入兩個字符串,都找到他們的老大,然后再從他們的老大,找到他們的對立老大,然后通過這些比較判斷他們是不是相同意思或者相反意思或者沒有關系。然后剛開始

輸入他們的關系并做判定的時候,由于最開始at數組和a數組都是空的,所以需要邊比較邊更新他們的數組值。其實還是感覺有點復雜,特別是預處理這塊。

#include<bits/stdc++.h>  using namespace std;#define maxn 100005map<string,int>mp;int n,m,q;int pre[maxn],a[maxn],at[maxn];int find(int u)  {      if (!u) return 0;      if (pre[u]!=u) pre[u]=find(pre[u]);      return pre[u];  }  int main(){    std::ios::sync_with_stdio(false);	    cin>>n>>m>>q;		memset(a,0,sizeof(a));		memset(at,0,sizeof(at));		memset(pre,0,sizeof(pre));		for(int i=1;i<=n;i++){			string name;			cin>>name;			mp[name]=i;			pre[i]=i;		}		while(m--){			int t,u,u1,v1,v;			string a,b;			cin>>t>>a>>b;	        u=find(mp[a]);v=find(mp[b]);	        u1=find(at[u]);v1=find(at[v]);	        if(t == 1){	        	if(u1 == v)	        	    printf("NO/n");	        	else {				    printf("YES/n");				    pre[u]=v;				    if(u1&&v1) pre[u1]=v1;				    if(!v1)  at[v]=u1; 				}			}			else {				if(u == v)				    printf("NO/n");				else{					printf("YES/n");					if(u1)  pre[v]=u1;					else at[u]=v;					if(v1)  pre[u]=v1;					else at[v]=u;			    	}				  		    	}		}		while(q--){			string a,b;			cin>>a>>b;			int u=find(mp[a]);			int v=find(mp[b]);			int u1=find(at[u]);			int v1=find(at[v]);			if(u == v)			  printf("1/n");			else if(u == v1 || v == u1)			  printf("2/n");			else printf("3/n");		}	return 0;} 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德令哈市| 石屏县| 克拉玛依市| 鹿泉市| 山阴县| 石屏县| 磐安县| 若尔盖县| 定兴县| 西平县| 博野县| 贵定县| 武功县| 邢台县| 定日县| 昭觉县| 新乡市| 方山县| 都匀市| 河北区| 华安县| 玉田县| 洛阳市| 余江县| 民勤县| 普兰店市| 湘乡市| 墨脱县| 汝城县| 南充市| 宜良县| 双牌县| 辽阳市| 沙田区| 曲阜市| 厦门市| 大关县| 井研县| 竹北市| 健康| 霍林郭勒市|