题意:有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. slimScroll的应用(一)

    本类文章依旧是针对初学者来说的,希望大家看到后觉得有用的能给个赞~~ 什么是slimScroll? 一.官网介绍: slimScroll is a small (4.6KB) jQuery plugi ...

  2. 【js数据结构】图的深度优先搜索与广度优先搜索

    图类的构建 function Graph(v) {this.vertices = v;this.edges = 0;this.adj = []; for (var i = 0; i < this ...

  3. How `delete’ works ?

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=21 February 16, 2013 How `delete’ works ...

  4. springboot的多个配置文件的关系

    一般我们在使用springboot时会用到多个环境下的配置文件,例如 application-dev.yml:开发环境 application-uat.yml:用户验收测试环境 application ...

  5. 北京区域赛I题,Uva7676,A Boring Problem,前缀和差分

    转载自https://blog.csdn.net/weixin_37517391/article/details/83821752 题解 其实这题不难,只要想到了前缀和差分就基本OK了. 我们要求的是 ...

  6. app自动化配置信息

    caps={    "platformName":"Android",#平台名称    "platformVersion":"6. ...

  7. 路由器wan口ip地址显示0.0.0.0怎么办

    http://m.xuexila.com/luyouqi/671049.html 这个网络时代里面我们最常用来连接网络的设备就是路由器了,现在的社会不管是工作还是生活几乎都离不开网络了,同时我们也要学 ...

  8. 使用VS自带WCF测试客户端

    打开VS自带WCF测试客户端 打开VS2015 开发人员命令提示 输入:wcftestclient,回车 提取wcftestclient 当然,可以看到VS2015 开发人员命令提示知道,当前路径在C ...

  9. 手动编译openslide

    1.下载openslide源代码, 2.转到openslide代码目录: ./configure 3.安装依赖库: sudo apt-get update sudo apt-get install l ...

  10. 24. TABLES

    24. TABLES TABLES表提供有关数据库中表的信息. TABLES表有以下列: TABLE_CATALOG :表所属目录的名称.该值始终为def. TABLE_SCHEMA :表所属sche ...