当前位置: 首页 > news >正文

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

D. Mahmoud and a Dictionary

题目连接:

http://codeforces.com/contest/766/problem/D

Description

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 n, m 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.

Sample Input

3 3 4
hate love like
1 love like
2 love hate
1 hate like
love like
love hate
like hate
hate like

Sample Output

YES
YES
NO
1
2
2
2

Hint

题意

给你n个串,然后给你m个关系,p个询问。

每个关系告诉你两个单词是同义还是反义的,如果与之前的不矛盾的话,输出yes;如果矛盾,就忽略这次操作,输出no。

对于每个询问,如果两个单词是同义输出1,反义输出2,不知道输出3.

题解:

经典老题了,POJ食物链。

我们用并查集去做,让2i表示这个词,2i-1表示这个词的反义。

如果i和j同义,就说明2i和2j一样,2i-1和2j-1一样。

如果反义,就说明2i-1和2j一样,2i和2j-1一样。

然后check就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n,m,p;
string s[maxn];
map<string,int> H;
int fa[maxn];
int fi(int x){
    return fa[x]==x?x:fa[x]=fi(fa[x]);
}
void uni(int x,int y){
    x=fi(x),y=fi(y);
    if(x==y)return;
    fa[x]=y;
}
int main()
{
    scanf("%d%d%d",&n,&m,&p);
    for(int i=1;i<=2*n;i++)
        fa[i]=i;
    for(int i=1;i<=n;i++){
        cin>>s[i];
        H[s[i]]=i;
    }
    for(int i=1;i<=m;i++){
        int op;
        string a,b;
        cin>>op>>a>>b;
        int id1=H[a],id2=H[b];
        if(op==1){
            if(fi(id1*2)==fi(id2*2-1)){
                cout<<"NO"<<endl;
                continue;
            }
            if(fi(id1*2-1)==fi(id2*2)){
                cout<<"NO"<<endl;
            }
            cout<<"YES"<<endl;
            uni(id1*2,id2*2);
            uni(id1*2-1,id2*2-1);
        }else{
            if(fi(id1*2)==fi(id2*2)){
                cout<<"NO"<<endl;
                continue;
            }
            if(fi(id1*2-1)==fi(id2*2-1)){
                cout<<"NO"<<endl;
            }
            cout<<"YES"<<endl;
            uni(id1*2,id2*2-1);
            uni(id1*2-1,id2*2);
        }
    }
    for(int i=1;i<=p;i++){
        string a,b;
        cin>>a>>b;
        int id1=H[a],id2=H[b];
        if(fi(2*id1)==fi(2*id2))
            cout<<"1"<<endl;
        else if(fi(2*id1-1)==fi(2*id2-1))
            cout<<"1"<<endl;
        else if(fi(2*id1-1)==fi(2*id2))
            cout<<"2"<<endl;
        else if(fi(2*id1)==fi(2*id2-1))
            cout<<"2"<<endl;
        else
            cout<<"3"<<endl;
    }
}

相关文章:

  • 为你的网络传输加把锁(OpenSSL)
  • Java之戳中痛点 - (2)取余用偶判断,不要用奇判断
  • 如何才能弥补实际工作经验不足,而获得一份好工作?
  • CentOS 7 网卡命名修改为eth0格式
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • python学习笔记 - ThreadLocal
  • ettercap dns_spoof
  • TYVJ1860 后缀数组
  • 【Spark Summit EU 2016】Spark与Couchbase——使用Spark扩展数据库操作
  • shell中的read命令详解
  • Effective C++ 笔记
  • Python MySQLdb在Linux下的快速安装
  • Python通过ssh连接服务器并执行命令
  • Oracle附录——Oracle事务
  • 微信小程序 view 布局
  • JavaScript 如何正确处理 Unicode 编码问题!
  • JS 中的深拷贝与浅拷贝
  • HomeBrew常规使用教程
  • JavaScript创建对象的四种方式
  • mysql 数据库四种事务隔离级别
  • yii2权限控制rbac之rule详细讲解
  • 初识MongoDB分片
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 规范化安全开发 KOA 手脚架
  • 猴子数据域名防封接口降低小说被封的风险
  • 回顾2016
  • 记一次和乔布斯合作最难忘的经历
  • 实习面试笔记
  • 使用 QuickBI 搭建酷炫可视化分析
  • 由插件封装引出的一丢丢思考
  • 【运维趟坑回忆录】vpc迁移 - 吃螃蟹之路
  • k8s使用glusterfs实现动态持久化存储
  • 第二十章:异步和文件I/O.(二十三)
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • #include
  • #NOIP 2014#Day.2 T3 解方程
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • (rabbitmq的高级特性)消息可靠性
  • (七)Knockout 创建自定义绑定
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (转)LINQ之路
  • ***通过什么方式***网吧
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
  • .Net Core缓存组件(MemoryCache)源码解析
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • .NET多线程执行函数
  • .NET中两种OCR方式对比
  • @Autowired注解的实现原理
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @Transactional类内部访问失效原因详解
  • [2017][note]基于空间交叉相位调制的两个连续波在few layer铋Bi中的全光switch——
  • [ABC294Ex] K-Coloring