传送门

D. 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: 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 likeand 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 nis 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 20characters. 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条定义,每条定义给出两个单词是近义词还是反义词,如果定义不成立就输出NO,否则输出YES,并建立关系。 然后是q次查询,查询给出的两个单词的关系,近义,反义或者未定义关系。

学习带权并查集get√

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e3+10;
const int maxx=3e5+100;
const double EPS=1e-7;
const int mod=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline LL Scan()
{int f=1;char C=getchar();LL x=0;while (C<'0'||C>'9'){if (C=='-')f=-f;C=getchar();}while (C>='0'&&C<='9'){x=x*10+C-'0';C=getchar();}x*=f;return x;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;map<string,int>mp;
int fa[maxx],height[maxx];
void init(int n)
{for(int i=1;i<=3*n;i++){fa[i]=i;height[i]=0;}
}
int fi(int x)
{      //路径压缩,把所有元素指向根return fa[x]==x?x:fi(fa[x]);}
void unite(int x, int y)
{x = fi(x);y = fi(y);if (x == y)return ;if (height[x] < height[y]){fa[x] = y;}else{fa[y] = x;if (height[x] == height[y])height[x]++;}
}
int main()
{int n,m,q,z;cin>>n>>m>>q;string s,s1,s2;for(int i=1;i<=n;i++){cin>>s;mp[s]=i;}init(n);FOR(1,m,i){cin>>z>>s1>>s2;int x=mp[s1],y=mp[s2];if(z==1){if(fi(x+n)==fi(y)||fi(x)==fi(y+n))printf("NO\n");else{printf("YES\n");unite(x,y);unite(x+n,y+n);}}else{if(fi(x)==fi(y))printf("NO\n");else{printf("YES\n");unite(x,y+n);unite(x+n,y);}}}FOR(1,q,i){cin>>s1>>s2;int x=mp[s1],y=mp[s2];if(fi(x)==fi(y))puts("1");else if(fi(x+n)==fi(y)||fi(x)==fi(y+n))puts("2");else puts("3");}
}

codeforce 766D Mahmoud and a Dictionary 带权并查集相关推荐

  1. 2017乌鲁木齐区域赛I(带权并查集)

    #include<bits/stdc++.h> using namespace std; int f[200010];//代表元 long long rl[200010];//记rl[i] ...

  2. BZOJ 2303 方格染色(带权并查集)

    要使得每个2*2的矩形有奇数个红色,如果我们把红色记为1,蓝色记为0,那么我们得到了这2*2的矩形里的数字异或和为1. 对于每个方格则有a(i,j)^a(i-1,j)^a(i,j-1)^a(i-1,j ...

  3. POJ1703带权并查集(距离或者异或)

    题意:       有两个黑社会帮派,有n个人,他们肯定属于两个帮派中的一个,然后有两种操作 1 D a b 给出a b 两个人不属于同一个帮派 2 A a b 问a b 两个人关系 输出 同一个帮派 ...

  4. POJ1988(带权并查集,搬砖块)

    题意:        可以这样理解,有n快方形积木,一开始都是单独的放到哪,然后有两种操作 1 M a b 把a所在的那一堆落到b所在那一堆的上面(一开始自己是一堆) 2 C a 问a下面有多少个积木 ...

  5. LA3027简单带权并查集

    题意:       有n个点,一开始大家都是独立的点,然后给出一些关系,a,b表示a是b的父亲节点,距离是abs(a-b)%1000,然后有一些询问,每次询问一个节点a到父亲节点的距离是多少? 思路: ...

  6. hdu3234 带权并查集(XOR)

    题意:       给你n个未知的正整数,有三总操作       I P V            P的值是V       I P Q V          P XOR Q = V       Q K ...

  7. hdu4829 带权并查集(题目不错)

    题意: Information Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. poj1182 and 携程预赛2第一题 带权并查集

    题意:       动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.  现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底 ...

  9. How Many Answers Are Wrong HDU - 3038(带权并查集经典题,满满的都是注释)

    How Many Answers Are Wrong HDU - 3038  点击打开链接 题意:现在有n个数(你并不知道这n个数是什么),m次查询,每次查询给出u,v,w.表示从第u个数到第v个数的 ...

最新文章

  1. 《百面机器学习》---AI算法工程师求职必备“面经”
  2. 海啸级后浪!“天才少年”曹原再次连发2篇Nature!
  3. matlab从flove,Matlab玩出新高度,变身表白女友神器_善良995的博客-CSDN博客
  4. Crawler:基于urllib库获取cn-proxy代理的IP地址
  5. linux加密格式化吗,linux环境下给文件加密/解密的方法
  6. 生物信息之ME, HMM, MEMM, CRF
  7. python二进制转十进制代码_python二进制转十六进制代码
  8. python3.14_leetcode-python3-14. 最长公共前缀
  9. 中国名人书画展由世界全媒体联盟中国区及广西明星影视文化传媒有限公司联合举办
  10. 前端职业规划 - 写给年轻的前端韭菜们
  11. 国家档案局发布第13号令《机关档案管理规定》
  12. 《少年pi》:每个人心中都有一只孟…
  13. 可视化工具VisIt安装使用教程(Windows)
  14. Python Ajax爬取微博个人博客数据
  15. 微软雅黑html中怎么写,网页中使用微软雅黑字体(css调用微软雅黑)
  16. Linux C/C++ 中锁的使用总结
  17. 绕过网站安全狗,方法 适合所有
  18. HT1622/HT1622G中英文资料
  19. Microsoft Edge使用方法和心得
  20. CNN卷积神经网络学习过程(权值更新)

热门文章

  1. Java虚拟机中STW(stop the world)是什么意思
  2. 美国股市暴跌,中国路在何方
  3. QuickMark: ElasticSearch curl command
  4. 【实用工具】技术人如何写好英文论文?
  5. Qt 5.15 安装步骤
  6. 【实例分割|Mask2Former】 解决模型推理预测的代码中存在的一些问题
  7. 评测三款最流行的txt阅读器(Mac适用)
  8. 【codeforces 794A】Bank Robbery
  9. 红队笔记之权限维持技术要点总结
  10. 量化投资学习——股指期货ETF套利