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

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
题目链接:http://codeforces.com/contest/766/problem/D
题意:有n个串,m个关系,q个询问。告诉两个串是同义还是反义。与之前的关系矛盾输出NO,否则关系成立。输出每次询问的两个串
之间的关系,同义输出1,反义输出2,不确定输出3。
思路:并查集。2*i表示这个串,2*i-1表示这个串的反义。如果i和j(i和j表示串的标号)同义,则2*i与2*j,2*i-1与2*j-1同义。如果i和j反义,则2*i与
2*j-1,2*j与2*i-1同义。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int MAXN=1e6+;
int pa[MAXN];
map<string,int>sign;
int findset(int x)
{
return pa[x]!=x?pa[x]=findset(pa[x]):x;
}
void uni(int s,int e)
{
int pas=findset(s),pae=findset(e);
if(pas!=pae) pa[pas]=pae;
}
int main()
{
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
for(int i=; i<=*n; i++) pa[i]=i;
for(int i=; i<=n; i++)
{
string ch;
cin>>ch;
sign[ch]=i;
}
string s,e,pas,pae;
for(int i=; i<m; i++)
{
int t;
int ans=;
scanf("%d",&t);
cin>>s>>e;
int id1=sign[s],id2=sign[e];
if(t==)
{
if(findset(*id1)==findset(*id2-)) cout<<"NO"<<endl;
else if(findset(*id2)==findset(*id1-)) cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
uni(*id1,*id2);
uni(*id1-,*id2-);
}
}
else
{
if(findset(*id1)==findset(*id2)) cout<<"NO"<<endl;
else if(findset(*id1-)==findset(*id2-)) cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
uni(*id1,*id2-);
uni(*id1-,*id2);
}
}
}
for(int i=; i<q; i++)
{
cin>>s>>e;
int id1=sign[s],id2=sign[e];
if(findset(*id1)==findset(*id2)) cout<<<<endl;
else if(findset(*id1-)==findset(*id2-)) cout<<<<endl;
else if(findset(*id1)==findset(*id2-)) cout<<<<endl;
else if(findset(*id2)==findset(*id1)) cout<<<<endl;
else cout<<<<endl;
}
return ;
}

并查集



Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分的更多相关文章

  1. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  2. Codeforces 766D Mahmoud and a Dictionary 2017-02-21 14:03 107人阅读 评论(0) 收藏

    D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...

  3. CodeForces 766D Mahmoud and a Dictionary

    并查集. 将每一个物品拆成两个,两个意义相反,然后并查集即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #i ...

  4. CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合

    题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反 ...

  5. Codefroces 766D Mahmoud and a Dictionary

    D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...

  6. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  7. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  8. CodeForces Roads not only in Berland(并查集)

    H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  9. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

随机推荐

  1. Eclipse下支持编写HTML/JS/CSS/JSP页面的自动提示

    地址:https://blog.csdn.net/AinUser/article/details/64904339 使用eclipse自带的插件,无需另外安装插件,具体步骤如下 1.打开eclipse ...

  2. 文件系统的描述信息-/etc/fstab

    /etc/fstab文件包含众多文件系统的描述信息.文件中每一行为一个文件系统的描述,每行的选项之间通过tab分隔,#开头的行会被转换为注释,空白行会被忽略./etc/fstab文件中的设备顺序很重要 ...

  3. LoadRunner 安装汉化后的一些问题

    我装好LoadRunner11后,按照下面的方法破解: 1.  把loadrunner相关程序全部退出: 2.  用LR8.0中的mlr5lprg.dll.lm70.dll覆盖LR9.5安装目录下“b ...

  4. 安装FrameWork后重新注册IIS

    IIS和.netfw4.0安装顺序是从前到后,如果不小心颠倒了,无所谓. 打开程序-运行-cmd:输入一下命令重新注册IIS C:\WINDOWS\Microsoft.NET\Framework\v4 ...

  5. linux命令--xargs的使用

    xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具. xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据. xargs 也可以将单行或多 ...

  6. Mssql合并查询结果

    在项目开发中,有时会碰到将列记录合并为一行的情况,例如根据地区将人员姓名合并,或根据拼音首字母合并城市等,下面就以根据地区将人员姓名合并为例,详细讲一下合并的方法. 首先,先建一个表,并添加一些数据, ...

  7. mysql脚本转h2

    注意事项:转的时候需要 脚本中不能包含utf8mb4格式

  8. Java中ArrayList的删除元素总结

    Java中循环遍历元素,一般有for循环遍历,foreach循环遍历,iterator遍历. 先定义一个List对象 List<String> list = new ArrayList&l ...

  9. linux脚本启动应用

    手动输入一些命令,启动任务会很麻烦.可以写个start.sh脚本,去执行. #!bin/sh pid=`ps -ef|grep -v grep|grep ****-1.0-SNAPSHOT.jar|a ...

  10. 获得随机N位数不重复数字

    1, 总结下:每个Random实例里面有一个原子性的种子变量用来记录当前的种子的值,当要生成新的随机数时候要根据当前种子计算新的种子并更新回原子变量.多线程下使用单个Random实例生成随机数时候,多 ...