并查集。

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

#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. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  2. [洛谷P2365] 任务安排

    洛谷题目链接:任务安排 题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时 ...

  3. python实现堆栈、队列

    一.利用python列表实现堆栈和队列 堆栈: 堆栈是一个后进先出的数据结构,其工作方式就像生活中常见到的直梯,先进去的人肯定是最后出. 我们可以设置一个类,用列表来存放栈中的元素的信息,利用列表的a ...

  4. PHP 数据加密

    <?php /** * * 加密 * */ function lock_url($txt, $key = "aiteng") { $chars = "ABCDEFG ...

  5. Maven-Dependency Mechanism

    依赖管理是maven的一个广为人知的特性, 这也是maven擅长的一个领域. 为单一的工程管理依赖不是很难, 但当你着手处理多模块工程和包含数十或数百个模块的应用时, maven可以帮助你很好地保持高 ...

  6. java collection (二)

    1.集合的概念: (1)现实生活中:很多的事物凑在一起. (2)数学中的集合:具有共同属性的事物的总体. (3java 中的集合类: 是一种工具类,就像是容器,存储任意数量的具有共同属性的对象.(集合 ...

  7. 【CodeForces】788E New task

    [题意]n个数,每个数有附加属性0或1,初始全为1.m个操作,每个操作可以改变一个数字的属性为0或1.对于每次操作后的序列求有多少子序列满足要求:5个数字,中间3个数相等且属性为1,左右两个数小于等于 ...

  8. ubuntu 提速

    linux的各大发行版,都有些不必要的服务被默认开启了,针对ubuntu,我们可以采用选择性关闭的方法加速起动,提高系统性能. 这里我们安装一个软件: sudo apt-get install sys ...

  9. 寻找kernel32.dll的地址

    为了寻找kernel32.dll的地址,可以直接输出,也可以通过TEB,PEB等查找. 寻找TEB: dt _TEB nt!_TEB +0x000 NtTib : _NT_TIB +0x01c Env ...

  10. Python3 json、pickle序列化与反序列化

    注意:可以dumps多次,loads只能一次,一般我们只dumps一次,loads一次,多个版本就写入多个文件 一.json序列化与反序列化: 支持各种语言数据交互,但只能处理字典,列表,集合等简单的 ...