Codeforces 766D 并查集
来源:互联网 发布:linux安装tomcat8 编辑:程序博客网 时间:2024/05/21 23: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: 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.
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.
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.
3 3 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate like
YESYESNO1222
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
YESYESYESYESNOYES33112
题意: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的关系是否等于xelse 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;}
- Codeforces 766D 并查集
- codeforces 766D 并查集进阶
- Codeforces 366D 贪心+并查集
- Codeforces 371D. Vessels【并查集】
- codeforces 366D 并查集
- codeforces 593D(并查集 + LCA)
- codeforces 691D 并查集
- [Codeforces 325 D. Reclamation]并查集
- Codeforces 776D 并查集
- CodeForces 25D(并查集)
- CF Round#396D (Div. 2)(Codeforces 766D) 简单并查集+map
- Codeforces 292D Connected Components (并查集)
- Codeforces Round #218 (Div. 2)---D. Vessels(并查集)
- 【并查集分块】Codeforces 475D CGCDSSQ
- CodeForces 151D Quantity of Strings(并查集)
- codeforces 593 D. Happy Tree Party (LCA + 并查集)
- codeforces 593D Happy Tree Party Lca+并查集
- Codeforces Round #334 D. Moodular Arithmetic(并查集)
- 【winter 2 】B. Om Nom and Dark Park 怪兽安路灯
- 创建JSTL标签库
- 努比亚Z11 mini打开USB调试模式与工程端口的方法
- 剑指offer-和为S的两个数组-两根指针
- 杨氏矩阵
- Codeforces 766D 并查集
- DataGridView中将column与数据库表对应的属性
- 389. Find the Difference | 找字符串中的不同
- HDU1856:More is better(并查集)
- 图像卷积与滤波的一些知识点
- 记录一个breed服务器地址
- 洛谷 1449——后缀表达式(线性数据结构)
- 第二章 Spring MVC入门
- 函数注册与回调实例