并查集。

将每一个物品拆成两个,两个意义相反,然后并查集即可。

#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的更多相关文章

  1. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  2. 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 ...

  3. Codefroces 766D Mahmoud and a Dictionary

    D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...

  4. 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 ...

  5. 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 ...

  6. Mahmoud and a Dictionary

    Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input sta ...

  7. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

  8. 【codeforces 766D】Mahmoud and a Dictionary

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)

    题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...

随机推荐

  1. JS常见的算法

    原文链接:Jack Pu's Blog 虽说我们很多时候前端很少有机会接触到算法.实际上学习数据结构与算法对于工程师去理解和分析问题都是有帮助的.如果将来当我们面对较为复杂的问题,这些基础知识的积累可 ...

  2. 11.nginx upload module + python django 后台 实现视频上传与切片

    1.需求:支持视频上传并切片,支持通过m3u8文件播放 2.视频切片的上一节已经谈过,这一节主要是视频上传的处理 第一步:upload-module模块安装 -----------首先下载upload ...

  3. c# bootstrap-table 知识

    bootstrap-table 提供手机端,电脑端访问,提供分页,筛选等. bootstrap-table说明文档:http://bootstrap-table.wenzhixin.net.cn/zh ...

  4. 【HDU】6148 Valley Numer 数位DP

    [算法]数位DP [题意]定义V-number为从左到看单位数字未出现先递增后递减现象的数字,求0~N中满足条件的数字个数.T<=200,lenth(n)<=100 [题解]百度之星201 ...

  5. Creating a new dynamic form project, business modeling.

    The domain logic is like there are a bunch of objects, as well as a lot of configurations, according ...

  6. Piggy-Bank(多重背包+一维和二维通过方式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题面: Problem Description Before ACM can do anythi ...

  7. hdu 1599 find the mincost route (最小环与floyd算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  8. perl6中字符串字母编历

    use v6; my $input = prompt '输入字符串:'; for $input.words -> $word { say $word; } for $input.comb -&g ...

  9. 去掉每行的特定字符py脚本

    百度下载一个脚本的时候遇到那么一个情况.每行的开头多了一个空格.https://www.0dayhack.com/post-104.html 一个个删就不要说了,很烦.于是就有了下面这个脚本. #! ...

  10. CreateProcess中的部分参数理解

    函数原型,这里写Unicode版本 WINBASEAPIBOOLWINAPICreateProcessW( _In_opt_ LPCWSTR lpApplicationName, //可执行文件名字 ...