Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
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.
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.
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
题目链接: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 并查集 二元敌对关系 点拆分的更多相关文章
- 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 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 ...
- CodeForces 766D Mahmoud and a Dictionary
并查集. 将每一个物品拆成两个,两个意义相反,然后并查集即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #i ...
- CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合
题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反 ...
- Codefroces 766D Mahmoud and a Dictionary
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces div2 603 D. Secret Passwords(并查集)
题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...
随机推荐
- MySQL(进阶部分)
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...
- 性能测试进阶指南——基础篇之磁盘IO
本文旨在帮助测试人员对性能测试常用指标做一个简单的讲解,主要包括CPU.内存.磁盘和网络带宽等系统资源,本文仅仅局限于Linux系统,Windows Server系统暂不做考虑. 使用iostat分析 ...
- Delphi调用C#编写的WebService 注意事项
返回的字段值区分大小写,c#和Delphi的字段要一致
- Tensorflow卷积神经网络[转]
Tensorflow卷积神经网络 卷积神经网络(Convolutional Neural Network, CNN)是一种前馈神经网络, 在计算机视觉等领域被广泛应用. 本文将简单介绍其原理并分析Te ...
- K8s简单yaml文件运行例子deployment
kubectl run 创建并运行一个或多个容器镜像. 创建一个deployment 或job 来管理容器. kubectl run 语法: $ run NAME --image=image [--e ...
- CentOS6.3上安装与配置nginx+php+mysql环境
1. 目前nginx采用是源码包安装的方式(yum安装失败),下载地址:http://nginx.org/en/download.html 我这里的安装包是:nginx-1.12.0.tar.gz 2 ...
- IE6-IE9中tbody的innerHTML不能赋值
对于IE6-IE9里如果要设置tbody的innerHTML,可以使用如下替代方法 Js代码 function setTBodyInnerHTML(tbody, html) { var div = d ...
- 移动端目标识别(1)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之TensorFlow Lite简介
平时工作就是做深度学习,但是深度学习没有落地就是比较虚,目前在移动端或嵌入式端应用的比较实际,也了解到目前主要有 caffe2,腾讯ncnn,tensorflow,因为工作用tensorflow比较多 ...
- C#串口数据收发数据
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- YDWE Keynote
[YDWE Keynote] 1.使用YDWE制作的地图,需要在禁用黑色阴影.迷雾.否则进入游戏将漆黑一片,什么都看不到. 2. 3. 4. 5. 6.