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的数可以交换无 ...
随机推荐
- Oracle EBS FA 本年折旧
FUNCTION get_ytd_deprn(p_asset_id IN NUMBER, p_book_type_code IN VARCHAR2, p_rate_source_rule IN VAR ...
- Linux系统修改日期时间
linux修改时间 服务器时间与网络时间不符: 一.手动修改 1. date命令:查看当前时间 2. date -s 时分秒 :修改时间 还需要把日期改过来 3. date -s 完整日期时间 ...
- WinForm设置注册表自动启动
string path = Application.StartupPath; SetAutoRun(path + @"\AppName.exe", true); /// <s ...
- 特别篇:Hyper-v群集模拟实战演示
介绍 由于前面几张的都是直接整理了下 九叔的hyper-v电子书发上来的,个人觉得他写的不是最详细,因此今天我按照自己的实际情况来写个模拟的实战演示.所有的东西都通过VMware WorkStatio ...
- MacOS远程Windows提示:远程桌面连接无法验证您希望连接的计算机的身份
解决方法: 1.在Windows端,运行输入 “gpedit.msc”,打开本地组策略编辑器 2.依次打开[计算机配置]→[管理模板]→[windows组件]→[远程桌面服务]→[远程桌面会话主机]→ ...
- JavaScript -- 时光流逝(三):js中的 String 对象的方法
JavaScript -- 知识点回顾篇(三):js中的 String 对象的方法 (1) anchor(): 创建 HTML 锚. <script type="text/javasc ...
- layui中,同一个页面动态加载table数据表格
效果图: 前端代码: <div class="layui-fluid" id="record-user" hidden="hidden" ...
- Unity Shader 效果(1) :图片流光效果
很多游戏Logo中可以看到这种流光效果,一般的实现方案就是对带有光条的图片uv根据时间进行移动,然后和原图就行叠加实现,不过实现过程中稍稍有点需要注意的地方.之前考虑过风宇冲的实现方式,但是考虑到sh ...
- (转)springcloud(一):大话Spring Cloud
http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spring ...
- form表单post请求乱码问题
今天做一个查询的小模块遇到了 请求乱码的问题,表单请求方式是post 解决方法是在web.xml 文件中配置一下内容 <!--配置解决post乱码问题filter --> <filt ...