Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D
题目:
4 seconds
256 megabytes
standard input
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: 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.
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, mis 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.
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.
3 3 4
hate love like
1 love like
2 love hate
1 hate like
love like
love hate
like hate
hate like
YES
YES
NO
1
2
2
2
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
YES
YES
YES
YES
NO
YES
3
3
1
1
2
思路:带权并查集模板题。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+; int n,m,q,f[K],rl[K];
map<string,int>hs;
string sa,sb; int fd(int x)
{
if(f[x]==x) return x;
int fa=f[x];
f[x]=fd(f[x]);
rl[x]=(rl[x]+rl[fa])%;
return f[x];
} int main(void)
{
cin>>n>>m>>q;
for(int i=;i<=n;i++)
cin>>sa,hs[sa]=i,f[i]=i;
for(int i=,op;i<=m;i++)
{
cin>>op>>sa>>sb;
int x=hs[sa],y=hs[sb];
int fx=fd(x),fy=fd(y);
op--;
if(fx!=fy)
{
puts("YES");
f[fy]=fx;
rl[fy]=(op+rl[x]+rl[y])%;
}
else
{
if((rl[x]+rl[y])%==op)
puts("YES");
else
puts("NO");
}
}
for(int i=,ans;i<=q;i++)
{
cin>>sa>>sb;
int x=hs[sa],y=hs[sb];
int fx=fd(x),fy=fd(y);
if(fx!=fy)
ans=;
else if((rl[x]+rl[y])%==)
ans=;
else
ans=;
printf("%d\n",ans);
}
return ;
}
Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary的更多相关文章
- 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 ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message dp
C. Mahmoud and a Message 题目连接: http://codeforces.com/contest/766/problem/C Description Mahmoud wrote ...
- Codeforces Round #396 (Div. 2) B. Mahmoud and a Triangle 贪心
B. Mahmoud and a Triangle 题目连接: http://codeforces.com/contest/766/problem/B Description Mahmoud has ...
- Codeforces Round #396 (Div. 2) A. Mahmoud and Longest Uncommon Subsequence 水题
A. Mahmoud and Longest Uncommon Subsequence 题目连接: http://codeforces.com/contest/766/problem/A Descri ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
地址:http://codeforces.com/contest/766/problem/E 题目: E. Mahmoud and a xor trip time limit per test 2 s ...
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
地址:http://codeforces.com/contest/766/problem/C 题目: C. Mahmoud and a Message time limit per test 2 se ...
- Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle
地址:http://codeforces.com/contest/766/problem/A A题: A. Mahmoud and Longest Uncommon Subsequence time ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip 树形压位DP
题目链接:http://codeforces.com/contest/766/problem/E Examples input 3 1 2 3 1 2 2 3 out 10 题意: 给你一棵n个点 ...
随机推荐
- kaggle比赛之youtube视频分类示例
1.训练模型:建bucket,建job,提交运行. BUCKET_NAME=gs://${USER}_yt8m_train_bucket_logisticmodel # (One Time) Crea ...
- Mathematica之基本操作
1.清楚所有变量 Clear["Global`*"];
- Dnsmasq简介
Dnsmasq是一个开源的轻量级DNS转发和DHCP.TFTP服务器,使用C语言编写.Dnsmasq针对家庭局域网等小型局域网设计,资源占用低,易于配置.支持的平台包括Debian.Fedora.Sm ...
- BlockingQueue(阻塞队列)分析
如果读者还有一点印象,我们在实现线程池时,用了队列这种数据结构来存储接收到的任务,在多线程环境中阻塞队列是一种非常有用的队列,在介绍BlockingQueue之前,我们先解释一下Queue接口. Qu ...
- Hadoop2的HA安装(high availability):JournalNode+ zookeeper
前面介绍过使用NFS+zookeeper来解决namenode单点失败问题,因为NFS可能也会存在单点问题,所以hadoop提供了一种叫做JournalNode技术,这项技术可以在JournalNod ...
- TaoKeeper
基于zookeeper的监控管理工具taokeeper,由淘宝团队开源的zk管理中间件: 按照taokeeper官方说明 http://jm-blog.aliapp.com/?p=1450 下载tao ...
- IntelliJ IDEA2017 java连接mysql数据库并查询数据
最近自己开始重新学习java基础了,做java开发不可避免要处理数据库,由于好久不写java了,对idea也有点陌生了.所以这里写篇用jdbc来连接mysql的文章 至于mysql怎么装,请自行百度 ...
- OA办公管理系统最全设计
参考文章:http://www.cnblogs.com/shisanmu/articles/5671785.html
- SurvivalShooter学习笔记(五.敌人生命)
敌人生命系统(受伤 死亡) 敌人生成后有初始生命,被攻击受伤有打击特效,降低生命值,直至死亡: 死亡后怪物:播放死亡音效,动画,然后下沉地表,销毁:玩家:得到相应分数. 敌人生命脚本如下: 1.变量: ...
- android应用安全——组件通信安全(Intent)
这里主要涉及到了Activity.Content Provider.Service.Broadcast Receiver等.这些如果在Androidmanifest.xml配置不当,会被其他应用调用, ...