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的数可以交换无 ...
随机推荐
- 孟岩:通证(token)和通证经济的目的在于改善现有经济的效率性
孟岩是最早将token翻译成为通证的区块链大咖,这个翻译已经得到到了越来越人的认可.原来它叫代币,孟岩建议把它翻译成通证.以下是孟岩关于通证的注解. (孟岩,柏链道捷CEO,CSDN副总裁,区块链通证 ...
- C#编辑EXE使用的appSettings节点的Config文件
/// <summary> /// 保存配置文件的设定 /// </summary> /// <param name="Key"></pa ...
- Can We Make Operating Systems Reliable and Secure?
Andrew S. Tanenbaum, Jorrit N. Herder, and Herbert Bos Vrije Universiteit, Amsterdam Microkernels-lo ...
- 对讲解OS文献的反思
前天把OS中Taneubaum写的那篇论文Can We Make Operating Systems Reliable and Secure?给班上的同学讲解了一遍.这篇文献我花了三天的时间才把它弄好 ...
- LeetCode算法题-Isomorphic Strings(Java实现)
这是悦乐书的第191次更新,第194篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第50题(顺位题号是205).给定两个字符串s和t,确定它们是否是同构的.如果s中的字符 ...
- LeetCode算法题-Single Number(Java实现)
这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两 ...
- 序列对象(bytearray, bytes,list, str, tuple)
列表: L.append(x) # x追加到L尾部 L.count(x) # 返回x在L中出现的次数 L.extend(m) # Iterable m的项追加到L末尾 L += m # 功能同L.ex ...
- php实现TXT小说章节解析、小说章节在线阅读
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 要实现TXT文本章节的解析,大概思路是在每个章节加入了特定的字符,然后根据字符的起始位 ...
- D. Zero Quantity Maximization(hash+map)
题意:就是让c=a*x+b,给你一个a[],b[],让你尽可能多的让c[]=0,输出有多少. 思路:直接令c=0,则x=-b/a, 也就是一条直线,通过这样就用hash值使相同的k值映射到一起,使用了 ...
- 【vue】vue +element 搭建项目,使用el-date-picker组件遇到的坑
1.html <el-form-item prop="dateTime"> <el-date-picker v-model="messageDataFo ...