【CF766D】Mahmoud and a Dictionary(并查集)
题意:有n个单词,给定m个关系,每个关系要么表示单词a与单词b相同,要么表示单词a与单词b相反。
并且“相同”与“相反”有性质:若a与b相同,b与c相同,则a与c相同(从而单词的相同关系是等价关系);
若a与b相反,b与c相反,则a与c相同。按顺序判断这m个关系是否可以成立,若可以成立,则加上这个关系,否则忽略。
再给定q个询问,每个询问 查询单词a与单词b的关系(相同、相反或未知)。
n,m,q<=10^5
思路:并查集
设与i相反的单词集合中的代表为fan[i],则x与y相同的条件是:find(fan[x])<>find(y),合并(find(x),find(y)),(find(fan[x]),find(fan[y]))
不同:find(x)<>find(y),合并时类似
询问类似
var a:array[..]of string;
fa,fan:array[..]of longint;
n,m,k,i,x,y,s,que,j,t1,t2,x1,y1,x2,y2:longint;
tmp,mid,t,ch:ansistring; procedure swap(var x,y:string);
begin
t:=x; x:=y; y:=t;
end; procedure qsort(l,r:longint);
var i,j,t:longint;
begin
i:=l; j:=r; mid:=a[(l+r)>>];
repeat
while mid>a[i] do inc(i);
while mid<a[j] do dec(j);
if i<=j then
begin
swap(a[i],a[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; function hash(x:string):longint;
var l,r,mid:longint;
begin
l:=; r:=n;
while l<=r do
begin
mid:=(l+r)>>;
if a[mid]=x then exit(mid)
else if a[mid]<x then l:=mid+
else r:=mid-;
end;
end; function find(k:longint):longint;
begin
if k= then exit();
if fa[k]<>k then fa[k]:=find(fa[k]);
exit(fa[k]);
end; procedure union(x,y:longint);
var p,q:longint;
begin
if (x=)or(y=) then exit;
p:=find(x); q:=find(y);
if p<>q then fa[p]:=q;
end; begin
// assign(input,'cf766d.in'); reset(input);
//assign(output,'cf766d.out'); rewrite(output);
readln(n,m,que);
readln(ch);
k:=length(ch);
s:=;
for j:= to k do
begin
if ch[j]=' ' then begin inc(s); a[s]:=tmp; tmp:=''; continue; end;
tmp:=tmp+ch[j];
end;
inc(s); a[s]:=tmp; qsort(,n);
for i:= to n do
begin
fan[i]:=; fa[i]:=i;
end;
for i:= to m do
begin
readln(ch); tmp:=''; s:=;
k:=length(ch);
for j:= to k do
begin
if ch[j]=' ' then
begin
inc(s);
if s= then x:=hash(tmp);
tmp:='';
continue;
end;
if (ch[j]>='a')and(ch[j]<='z') then tmp:=tmp+ch[j];
end;
y:=hash(tmp);
x1:=find(x); y1:=find(y);
x2:=find(fan[x1]); y2:=find(fan[y1]);
case ch[] of
'':
begin
if x2=y1 then writeln('NO')
else
begin
writeln('YES');
union(x1,y1);
union(x2,y2);
if y2= then fan[y1]:=x2;
end;
end;
'':
begin
if x1=y1 then writeln('NO')
else
begin
writeln('YES');
if x2> then union(y1,x2)
else fan[x1]:=y1;
if y2> then union(x1,y2)
else fan[y1]:=x1;
end;
end;
end;
end;
for i:= to que do
begin
readln(ch); tmp:=''; s:=;
k:=length(ch);
for j:= to k do
begin
if ch[j]=' ' then
begin
inc(s);
if s= then x:=hash(tmp);
tmp:='';
continue;
end;
if (ch[j]>='a')and(ch[j]<='z') then tmp:=tmp+ch[j];
end;
y:=hash(tmp);
x1:=find(x); y1:=find(y);
x2:=find(fan[x1]); y2:=find(fan[y1]);
if x1=y1 then writeln()
else if (x1=y2)or(x2=y1) then writeln()
else writeln();
end;
//close(input);
//close(output);
end.
【CF766D】Mahmoud and a Dictionary(并查集)的更多相关文章
- 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#766 D. Mahmoud and a Dictionary (并查集)
题意:给出n个单词,m条关系,q个询问,每个对应关系有,a和b是同义词,a和b是反义词,如果对应关系无法成立就输出no,并且忽视这个关系,如果可以成立则加入这个约束,并且输出yes.每次询问两个单词的 ...
- D. Mahmoud and a Dictionary 种类并查集
http://codeforces.com/contest/766/problem/D 所谓种类并查集,题型一般如下:给定一些基本信息给你,然后又给出一些信息,要求你判断是真是假.例如给出a和b支持不 ...
- codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)
题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...
- 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 ...
- Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- 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 ...
随机推荐
- RxJava尝试取代Handler初探
在之前的一篇文章中,我们探究了RxJava的使用方法,详细请看https://www.cnblogs.com/yanyojun/p/9745675.html 根据扔物线大神的描述,如果用一个词来概括R ...
- HDU 5381 The sum of gcd (技巧,莫队算法)
题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...
- CentOS 7下安装配置proftpd搭建ftp服务器
proftpd全称:Professional FTP daemon,是针对Wu-FTP的弱项而开发的,除了改进的安全性,还具备许多Wu-FTP没有的特点,能以Stand-alone.xinetd模式运 ...
- MongoDB最简单的入门教程之三 使用Java代码往MongoDB里插入数据
前两篇教程我们介绍了如何搭建MongoDB的本地环境: MongoDB最简单的入门教程之一 环境搭建 以及如何用nodejs读取MongoDB里的记录: MongoDB最简单的入门教程之二 使用nod ...
- 在proe模型文件里面存储用户数据
存储外部数据 author:visualsan 2014.2 上海 1.简介 利用外部数据存储外部接口,可以在模型文件里面尺寸用户自定义数据.在模型保存时数据自动存储,在模型载入时数据自动载入.外部数 ...
- fork后子进程从哪里开始执行
子进程和父进程都从调用fork函数的下一条语句开始执行
- Android安卓下拉阻尼效果实现原理及简单实例
原理 这种效果是通过自定义控件的方式来实现的,我自定义了一个控件类型,这个自定义控件(PullDownDumperLayout)继承自线性布局(LinearLayout). 用户可以下拉弹出的那个 ...
- Unity3D 在自定义脚本中实现Button组件上的OnClick面板
下述内容不对c#语法做过多讲解,仅对已入门并有兴趣的同学做为学习和拓展的资料 大家在Unity制作的过程中一定都使用过UI功能,那么很多人也一定见过这个面板: 那么我们如何能在自己的脚本中添加上像On ...
- Promise 理解与使用
个人觉得这篇博客写的非常详细且易懂,推荐给小伙伴们~ https://www.cnblogs.com/lvdabao/p/es6-promise-1.html#!comments
- Dart开发环境搭建
一.SDK的安装与环境配置 1. 下载Dark SDK http://www.gekorm.com/dart-windows/ 2. 安装SDK 3. 配置环境变量(一般已经默认生成好了,这里可以 ...