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. 通过SID查找历史执行的SQL语句

    这次某系统发生严重的阻塞,但是去查顶级会话,发现已经没有该对应的sql_id了,于是我们只用通过 v$active_session_history 视图来寻找.下面是查找的过程: 1.查找顶层ASH历 ...

  2. orcal - 添加用户、授权

    create user jy2 identified by jy2; grant dba to jy2;

  3. 关于学习CentOS7使用firewalld打开关闭防火墙和端口

    1.firewalld简介 firewalld是centos7的一大特点,主要有两个优点:一是支持动态更新,不需要重启服务:二就是加入了防火墙的“zone”概念. firewalld有图形界面和工具界 ...

  4. asp.net core结合docker实现自动化获取源码、部署、更新

    之前入坑dotnet core,由于一开始就遇到在windows上编译发布的web无法直接放到centos上执行.之后便直接研究docker,实现在容器中编译发布.然后就越玩越大,后来利用git的ho ...

  5. jsfl 添加代码

    var __name=""; var __author=""; var __book=[]; var newJz=""; for(var d ...

  6. [SF] Symfony 标准 HttpFoundation\Request 实现分析

    使用方式 /** * 如果直接示例化 Request 默认是没有参数的,可以自己传入 * 本方法将 PHP 超全局变量作为参数然后实例化自身(Request)进行初始化. */ $request = ...

  7. 2017-11-11 Sa Oct Is it online

    2017-11-11 Sa Oct Is it online 9:07 AM After breakfast I tried connecting to the course selection sy ...

  8. Docker中使用Tomcat并部署war工程

    准备 首先从远程仓库拉取Tomcat镜像到本地. docker pull tomcat 使用images命令查看是否拉取成功. 创建镜像文件并将war包上传到同级目录下.(本文是在/usr/local ...

  9. tornado 基于MongoDB存储 session组件开发

    1.开发伊始 根据源码中RequestHandler类中发现__init__函数中会调用自身initialize函数,此函数中为pass,即可以围绕initialize开发一系列的组件 2.开发实现 ...

  10. mongodb安装使用简单命令

    .window+x,A,管理员进入cmd.cd C:\Program Files\MongoDB\Server\3.4\bin.安装:mongod --dbpath "D:\work\DB\ ...