Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)
题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图。
思路:将一个连通块内最大的点做为根,用并查集维护,遍历一遍,对于某个点$i$及该点连通块内的根$fx$,$i$到$fx$内的每一个点,当与$i$不属于一个集合时,进行合并,答案加一,同时更新该连通块的根。
#include <iostream>
#include <algorithm>
#include <cstdio> using namespace std; const int N = ; int fa[N], n, m; void init()
{
for (int i = ; i <= n; i++) fa[i] = i;
} int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
} void nuio(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy) {
if (fx > fy) fa[fy] = fx;
else fa[fx] = fy;
}
} int main()
{
scanf("%d%d", &n, &m);
init();
for (int i = ; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
nuio(x, y);
}
int res = ;
for (int i = ; i <= n; i++) {
int fx = find(i);
while (i < fx) {
int fy = find(i);
if (fx != fy) {
res++;
if (fx > fy) fa[fy] = fx;
else fa[fx] = fy;
fx = max(fx, fy);
}
i++;
}
}
printf("%d\n", res);
return ;
}
Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)的更多相关文章
- Codeforces Round #600 (Div. 2) D题【并查集+思维】
题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集
D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题
E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #345 (Div. 1) C. Table Compression (并查集)
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...
- Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)
题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
随机推荐
- Matlab filter常用函数
Filtering and Analysis Functions Filtering Function Description fftfilt Filters a signal with a digi ...
- SaltStack自动化软件简介及安装
==================================================================================================== ...
- 揭秘jQuery-选择器
先看代码: $(“li”)只选择第一个无序列表中的一个li元素,而不会选择另一个无序列表中的li元素 <!DOCTYPE html> <html> <head> & ...
- springboot中druid监控的配置(DruidConfiguration)
当数据库连接池使用druid 时,我们进行一些简单的配置就能查看到sql监控,web监控,url监控等等. 以springboot为例,配置如下 import com.alibaba.druid.su ...
- win10图标变白的解决办法(亲测有用)
1.首先,随便打开一个文件夹点击[查看]菜单,然后勾选[隐藏的项目]: 2.同时按下快捷键[Win]+[R],在打开的[运行]窗口中输入%localappdata%: 3.在打开的文件夹中,找到[Ic ...
- php对字符串的操作
php最文字的处理很是强大,之前一直云里雾里,这次学习一下. 1,' 与 ”的区别 <?php //双引号中的特殊字符会被解析 echo "你好\t我好";echo &quo ...
- 树链剖分-Hello!链剖-[NOIP2015]运输计划-[填坑]
This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address. http://ww ...
- 小白艰难的Python图像的绘制
1.贪吃蛇 代码: import turtle turtle.setup(650,350) turtle.penup() turtle.fd(-250) turtle.pendown() turtle ...
- pip install 安装指定版本的包
pip install 安装指定版本的包 要用 pip 安装指定版本的 Python 包,只需通过 == 操作符 指定 pip install robotframework==2.8.7 将安装r ...
- 读懂timing report
三部分:表头/launch path /capture path 1.表头 1) 工具版本信息:如示例中的18.10-p001,对某个具体项目timing signoff 工具的版本最好保证一致: 操 ...