Harmonious Graph
You're given an undirected graph with nn nodes and mm edges. Nodes are numbered from 11 to nn.
The graph is considered harmonious if and only if the following property holds:
- For every triple of integers (l,m,r)(l,m,r) such that 1≤l<m<r≤n1≤l<m<r≤n, if there exists a path going from node ll to node rr, then there exists a path going from node ll to node mm.
In other words, in a harmonious graph, if from a node ll we can reach a node rr through edges (l<rl<r), then we should able to reach nodes (l+1),(l+2),…,(r−1)(l+1),(l+2),…,(r−1) too.
What is the minimum number of edges we need to add to make the graph harmonious?
Input
The first line contains two integers nn and mm (3≤n≤200 0003≤n≤200 000 and 1≤m≤200 0001≤m≤200 000).
The ii-th of the next mm lines contains two integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi), that mean that there's an edge between nodes uu and vv.
It is guaranteed that the given graph is simple (there is no self-loop, and there is at most one edge between every pair of nodes).
Output
Print the minimum number of edges we have to add to the graph to make it harmonious.
Examples
input
14 8 1 2 2 7 3 4 6 3 5 7 3 8 6 8 11 12output
1input
200000 3 7 9 9 8 4 5output
0
假如1->2>7就要让1能到1->3,1->4,1->5,1->6,假如1->3,1->7被连接,也就是3->7也连接了,所以如果用并查集维护的时候,父节点相同的就是连接的点。我们要使得,每个集合中最大的点为根(设为k),其余节点分别为:1-k-1。也就是说每个并查集必须含有(1-k)的所有整数值。
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define per(i, a, b) for(int i = a; i >= b; --i)
using namespace std;const int N = 200010;int p[N];int find(int x){if(p[x] == x) return x;else return p[x] = find(p[x]);
}int main()
{int n,m; cin >> n >> m;rep(i,1,n) p[i] = i;rep(i,1,m){int a,b; cin >> a >> b;int px = find(a),pb = find(b);
// 让大的数为父亲if(px < pb){p[px] = pb;}else {p[pb] = px;}}int t = 1,maxn = 1,ans = 0;while(t <= n){maxn = find(t);rep(i,t+1,maxn){//要让t,到maxn间的值为一个集合int x = find(i);if(x != maxn){ans++;if(x > maxn){p[maxn] = x;maxn = x;// 集合应该是更大的范围。}else{p[x] = maxn;// 合并两集合}}}t = maxn + 1;}cout << ans << endl;return 0;
}
Harmonious Graph相关推荐
- Harmonious Graph CodeForces - 1253D(思维+并查集)
You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...
- D. Harmonious Graph【并查集】
You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...
- D. Harmonious Graph
题目:D. Harmonious Graph 总结:将每一个连通块的所有点都指向该连通图的最大点.从1点开始遍历,假设fa[1] = x;那么从2~(x-1)的所有点的根结点都一定会指向x.(是在一个 ...
- Harmonious Graph (并查集 —父亲为最大值)
You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...
- Harmonious Graph(并查集)
思路 题意:n个点,m条边,组成若干个连通图,如果一个点x能到另一个点y,那么如果(x,y)中任意一个点,都能到y,就称这个图为和谐图,求需要连多少个边,使得所有连通图成为和谐图 做法:用并查集存下所 ...
- CF 1253D-Harmonious Graph-并查集+贪心
Description You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to ...
- 图融合GCN(Graph Convolutional Networks)
图融合GCN(Graph Convolutional Networks) 数据其实是图(graph),图在生活中无处不在,如社交网络,知识图谱,蛋白质结构等.本文介绍GNN(Graph Neural ...
- Graph Representation 图神经网络
Graph Representation 图神经网络 图表示学习(representation learning)--图神经网络框架,主要涉及PyG.DGL.Euler.NeuGraph和AliGra ...
- TVM 图优化Graph Optimization
TVM 图优化Graph Optimization Codegen
最新文章
- tictoc正方形网络模型_Trick | 分类网络Trick大汇总
- 改善前端优化的有用技巧
- Ubuntu14.04安装Octave
- Boost在Linux和windows下的编译 32位 64位
- updating error reports database解决方案
- python列表添加元组_python 列表与元组的操作简介
- Array为什么这样会有错?
- 计算机网络领悟摩尔定,计算机网络等133信息技术展望P15-P17.PPT
- html内容超出不自动滚动,16.css: overflow使用 例: 固定div大小,不让内容超出div
- 基础集合论 第二章 9 族
- MyCat的zookeeper集群模式总结
- 硬件工程师成长之路(10.1)——芯片选型
- 计算机网络原理综合实验
- 微信公众号排版方法及相关素材网站
- 社区拼团赛道的突然火爆,究竟是受何因素影响?
- php 数字英文中文版,php过滤只保留中文,英文以及数字的方法
- word文档页眉清除和页码设置
- 医疗保险,公积金、养老、生育、工伤、失业保险
- 豆瓣上的一条关于泰坦尼克的影评1
- ios7 未能连接到服务器,完美解决iTunes 未能连接到此 iPhone。发生未知错误(0xe8000003)和0xe8000084...