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. C#拷贝一个库的表到另外一个库中(的四种方式)

    1.该方法 基本不能用于实际开发中 ,仅供学习参考 public string Copy() { //要复制的表名 string table = "AAAAA"; //构造连接字符 ...

  2. Android 开发 关于7.0 FileUriExposedException异常 详解

    异常原因 Android不再允许在app中把file://Uri暴露给其他app,包括但不局限于通过Intent或ClipData 等方法.原因在于使用file://Uri会有一些风险,比如: 文件是 ...

  3. expdp/impdp数据泵分区表导入太慢了。添加不检查元数据参数提高效率:ACCESS_METHOD=DIRECT_PATH

    分区表数据泵导入太慢,达不到客户的迁移要求导出语句如下:(10G单节点)userid='/ as sysdba'directory=milk_dirdumpfile=mon_%U.dmplogfile ...

  4. Https证书配置

    本文介绍配置免费证书的方法 具体步骤: 一.获取免费CA证书 步骤1到腾讯云找到: 二.申请完成 后域名验证指引 申请域名型证书,可以通过以下方式验证域名的所有权: 1. 手动 DNS 验证 通过解析 ...

  5. Hive:map字段存储和取用 ( str_to_map函数 )

    str_to_map(字符串参数, 分隔符1, 分隔符2) 使用两个分隔符将文本拆分为键值对. 分隔符1将文本分成K-V对,分隔符2分割每个K-V对.对于分隔符1默认分隔符是 ',',对于分隔符2默认 ...

  6. oracle 的tnsnames.ora,listener.ora

    x:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN listener.ora: # listener.ora Network Conf ...

  7. orcal - 增删改

    数据跟新 增删改 将emp复制到myemp CREATE TABLE myemp AS SELECT * FROM emp; 新增: INSERT INTO 表名称[(列名称1,列名称2,.....) ...

  8. getPageNumRange

    <script> function getPageNumRange(pagenumstr) { var pages=pagenumstr.split(";"); pag ...

  9. 去除编辑器的HTML标签

    去除HTML携带的标签常用函数 string strip_tags(string str); 编辑器存放内容到数据库时p标签会转换成这种<p></p> 需要使用htmlspec ...

  10. chrome开发者工具实现整站截屏

    我们经常要遇到将整个网站作为图片保存下来的情况,而windows系统自带的PrintScreen键只能保存当前屏幕的截图 在chrome浏览器中可以安装第三方的截图插件实现整站截图 今天我们要介绍的方 ...