Codeforces #396 (Div. 2) D. Mahmoud and a Dictionary (并查集+map
来源:互联网 发布:2016年驾校招生数据 编辑:程序博客网 时间:2024/05/17 21:44
D. Mahmoud and a Dictionary
Description
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 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.
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.
Sample Input
3 3 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate like
Sample Output
YESYESNO1222
题意
很简单的带权并查集TAT
AC代码
#include <bits/stdc++.h>using namespace std;const int MAXN = 1000001;map<string, int> mp;struct node{ int par; int relation;}p[MAXN];void init(int n){ for(int i = 0; i < n; i++) { p[i].par = i; p[i].relation = 0; }}int find(int x){ if(x == p[x].par) return x; int t = p[x].par; p[x].par = find(t); p[x].relation = (p[x].relation+p[t].relation)%2; return p[x].par;}int main(){ int n, m, q; cin>>n>>m>>q; string s1, s2; for(int i = 0; i < n; i++) { cin>>s1; mp[s1] = i; } init(n); int op; for(int i = 0; i < m; i++) { cin>>op>>s1>>s2; op--; int x = mp[s1]; int y = mp[s2]; int xx = find(x); int yy = find(y); if(xx != yy) { puts("YES"); p[yy].par = p[xx].par; p[yy].relation = (op+p[x].relation-p[y].relation); } else { int res = (2+p[x].relation-p[y].relation)%2; if(res == op) puts("YES"); else puts("NO"); } } while(q--) { cin>>s1>>s2; int x = mp[s1]; int y = mp[s2]; int xx = find(x); int yy = find(y); if(xx != yy) puts("3"); else { int res = (2+p[x].relation-p[y].relation)%2; printf("%d\n",res+1); } }return 0;}
- Codeforces #396 (Div. 2) D. Mahmoud and a Dictionary (并查集+map
- Codeforces Round #396 (Div. 2)-D. Mahmoud and a Dictionary(关系并查集)
- Codeforces Round #396 (Div. 2)-D. Mahmoud and a Dictionary(并查集)
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary(并查集)
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary【带权并查集】
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary (并查集)
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
- codeforces 766D Mahmoud and a Dictionary(带权并查集+map查询)
- Codeforces 766D Mahmoud and a Dictionary(并查集+map)
- D. Mahmoud and a Dictionary----带权并查集
- D. Mahmoud and a Dictionary(种类并查集)
- codeforces-766D-Mahmoud and a Dictionary(带权并查集)
- CodeForces - 766D Mahmoud and a Dictionary (并查集)
- Codeforces Round #396 (Div. 2)Mahmoud and a Dictionary
- 【codeforces 766D】Mahmoud and a Dictionary
- codeforces 766D Mahmoud and a Dictionary
- Visual Studio Enterprise 2017 ReferenceManagerPackage的解决方案
- springMvc执行流程简介
- 和为S的两个数字
- 和为S的连续正数序列
- [cnblogs镜像] Unity官方宣传片Adam 播放地址
- Codeforces #396 (Div. 2) D. Mahmoud and a Dictionary (并查集+map
- 平衡二叉树
- [cnblogs镜像]Unity 烘焙的2种方式
- HDU5952(2016沈阳现场赛)
- [cnblogs镜像]Unity 获取坐标函数 坐标转换函数
- 跨平台ssh客户端Xshell使用指南
- 二叉树的深度
- Python入门记——函数
- [cnblogs镜像]Unity 脚本挂载位置