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个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...
随机推荐
- Centos7下关于memcached的安装和简单使用
在这里,由于用编译安装memcached服务端过于复杂,因此我选用依赖管理工具 yum 来实现 memcached 的服务端安装: [root@localhost /]# yum install -y ...
- SSH ERROR: Too many Authentication Failures
来自: How to recover from "Too many Authentication Failures for user root" 其中一种可以解决的方式 eval ...
- Webview 中FaultyInfo代码说明
class FaultyInfoHandler(tornado.web.RequestHandler): def get(self): import xmlrpc.client s = xmlrpc. ...
- Tornado 安装及简单程序示例
1.安装步骤:tar xvzf tornado-3.2.tar.gz cd tornado-3.2 python setup.py build sudo python setup.py install ...
- YUV颜色编码解析(转)
原文转自 https://www.jianshu.com/p/a91502c00fb0
- Python模块学习 - IPy
简介 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6地址的类与工 ...
- 【转】C++多继承的细节
这几天写的程序应用到多继承. 以前对多继承的概念非常清晰,可是很久没用就有点模糊了.重新研究一下,“刷新”下记忆. 假设我们有下面的代码: #include <stdio.h> class ...
- python基础===解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX(转载)
本文转自:解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX 从网上抓了一些字 ...
- linux dpm机制分析(下)【转】
转自:http://blog.csdn.net/lixiaojie1012/article/details/23707901 1 设备注册到dpm_list路径 (Platform_devi ...
- sicily 1009. Mersenne Composite N
Description One of the world-wide cooperative computing tasks is the "Grand Internet Mersenne P ...