【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 ...
随机推荐
- SpringBoot 2.x (7):拦截器
类似以前SpringMVC的拦截器,但也有一些区别 SpringBoot的拦截器有两种方式: 第一种方式:过时的方式,适用于SpringBoot1.x的方式 package org.dreamtech ...
- JVM内存区域参数配置
转自:https://www.jianshu.com/p/5946c0a414b5 需要提前了解的知识点: JVM内存模型 JVM垃圾回收算法 下图是JVM内存区域划分的逻辑图 JVM内存区域逻辑 ...
- 洛谷 P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- PHP CLI应用的调试原理
我们在Eclipse里选中一个PHP文件,右键选择Debug As->PHP CLI Application. 所谓CLI应用,是指这种脚本文件不需要任何Web服务器即可运行,当然, PHP运行 ...
- Netbeans Makefile: recipe for target 'XXX' failed 运行failed(退出值 -1073741511 找不到C/C++库文件,关键字
今天不知怎么的又出错了 吐血了 找不到NULL new等关键字了 看到知乎上有人和我一个问题,怎么办?很简单卸载了以前的cygwin和netbeans然后重装,我重装时没有把以前的cygwin ...
- leetcode_1039. Minimum Score Triangulation of Polygon_动态规划
https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题意:给定一个凸的N边形(N<=50),每个顶点有一个 ...
- Unity3D windows平台视频录制录屏插件 UnityRecorder
例子:从官方例子简单改了 using UnityEditor;using UnityEditor.Recorder;using UnityEditor.Recorder.Input;using Sys ...
- 用python+pygame写贪吃蛇小游戏
因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...
- hql语法002
1. package cn.jbit.hibernatedemo.test; import java.util.Iterator; import java.util.List; import org. ...
- execl, execlp, execle, execv, execvp - 执行某个文件
总览 (SYNOPSIS) #include <unistd.h> extern char **environ; int execl( const char *path, const ch ...