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个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...
随机推荐
- 我的CCF备考指南
CCF计算机软件能力认证(简称CCF CSP认证). 认证涉及知识点: 认证内容主要覆盖大学计算机专业所学习的程序设计.数据结构.算法以及相关的数学基础知识.包括但不限于: (1)程序设计基础 逻辑与 ...
- git代码冲突
如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候, 在发布这个配置文件的时候,会发生代码冲突: error: Your local changes to the f ...
- 数据结构&字符串:可持久化字典树
利用可持久化Trie树实现范围内取值异或最大值 如果标题没有表达清楚意思,可以看这里的题干: 然后根据异或的性质,异或一个数两次相当于没有异或,那么我们可以维护一个异或前缀和 有了异或前缀和之后我们就 ...
- 数学:GCD
求最大公约数利用辗转相除法: long long gcd(long long a,long long b) { ) return a; else return gcd(b,a%b); } 求最小公倍数 ...
- c# 自定义排序Compare
.net FrameWork 框架博大精深,用着忘着,计划对自己能够想到知识点梳理一下,此篇是对自定义排序的理解: class Program { static void Main(string[] ...
- 元类编程-- metaclass
#类也是对象,type创建类的类 def create_class(name): if name == "user": class User: def __str__(self): ...
- 【hdu1255】线段树求矩形面积交
题意大概就是上图这个样子.<=100组测试数据,每组<=1000个矩形. 题解: 这个问题怎么解决..做了上一题矩形面积并应该就会了.. 对于每个节点维护3个值: cnt:该节点所代表的这 ...
- szoj657 【AHSDFZNOI 7.2 WuHongxun】Odd
[题目大意] 给出$n$个数$a_1, a_2, ..., a_n$,求有多少个区间$[l, r]$,满足每个数都出现了奇数次. $1 \leq n \leq 2 * 10^5, 0 \leq a_i ...
- bzoj3940&&bzoj3942 Ac自动机||kpm算法
方法就是维护一个动态栈 记录栈的每一位匹配到串的哪一位的编号 第一道kmp第二道ac自动机 自己理会 #include<cstdio> #include<cstring> #i ...
- .net WebAPI返回xml、json格式
WebAPI返回xml.json格式简单示例 using System.Net.Http.Formatting; public class TestController : ApiController ...