Codeforces 766D 并查集

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: 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 ≤ 1051 ≤ 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个条件,q次询问,每个条件是  a x y ,如果a是1,代表xy是同义词,否则是反义词。

判断这个条件是否与以前的冲突,如果不冲突则列为已知条件并输出yes,否则输出no。

每次询问,询问两个单词的关系,如果没有关系,输出3。


题解:带位移的并查集。

先将字符串扔到map里。

在普通并查集的基础上加个relation,0代表与父节点为同义词,1代表为反义词。

然后处理压缩路径时,则a与新根节点的关系更新就为,a和现在结点的关系+这个结点与根结点的关系 mod 2

举个栗子  c-a-b  c与a是同义词  0  a与b为同义词  0+0 mod 2 = 0   所以c和b是同义词  如果a与b是反义词  0+1 mod 2 = 1  c与b是反义词 

相当于数学上的向量  ac=ab+bc

物理上的  人对船的速度=人对地的速度+地对船的速度

注意细节

ps:POJ 1182 这里讲的更详细

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
string c,s;
map<string,int>sp;
struct node{
	int parent,relation;
}e[200005];
int find(int x){
	if(e[x].parent==x)return x;
	int temp=e[x].parent;//路径压缩
	e[x].parent=find(temp);
	e[x].relation=(e[x].relation+e[temp].relation)%2;//关系更新
	return e[x].parent;
}    
int main(){
	int x,i,j,n,m,q;
	scanf("%d%d%d",&n,&m,&q);
	for(i=1;i<=n;i++){
		cin>>c;
		sp[c]=i;
		e[i].parent=i;
		e[i].relation=0;//自己和自己就是同义词
	}
	for(i=1;i<=m;i++){
		scanf("%d",&x);
		x--;//这里x--是因为下面定义0是同义词  1是反义词
		cin>>c>>s;
		int r1=sp[c],r2=sp[s];
		int root1=find(r1);
		int root2=find(r2);
		if(root1!=root2){
			printf("YES\n");
			e[root1].parent=r2;
			e[root1].relation=(e[r1].relation+x)%2;r1的根结点和r2的关系为r1根节点对r1的关系+r1对r2的关系 mod 2
		}
		else{
			if(x==0){
				if(e[r1].relation!=e[r2].relation)printf("NO\n");//如果x y是同义词  判断r1与根节点的关系是否等于r2与根节点的关系
				else printf("YES\n");
			}
			else{
				if((e[r1].relation+e[r2].relation)%2!=x)printf("NO\n");//算出r1与r2的关系是否等于x
				else printf("YES\n");
			}
		}
	}
	for(i=1;i<=q;i++){
		cin>>c>>s;
		int r1=sp[c],r2=sp[s];
		int root1=find(r1),root2=find(r2);
		if(root1!=root2)printf("3\n");//如果r1和r2没关系
		else printf("%d\n",(e[r1].relation+e[r2].relation)%2+1);//+1是因为题中1是同义词  2是 反义词
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值