题意:有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. 【转】java编程思想第20章的注解例子用到的com.sun.mirror的jar包

    Java編程思想>中的注解代码中引入过这么一个包(com.sun.mirror),书上说的是在Jdk中有个tools.jar中,引入这个包就每这个问题了,但是笔者用的是JDK 1.8,把这个包i ...

  2. 网页尺寸scrollHeight/offsetHeight

    scrollHeight和scrollWidth,获取网页内容高度和宽度. 一.针对IE.Opera: scrollHeight 是网页内容实际高度,可以小于 clientHeight. 二.针对NS ...

  3. Android小玩意儿-- 从头开发一个正经的MusicPlayer(一)

    之前从未接触过音乐播放器这块东西的开发.今天偶然想做一个自己的音乐播放器.算是练练手.既然要做,就要做一个正儿八经的App.很多网上的资料也是模模糊糊,不是很全,现在开始,自己摸索着尝试着一步一步的做 ...

  4. C#处理Android Audio and Video

    Video Converter for .NET (C#) FFMpeg wrapper http://www.nrecosite.com/video_converter_net.aspx Docum ...

  5. Knockout-了解Observable与computed

    KO是什么? KO不是万能的,它的出现主要是为了方便的解决下面的问题: UI元素较多,用户交互比较频繁,需要编写大量的手工代码维护UI元素的状态.样式等属性? UI元素之间关系比较紧密,比如操作一个元 ...

  6. Java Web开发之Spring | SpringMvc | Mybatis | Hibernate整合、配置、使用

    1.Spring与Mybatis整合 web.xml: <?xml version="1.0" encoding="UTF-8"?> <web ...

  7. VBA 从sql存储过程-记录集-导入

    cnn.Open cnnstr cmd.ActiveConnection = cnn cmd.CommandTimeout = 120 cmd.CommandText = "dbo.t_bi ...

  8. 华为S3700交换机DHCP 配置

    1.设置交换机名称 system-view [Huawei]sysname dhcp01 [dhcp01] 2.配置管理IP [dhcp01]interface Vlanif 1 [dhcp01-Vl ...

  9. ABAP的HTTP_GET和Linux的curl

    curl是利用URL语法在命令行方式下工作的开源文件传输工具,广泛应用在Unix,多种Linux发行版中. 在Windows系统下也有移植版. curl尤其被广泛应用在github上众多开源软件和框架 ...

  10. saltstack 源码安装

    面向对象编程(oop) 面向对象: 面向对象三大特性: 封装 继承 多肽封装: 封装就是将具体的客观事物封装成抽象的类.并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可行的进行信息隐藏继承 ...