数据结构----并查集
#
夕林山寸
这个作者很懒,什么都没留下…
展开
-
2020牛客暑期多校训练营(第三场)G Operating on a Graph 并查集+链表 维护图上染色
操作一个颜色组时:与其相连的所有点所代表的颜色全部消失。对于每个点至多会有一次把相邻的点和自己变成相同颜色的操作,经过该次操作后,就永远和相邻的点同色了。我们开一个链表组,记录第i种颜色所相邻的点(注意这里时点不是颜色)。一次x操作后,把颜色x相邻的点y相邻的点集并变成点x相邻的点集,y所在集合的颜色变为x。然后发现:由于每个点最多进行一次把相邻点变成自己的操作,所有每个点x最多访问与x相连的点个数nm+1。总访问次数(n+m)次。再加上并查集,复杂度为O((n+m)*logn)原创 2020-07-20 23:16:44 · 180 阅读 · 0 评论 -
算法竞赛——进阶指南——acwing 364. 网络 e-DCC + LCA + 并查集 O(M+logn*Q)做法
之前我们暴力枚举路径上的边每次要ON的复杂度太慢了。考虑我们每次找路径是找自底向上找,,很多次会找到非桥边,造成不必要的浪费步数。所以我们可以用并查集来压缩路径。若点x与其父亲的路径从桥边变成非桥边,则把x指向y的祖宗。这样就能跳过非桥边进行无意义的访问了。单次复杂度将为logn#include <bits/stdc++.h>using namespace std;typedef long long ll;const int M = 4e5+7;int hea原创 2020-06-30 19:03:41 · 211 阅读 · 0 评论 -
P2294 [HNOI2005]狡猾的商人 带权并查集
顺便回顾下带权并查集。在普通并查集的基础上维护一个d数组,表示权值。这题d[i]表示当前节点x与其祖宗节点get(x), 的sm差值,即sm[x]-sm[get(x)]=d[x]初始d均为0.给了一组 l,r, w 。先令l--.若l,r,在一个集合里。如下图则sm[r]-sm[l] = d[r]-d[f] -(d[l] - d[f])=d[r]-d[l];判断这个值是否等于w即可。若l,r不在一个集合里,要合并这两个集合。如图,我们让l的祖宗fl指向r的祖.原创 2020-06-24 16:27:20 · 225 阅读 · 0 评论 -
ICPC North America Qualifier Contest 2015 K. UnDetected 经典并查集维护连通性
由于起点是y轴,所以只要矩形左边和矩形右边联通,那么就无法到达终点。暴力连边即可,判连通性即可#include <bits/stdc++.h>using namespace std;typedef long long ll;#define ls (o<<1)#define rs (o<<1|1)#define pb push_backcon...原创 2020-04-21 11:53:44 · 228 阅读 · 0 评论 -
kuangbin带你飞专题5 并查集 A - Wireless Network POJ - 2236
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the n...原创 2019-04-09 19:53:57 · 270 阅读 · 0 评论 -
[kuangbin带你飞]专题5 并查集H - Navigation Nightmare POJ - 1984
Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 ...原创 2019-04-12 22:03:16 · 104 阅读 · 0 评论 -
kuangbin带你飞专题5 并查集 B - The Suspects POJ - 1611
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to s...原创 2019-04-09 20:08:01 · 135 阅读 · 0 评论 -
kuangbin带你飞专题5 并查集 C - How Many Tables HDU - 1213
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, a...原创 2019-04-09 20:19:22 · 133 阅读 · 0 评论 -
kuangbin带你飞专题5 并查集 D - How Many Answers Are Wrong HDU - 3038 带权并查集 图解。。。
TT and FF are ... friends. Uh... very very good friends -________-bFF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should ...原创 2019-04-09 21:19:33 · 179 阅读 · 0 评论 -
kuangbin带你飞专题5 并查集 G - Supermarket POJ - 1456 优先队列(小根堆)+贪心 并查集+贪心
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the s...原创 2019-04-02 22:49:05 · 182 阅读 · 0 评论 -
[kuangbin带你飞]专题5 并查集 M - Is It A Tree? POJ - 1308
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.The...原创 2019-04-11 23:00:29 · 85 阅读 · 0 评论 -
BZOJ 4195 并查集+离散化入门 可用来练习这两个知识点
https://www.lydsy.com/JudgeOnline/problem.php?id=4195离散化 用数组进行处理,比map稍微快一丢丢。。。#include<bits/stdc++.h>using namespace std;const int M = 1000000+100;int fa[M];int sa[M],sb[M];int a[M*2]...原创 2019-04-02 20:45:32 · 122 阅读 · 0 评论 -
求连通块个数 (并查集思路)
今天复习DFS时做到连通块的题,突然发现并查集也可以搞 我就试试没想到还真能弄。。。。///#include <iostream>#include <string>using namespace std;const int M=10000+100;string s[110];int fa[M];int get(int x)...原创 2019-03-21 19:51:40 · 3055 阅读 · 0 评论 -
POJ1733 Parity Game 0x40「数据结构进阶」例题 带权并查集(离散化)或扩展域并查集
POJ1733 Parity Game0x40「数据结构进阶」例题http://bailian.openjudge.cn/practice/1733总时间限制:1000ms内存限制:65536kB描述Now and then you play the following game with your friend. Your friend writes do...原创 2019-04-03 23:00:29 · 175 阅读 · 0 评论 -
POJ1182 食物链 并查集 带权或者扩展域 详细分析 适合新手
描述动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。有人用两种说法对这N个动物所构成的食物链关系进行描述:第一种说法是"1 X Y",表示X和Y是同类。第二种说法是"2 X Y",表示X吃Y。此人对N个动物,用上述两种说法,一句接一句地说出K句话,...原创 2019-04-04 21:49:04 · 175 阅读 · 0 评论 -
4901 关押罪犯 扩展域并查集
4901 关押罪犯0x49「数据结构进阶」练习描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N。他们之间的关系自然也极不和谐。很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突。我们用“怨气值”(一个正整数值)来表示某两名罪犯之间的仇恨程度,怨气值越大,则这两名罪犯之间的积怨越多。如果两名怨气值为c 的罪犯被关押在同一监狱,他们俩之间会发生摩擦,并造成影响力...原创 2019-04-05 22:58:38 · 1106 阅读 · 0 评论 -
kuangbin带你飞专题5 I - A Bug's Life POJ - 2492
BackgroundProfessor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gend...原创 2019-04-10 19:05:33 · 155 阅读 · 0 评论 -
[kuangbin带你飞]专题5 F - True Liars POJ - 1417
有一群好人和一群坏人,好人永远说真话,坏人永远说假话。现给出一组话,问能否唯一确定每个人是好人还是坏人。Input对于每组数据 第一行包括三个整数 n p q 表示话语的数量 好人和坏人的数量 接下来n行 形式如 x1 y1 yes/no 表示x1 说y1 是 好人/坏人 以0 0 0 结尾。 n<1000 ; p1 ,p2<300Output如果能唯一确定,输出所有的...原创 2019-04-10 20:24:24 · 228 阅读 · 0 评论 -
[kuangbin带你飞]专题5 并查集 L - 小希的迷宫 HDU - 1272
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的...原创 2019-04-11 21:42:17 · 135 阅读 · 0 评论 -
hdu1856(并查集)
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements. Mr Wang selected a room...原创 2019-02-22 10:35:24 · 195 阅读 · 0 评论