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

来源:互联网 发布:mac优化 编辑:程序博客网 时间:2024/04/30 07:38

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 ≤ 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.

Example
Input
3 3 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate like
Output
YESYESNO1222
Input
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
Output
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;} 

0 0
原创粉丝点击