![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
并查集
AKone123456
这个作者很懒,什么都没留下…
展开
-
Total Eclipse--------------------------思维(并查集+思维+贪心)
题意:给定n个点,m条边的无向图,每个点有一个权值vi ,你每次选择一个连通块使得连通块里面的点权值vi-1问最少操作多少次使得n个点的权值都为0解析:贪心,我们按照权值从大到小排序,我们需要把权值大的减成和权值小的一样,同时还有维护连通块的数量。举个例子节点编号为:1,2,3节点权值为:4,3,2现在将1号节点加入到图中 连通块只有cnt=1个将2号节点加入到图中,我们计算对答案的贡献就是1号节点和2号节点的差值:(4-3)*cnt,那么1号节点的权值就为3假设(1,2)不连通的...原创 2020-07-24 16:42:58 · 174 阅读 · 0 评论 -
B. so easy-----------------------------思维(unordered_map模拟并查集)
解析:利用unordered_map来模拟并查集。1操作:我们只需要fa[x]=find(x+1) 为什么不是x+1,因为有可能x+1也被标记不可用了,指向了x+22操作:如果x不在这个map里面,那答案就是自己,否则就要find找到自己的父节点。为什么不能写成数组形式,因为会爆内存#include<bits/stdc++.h>using namespace std;typedef long long ll;unordered_map<int ,int > fa..原创 2020-07-11 20:33:25 · 273 阅读 · 0 评论 -
问题 B: 分院帽 (hat)----------------------------思维(并查集+0/1背包)
题目描述在霍格沃兹魔法学校,每年都要举行分院仪式。分院帽今年不但负责将学生分到格兰芬多,赫奇帕奇,拉文克劳以及斯莱特林四个学院。还要把一部分学生分到新建起的一所学院 —— StandardDeviation学院。也就是离入学标准还差一点的学院。(因为想来学习魔法的人真的太多了,大约有n人)但即使是这样,也不能招收太多人,准确来说,学校打算招收m人。 可是,这是一所公平的学校,如果一些同分的学生中,只有一些被招收走了,那么学校将会颜面尽失。老师十分不好办。根据学校的记录,总共有k对人成绩相同,老师必须原创 2020-06-30 23:32:06 · 280 阅读 · 0 评论 -
银河英雄传说----------------------------------思维(并查集)
解析:对于两个战舰之间的战舰的数量用一个数组记录,压缩路径的同时更新这个数组即可#include<bits/stdc++.h>using namespace std;const int N=3e4+1000;int fa[N];int s[N],d[N],a,b;int t;int find(int x){ if(x!=fa[x]) { int root=find(fa[x]); d[x]+=d[fa[x]]; ..原创 2020-05-27 14:44:58 · 157 阅读 · 0 评论 -
程序自动分析-----------------------------思维(并查集+离散化)
解析:由于i,j非常大,所以我们需要离散化一下先把相同的加入到集合中,然后再去找不同的判断不同的两个数是否在同一集合中#include<bits/stdc++.h>using namespace std;const int N=1e6+10;struct node{ int x,y,f; }res[N];int a[N],b[N];int fa[N];int t,n;int find(int x){ if(x!=fa[x]) fa[x]=fin..原创 2020-05-26 16:31:43 · 153 阅读 · 0 评论 -
白魔法师-----------------------思维(dfs+并查集)
解析:dfs维护每一个连通块内部成员数量,取连通块数量最大的一个。然后枚举黑色节点。判断与黑色节点相连的点是不是属于白色连通块里,如果属于那么总的个数就是 size[find(x)]+1 和之前答案取最大值#include<bits/stdc++.h>using namespace std;const int N=1e5+1000;int e[N*2],ne[N*2],idx,h[N];int n,u,v;int fa[N];int size[N];int ans=0;..原创 2020-05-18 20:35:05 · 210 阅读 · 0 评论 -
经商-------------------------------------思维(并查集+dp)
解析:并查集维护和1有关系的点因为每个点只能用一次,那就转换成0/1背包把维护的人数看成物品数,每个物品有两个属性一个体积,一个价值。求体积为C的情况下,获得最大的价值是多少。并查集上跑0/1背包#include<bits/stdc++.h>using namespace std;const int N=1e5+10000;int fa[N],w[N],c[N],...原创 2020-03-13 11:04:31 · 118 阅读 · 0 评论 -
问题 I: 星区划分-------------------------------思维(暴力+并查集)
题目描述爱好天文的小七发明了一种太空分区方法,在这个方法中,宇宙里亮度相近的星星被划为同一个星区。空间中任意相邻或坐标相同的星星若亮度差不大于给定整数M,则这两个星星属于同一星区(两个星星可能会有相同的坐标)。现给你一个空间n个星星的三维坐标(x,y,z),和亮度值v,每个星星的上、下、左、右、前、后的六个相邻位置被认为是与其相邻的。请你计算一下该空间内的星区数量。输入第一行1个正整数n。...原创 2020-03-09 23:29:21 · 133 阅读 · 0 评论 -
D. Segment Tree------思维(难)+排序+set/好
As the name of the task implies, you are asked to do some work with segments and trees.Recall that a tree is a connected undirected graph such that there is exactly one simple path between every pair...原创 2020-01-24 20:48:12 · 179 阅读 · 0 评论 -
小希的迷宫 HDU - 1272---并查集+思维
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的...原创 2020-01-21 23:22:19 · 112 阅读 · 0 评论 -
畅通工程 HDU - 1232 -----并查集
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?Input测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正...转载 2020-01-21 22:12:21 · 100 阅读 · 0 评论 -
X-Plosives UVA - 1160----并查集+思维
A secret service developed a new kind of explosive that attain its volatile property only when a specificassociation of products occurs. Each product is a mix of two different simple compounds, to wh...原创 2020-01-21 21:58:24 · 1159 阅读 · 0 评论 -
Farm Irrigation-----并查集+构图
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of ...原创 2020-01-20 23:51:16 · 118 阅读 · 0 评论 -
How Many Tables----并查集
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...原创 2020-01-19 23:10:43 · 81 阅读 · 0 评论 -
D-City HDU - 4496--------并查集+逆向思维
Luxer is a really bad guy. He destroys everything he met.One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lin...原创 2020-01-18 22:25:57 · 141 阅读 · 0 评论 -
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...原创 2020-01-18 21:15:26 · 110 阅读 · 0 评论 -
D. Secret Passwords---巧用并查集
第二天叫醒我的不是闹钟,是梦想! One unknown hacker wants to get the admin’s password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator’s office a...原创 2019-11-30 23:56:56 · 196 阅读 · 2 评论