CodeForces 766D Mahmoud and a Dictionary
并查集。
将每一个物品拆成两个,两个意义相反,然后并查集即可。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int n,m,q;
char s[],t[];
map<string,int>M; int f[]; int Find(int x)
{
if(x!=f[x]) f[x] = Find(f[x]);
return f[x];
} int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<n;i++)
{
scanf("%s",s);
M[s]=i;
} for(int i=;i<*n;i++) f[i]=i; for(int i=;i<=m;i++)
{
int op; scanf("%d",&op);
scanf("%s%s",s,t); if(op==)
{
int id1=M[s],id2=M[t];
int A=Find(*id1),B=Find(*id2+);
if(A==B)
{
printf("NO\n");
continue;
}
else
{
printf("YES\n");
A=Find(*id1),B=Find(*id2);
if(A!=B) f[A]=B;
A=Find(*id1+),B=Find(*id2+);
if(A!=B) f[A]=B;
}
} else
{
int id1=M[s],id2=M[t];
int A=Find(*id1),B=Find(*id2);
if(A==B)
{
printf("NO\n");
continue;
}
else
{
printf("YES\n");
A=Find(*id1),B=Find(*id2+);
if(A!=B) f[A]=B;
A=Find(*id1+),B=Find(*id2);
if(A!=B) f[A]=B;
}
}
} for(int i=;i<=q;i++)
{
scanf("%s%s",s,t);
int id1=M[s],id2=M[t];
int A=Find(*id1),B=Find(*id2);
int C=Find(*id1),D=Find(*id2+);
if(A==B) printf("1\n");
else if(C==D) printf("2\n");
else printf("3\n");
} return ;
}
CodeForces 766D Mahmoud and a Dictionary的更多相关文章
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- 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 ...
- 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 #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) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...
- Mahmoud and a Dictionary
Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- 【codeforces 766D】Mahmoud and a Dictionary
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)
题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...
随机推荐
- Redis(3) 配置文件 redis.conf
Redis.conf 配置详解: # Redis configuration file example. # # Note that in order to read the configuratio ...
- [LeetCode] string整体做hash key,窗口思想复杂度O(n)。附来自LeetCode的4例题(标题有字数限制,写不下所有例题题目 T.T)
引言 在字符串类型的题目中,常常在解题的时候涉及到大量的字符串的两两比较,比如要统计某一个字符串出现的次数.如果每次比较都通过挨个字符比较的方式,那么毫无疑问是非常占用时间的,因此在一些情况下,我们可 ...
- 【PHP】Windows下配置用mail()发送邮件
ZZ:解决windows系统下php.ini邮件配置正确不发送邮件的问题 php mail()函数在windows不能用,需要安装sendmail,假如是用的XAMPP,则已经下载好,不需要重新下载~ ...
- lnmp、lamp、lnmpa一键安装包(Updated: 2016-4-12)
lnmp.lamp.lnmpa一键安装包(Updated: 2016-4-12) 文章目录 脚本特性 安装步骤 如何添加虚拟主机? 如何删除虚拟主机? 如何管理ftp账号? 数据备份 如何管理服务 ...
- CTF线下赛AWD模式下的生存技巧
作者:Veneno@Nu1L 稿费:200RMB 投稿方式:发送邮件至linwei#360.cn,或登陆网页版在线投稿 原文:https://www.anquanke.com/post/id/8467 ...
- 去掉每行的特定字符py脚本
百度下载一个脚本的时候遇到那么一个情况.每行的开头多了一个空格.https://www.0dayhack.com/post-104.html 一个个删就不要说了,很烦.于是就有了下面这个脚本. #! ...
- swift中的如果在构造方法中使用KVC, 调用了super.init(), 报错, 基本数据类型属性找不到
swift要求, 属性必须有初始化值, 如果不对其赋值, 可以加一个?系统会默认给其包装一个可选值(直说就是nil) 如果定义一个基本类型, 建议直接赋值, 不建议使用? 下面说下标题中的问题 有时候 ...
- 深入浅出Node.js(一) - 初识Node.js
1.Node.js将Javascript解决不确定性所使用的事件驱动方式引入了进来,因为JS是一门事件驱动的语言,旨在能够对外界的事件作出响应; 2.Node.js中,所有的有关异步的操作,都在同步操 ...
- jQuery通过Ajax向PHP服务端发送请求并返回JSON数据
SON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.JSON在前后台交互的过程中发挥着相当出色的作用.请接着往下看教 ...
- 浅谈Javascript设计模式
什么是模式 模式是一种可复用的解决方案,可用于解决软件设计中遇到的常见问题. 也就是说用来解决常见问题的可复用的解决方案. 常见的js设计模式 Constructor(构造器)模式 Construct ...