题意:有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(并查集)的更多相关文章

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

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

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

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

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

  4. codeforces#766 D. Mahmoud and a Dictionary (并查集)

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

  5. D. Mahmoud and a Dictionary 种类并查集

    http://codeforces.com/contest/766/problem/D 所谓种类并查集,题型一般如下:给定一些基本信息给你,然后又给出一些信息,要求你判断是真是假.例如给出a和b支持不 ...

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

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

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

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

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

随机推荐

  1. websocket 加layim实现在线聊天系统

    实现流程: 1.浏览器连接服务器时保存所有用户id以及对应的唯一session(session用户用户消息推送). 1.1:判断登录用户是否有离线消息(个人消息以及群消息),有则将离线消息进行推送给登 ...

  2. Arduino中数据类型转换 float/double转换为char 亲测好使,dtostrf()函数

    如何轻松玩转Arduino单片机,当我在进行数据转换的时候,遇到了问题,尝试了C语言和C++中的好多函数,都没有达到将float型数据转换为char型的目的.苦苦查阅资料后,终于找到了一个大神级函数! ...

  3. 洛谷 P2053 [SCOI2007]修车

    题目描述 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待 ...

  4. 备忘录:python 3在class中使用yield

    之前代码都是直接在函数级别使用yield,但封装class后如何使用yield很少遇到. 经过半天的学习,总算完成示例.代码没有什么特殊地方,仅仅作为一个工作项. 与生成器合作: ########## ...

  5. iOS开发内购全套图文教程

    2015年最全的内购图文教程,首先是填各种资料,最后是代码,废话不多说,直接上图 ======================第一部分协议=============== 第一步 第二步 第三步 第四步 ...

  6. 【C++】cerr,cout,clog

    http://stackoverflow.com/questions/16772842/what-is-the-difference-between-cout-cerr-clog-of-iostrea ...

  7. Recyclerview设置间距

    首先自定义一个RecyclerViewDivider 继承 RecyclerView.ItemDecoration,实现自定义. public class RecyclerViewDivider ex ...

  8. 递归的可视化(Fibonacci)

    递归的可视化 修改递归函数,使其能够显示打印出每次函数递归调用的形参的值. 每一级调用的输出都带有一级缩进,就是使得程序的输出清晰.有趣并且有含义. 思路 以斐波那契数列为例,假设n=5,递归的形参如 ...

  9. 富通天下(W 笔试)

    纸质算法题目 1.给你一个字符串,找出其中第一个只出现过一次的字符及其位置 正解:一层for循环,循环按序取出字符串中的单个字符,循环体内部使用String类的indexOf(),从当前字符下标往后搜 ...

  10. centOS出现 -bash: vim: command not found

    问题描述 用centos 的主机的時候, 用 vim 时出现 -bash: vim: command not found. 只能使用 vi. 那么如何安裝 vim 呢? 解决步骤 1.查看是否安装 输 ...