题意:给出n个单词,m条关系,q个询问,每个对应关系有,a和b是同义词,a和b是反义词,如果对应关系无法成立就输出no,并且忽视这个关系,如果可以成立则加入这个约束,并且输出yes。每次询问两个单词的关系,1,同义词,2,反义词,3,不确定

题解:这题思路比较奇特,开辟2*n的并查集的空间,第i+n代表i的反义词所在的树,初始为i+n,也就是说i+n代表i的反义词

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+10;
char a[30],b[30];
map<string,int>ma;
int book[maxn*2];
int fin(int x)
{
if(book[x]==x)return x;
else return book[x]=fin(book[x]);
}
int main()
{
int n,m,q;
scanf("%d %d %d",&n,&m,&q);
for(int i=1;i<=n;i++)
{
scanf("%s",a);
ma[a]=i;
}
for(int i=1;i<=2*n;i++)
book[i]=i;
for(int i=1;i<=m;i++)
{
int com;
scanf("%d %s %s",&com,a,b);
int x=ma[a],y=ma[b];
if(com==1)
{
if(fin(x)==fin(y+n)||fin(y)==fin(x+n))
printf("NO\n");
else
{
printf("YES\n");
book[fin(x)]=fin(y);
book[fin(x+n)]=fin(y+n);
}
}
else
{
if(fin(x)==fin(y)||fin(x+n)==fin(y+n))
printf("NO\n");
else
{
printf("YES\n");
book[fin(x)]=fin(y+n);
book[fin(y)]=fin(x+n);
}
}
}
for(int i=1;i<=q;i++)
{
scanf("%s %s",a,b);
int x=ma[a],y=ma[b];
if(fin(x)==fin(y)||fin(x+n)==fin(y+n))
printf("1\n");
else if(fin(x)==fin(y+n)||fin(y)==fin(x+n))
printf("2\n");
else
printf("3\n");
}
return 0;
}

  

codeforces#766 D. Mahmoud and a Dictionary (并查集)的更多相关文章

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

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

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

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

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

  4. CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合

    题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反 ...

  5. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  6. 【codeforces 766D】Mahmoud and a Dictionary

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

  7. Codeforces Beta Round #5 E. Bindian Signalizing 并查集

    E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  8. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  9. Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)

    题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...

随机推荐

  1. CentOS 7更改yum源与更新系统

    在CentOS 7下更改yum源与更新系统. [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo cp /etc/yum.repos.d/CentOS-Base.rep ...

  2. c/c++ 标准库 map set 大锅炖

    标准库 map set 大锅炖 一,关联容器有哪些 按关键字有序保存元素 map 保存key和value set 只保存key mulutimap key可以重复出现 multiset key可以重复 ...

  3. c/c++ 数组 数组的引用,指针数组的引用

    c/c++ 数组 知识点 1,数组的声明和初始化,对应代码里的test1和test2 2,char数组,对应代码里的test3 3,数组不可以拷贝和复制,对应代码里的test4 4,指针数组, 数组的 ...

  4. 优雅的使用Spring

    Bean声明的三种方式: 1.@Component, @Service, @Repository,@Controller 用于声明一个组件,程序启动时会扫描这些组件,并创建实例. 2.在applica ...

  5. C语言 统计一篇英文短文中单词的个数

    //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ #include<stdio.h> #define N 1000 void main(){ ] ...

  6. ubuntu如何安装 adobe flash player或adobe插件

    方法/步骤 第一步当然是打开终端控制器.有很多方法,这里推荐使用快捷键:ctrl+alt+T.快捷又方便. 然后更新源列表,使用如下命令:sudo apt-get update,后面要输入密码. 下面 ...

  7. https验证证书的三个级别

    一.无条件信任证书 1. func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge ...

  8. git pull request 流程

    git pull request 用于在 fork 官方 repo 到个人 github, 在本地修改后,向官方 repo 请求合并.在官方团队审查过代码后,就可以将自己所做的改动合并到官方 repo ...

  9. 转://Oracle 事务探索与实例(二)

    一数据库版本 SYS@LEO1>select * from v$version; BANNER ------------------------------------------------- ...

  10. jira8.0.2安装与破解

    环境: centos7.4 java1.8 jira版本:8.0.2 mysql :mariadb 软件下载: 破解包: atlassian-extras-3.2.jar 连接mysql驱动: mys ...