题意:对于一张图,如果$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. CSS和JS两种颜色渐变文字效果代码

    js实现颜色渐变文字效果代码: <!-- js颜色渐变色文字 --> <div id="moml"> <div style="text-al ...

  2. dbGet (二)

    dbGet是由它基本的语法加上各种object的attribute的组合构成的.大家在熟悉基本语法之后,就应该去学习各个object的attribute了.说实话,这很难,因为attribute很多, ...

  3. MFC单文档视图中嵌入GLFW窗口

    开始学习OpenGL由于有一段时间,但是glfw只有窗口区,虽然通过某种手段(移步这里)可以加入工具栏,但仍然无法作为一个标准的GUI,而直接在MFC或Qt里面使用OpenGL API感觉有诸多制肘, ...

  4. hz和s和脉冲

    先弄清楚定义,HZ是频率的单位,而s是周期的单位:而f=1/T. 故1hz=1s:5hz=1/5=0.02s; 占空比    占空比(Duty Ratio)在电信领域中有如下含义:    在一串理想的 ...

  5. MySQL必知必会(第4版)整理笔记

    参考书籍: BookName:<SQL必知必会(第4版)> BookName:<Mysql必知必会(第4版)> Author: Ben Forta 说明:本书学习笔记 1.了解 ...

  6. Hadoop 启动/停止集群和节点的命令

    集群启动/停止Hadoop集群:start-all.sh    stop-all.sh 1) 启动NameNode, DataNode 2) 启动JournalNode, JournalNode在hd ...

  7. .net core 通过代码创建数据库表

    0.结构: 1.API using System; using System.Collections.Generic; using System.IO; using System.Linq; usin ...

  8. async 异步协程进阶

    协程通过 async/await 语法进行声明,是编写异步应用的推荐方式 例如新定义一个协程(coroutine object): async def foo(): return 42 首先先来介绍下 ...

  9. laravel执行数据库迁移的过程中出现Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Operation timed out (SQL: select * from information_schema.tables where table_schema = shop and table_name = migrations

    向customers表添加字段phone php artisan make:migration add_phone_to_customers_table 问题: 解决方法: 将DB_HOST配置项修改 ...

  10. Speech Bandwidth Extension With WaveNet

    利用WAVENET扩展语音带宽 作者:Archit Gupta, Brendan Shillingford, Yannis Assael, Thomas C. Walters 博客地址:https:/ ...