codeforces#766 D. Mahmoud and a Dictionary (并查集)
题意:给出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 (并查集)的更多相关文章
- codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)
		
题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...
 - 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 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-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合
		
题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反 ...
 - Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
		
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
 - 【codeforces 766D】Mahmoud and a Dictionary
		
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
 - Codeforces Beta Round #5 E. Bindian Signalizing 并查集
		
E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
 - Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
		
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
 - Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
		
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
 
随机推荐
- SpringBoot集成spring-data-jpa注入Bean失败
			
当项目结构正常(spring管理的Bean在SrpingBoot启动类平级或下级,支持spring扫描时),实现类上加 @Service注解,在实现类中注入dao层的Bean时,项目无法启动,无法找到 ...
 - phpstorm设置背景图片
			
对于这个问题,其实很简单,连续按两下shift键或者是Ctrl+Shift+A键即可.出来的界面如下: 输入set Background Image命令,出现的界面如下: 然后选择图片, 点击ok,即 ...
 - Linux 小知识翻译 - 「LDAP」
			
这次聊聊「LDAP」. LDAP是「Lightweight Directory Access Protocol」的所有,从名字上可以看出是协议的一种. LDAP是访问数据库(层次型数据库)的组件.管理 ...
 - Redis主从数据库同步
			
Redis主从同步原理-SYNC和MySQL主从复制的原因一样,Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况.为了分担读压力,Redis支持主从复制,Redis的主从结构可以采 ...
 - 【css】怎么让Chrome支持小于12px 的文字
			
谷歌浏览器Chrome是Webkit的内核,有一个 -webkit-text-size-adjust 的私有 CSS 属性,通过它即可实现字体大小不随终端设备或浏览器影响.CSS样式定义如下:-web ...
 - (5)HomeAssistant  增加设备
			
将设备添加到Home Assistant https://www.home-assistant.io/docs/configuration/devices/ configuration.yaml文件 ...
 - hadoop学习笔记叁--简单应用
			
1.通过命令向HDFS传输文件 上传:./hadoop fs -put hdfs.cmd (本地文件名) hdfs://主机名称:9000/ hadoop fs -copyFromLoca ...
 - ORA-19566: exceeded limit of 0 corrupt blocks for file E:\xxxx\<datafilename>.ORA.
			
How to Format Corrupted Block Not Part of Any Segment (Doc ID 336133.1) To BottomTo Bottom In this D ...
 - SSH远程SOLARIS11时被拒绝
			
在虚拟机中新安装的solaris11,安装过程中配置了静态IP地址用以方便ssh连接,使用root用户ssh连接时,密码没有错,但总是提示密码被拒绝,连接established 代表是通的,telne ...
 - windwos 下编译minicap
			
一.参考github 介绍:https://github.com/openstf/minicap Requirements (前提) NDK, Revision 10e (May 2015) make ...