题意:对于一张图,如果$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(并查集)的更多相关文章

  1. Codeforces Round #600 (Div. 2) D题【并查集+思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

  2. 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 ...

  3. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  4. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  5. 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, ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)

    题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...

  9. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

随机推荐

  1. drc实现

    原理参考之前转载的matlab上关于DRC的描述. 目前主要实现了compressor和expander. compressor: Limit: expander: 实现代码: #include< ...

  2. mybatis报错:A query was run and no Result Maps were found for the Mapped Statement

    转自:https://blog.csdn.net/u013399093/article/details/53087469 今天编辑mybatis的xml文件,出现如下错误: 程序出现异常[A quer ...

  3. Linux中内容查看命令"大PK"

    众所周知linux中命令cat.more.less均可用来查看文件内容,当然还有我们"非主流"的vim以及使用较少的head.tail.tac. 下面我将介绍各种命令的用法及对比. ...

  4. 使用java实现AES算法的加解密(亲测可用)

    话不多说,直接上代码 import javax.crypto.Cipher;   import javax.crypto.spec.IvParameterSpec; import javax.cryp ...

  5. bootstrap环境

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. go语言 二叉树

    package main import ( "fmt" "reflect" ) type BinaryNode struct { Data interface{ ...

  7. jquery ajax获取后台数据后无法输出

    今天做ajax获取数据,再浏览器的debugger窗口也看到了数据 ajax代码 $('#userSearch').click(function(){ $.get("loadAllUsers ...

  8. IDE - IDEA - tab - 方法相关的移动

    1. 概述 标题可能会改 一个 tab 里方法相关的操作 2. 前提 以默认的模式编辑 tab 对我来说, 就关掉 vim 插件 3. 操作 1. 查看文件结构 概述 唤出当前文件的 结构 唤出后可以 ...

  9. CSS学习(5)更多的选择器

    1.通配符选择器 * 表示选中所有元素 *{color:red;} 2.属性选择器  根据属性名和属性值选中元素 https://developer.mozilla.org/zh-CN/docs/We ...

  10. 基于bs4库的HTML内容查找方法

    一.信息提取实例 提取HTML中所有的URL链接 思路:1)搜索到所有的<a>标签 2)解析<a>标签格式,提取href后的链接内容 >>> import r ...