You’re given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n.

The graph is considered harmonious if and only if the following property holds:

For every triple of integers (l,m,r) such that 1≤l<m<r≤n, if there exists a path going from node l to node r, then there exists a path going from node l to node m.
In other words, in a harmonious graph, if from a node l we can reach a node r through edges (l<r), then we should able to reach nodes (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 n and m (3≤n≤200 000 and 1≤m≤200 000).

The i-th of the next m lines contains two integers ui and vi (1≤ui,vi≤n, ui≠vi), that mean that there’s an edge between nodes u and v.

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 12
Output
1
Input
200000 3
7 9
9 8
4 5
Output
0
Note
In the first example, the given graph is not harmonious (for instance, 1<6<7, node 1 can reach node 7 through the path 1→2→7, but node 1 can’t reach node 6). However adding the edge (2,4) is sufficient to make it harmonious.

In the second example, the given graph is already harmonious.

#pragma warning(disable:4996)
#include"iostream"
#include"functional"
#include"algorithm"
#include"cstring"
#include"stack"
#include"cmath"
#include"queue"
#include"vector"
#include"map"
typedef long long int ll;
using namespace std;
ll fa[9999999];
ll find(ll a){if(a==fa[a]) return a;else{return fa[a]=find(fa[a]);}
}
int main(){ll a,b;cin>>a>>b;for(int i=1;i<=a;i++) fa[i]=i;for(int i=0;i<b;i++){ll c,d;cin>>c>>d;ll fc=find(c),fd=find(d);if(fc<fd) fa[fc]=fd;else if(fc>fd){fa[fd]=fc;}}ll ans=0;for(int i=1;i<=a;i++){ll k=find(i);while(k>i){ll j=find(i);if(j!=k){ans++;fa[min(j,k)]=max(j,k);k=max(k,j);}i++;}}cout<<ans;
}

Harmonious Graph (并查集 —父亲为最大值)相关推荐

  1. Harmonious Graph(并查集)

    思路 题意:n个点,m条边,组成若干个连通图,如果一个点x能到另一个点y,那么如果(x,y)中任意一个点,都能到y,就称这个图为和谐图,求需要连多少个边,使得所有连通图成为和谐图 做法:用并查集存下所 ...

  2. 牛客多校3 - Operating on a Graph(并查集+链表合并)

    题目链接:点击查看 题目大意:给出一个由 n 个点和 m 条边组成的无向图,每个点初始时都有颜色 i ,i ∈ [ 0 , n - 1 ] ,接下来有 q 次操作,每次操作会给出一种颜色 x ,分两种 ...

  3. PAT甲级1114 Family Property:[C++题解]结构体、并查集、测试点3、4、5有问题的进来!!

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 先建边.读入每家的信息,在本人和父母(如果有的话),本人与子女(如果有的话)之间分别建边.边用结构体来存,边记录两个端点. 遍历每条 ...

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

  5. D. Harmonious Graph【并查集】

    You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...

  6. 图论500题 ---- 并查集求路径上最大值最小不超过K的点对数 HDU Portal

    题目链接 题目大意: 就给你一个图,qqq次询问,问你这个图上有多少对点之间的所以路径上的最大值的最小值不超过kkk? 解题思路: 首先我们知道这本质上就是求两个点联通的路径上的最大值最小是多少? 那 ...

  7. 【CF#505B】Mr. Kitayuta's Colorful Graph (并查集或Floyd或BFS)

    题干: Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The verti ...

  8. 图论 + 并查集 ----最小生成树重构图 + 可撤销并查集 + set启发式合并 时间线上的离线求解 D. Graph and Queries

    解题思路 题目大意: 就是给你一个无向图,每个点都有一个权值,和qqq次询问 每次询问有两种操作 1 x:就询问从x点出发,能访问到的最大权值是多少,并把最大权值那个点的权值设置为0 2 x:就是删除 ...

  9. Graph Destruction 并查集,图论(500)

    题意 : 给一n个点m条边的无向图,问逐渐删去1-n点及它们的边,分别输出当前剩下多少个连通区域 思路 : 求连通块,想到了并查集,但并查集是增边,这里是减边,只要倒序考虑问题即可,那么问题转换为 : ...

最新文章

  1. 清理SQL多余登录信息
  2. 服务器证书安装配置指南(Nginx)-天威诚信
  3. GRDB使用SQLite的WAL模式
  4. 如何快速阅读一篇英文文献
  5. 【Groovy】Groovy 脚本调用 ( Groovy 配置文件格式 | Groovy 配置文件读取 | 完整配置文件及解析代码示例 )
  6. 再理解RankNet算法
  7. CodeForces - 1535E Gold Transfer(树上倍增+交互)
  8. P6015-[CSGRound3]游戏【树状数组】
  9. [渝粤教育] 江西外语外贸职业学院 商务英语翻译技巧 参考 资料
  10. 飞鸽传书网站最新改版
  11. date对象加十分钟_js面向对象-这样学很轻松
  12. 我的ActiveRecord学习之路(一)
  13. 韦东山linux嵌入式学习之路
  14. 惠普服务器故障代码_惠普服务器常见问题及故障排除
  15. 卷积神经网络结构图绘制,卷积神经网络图怎么画
  16. android webview静态方法,在android webview中加载静态页面
  17. java实现根据高德地图API接口进行地址位置解析,将地址转化为经纬度
  18. Edge浏览器缓存问题简直可怕
  19. Python发送邮件报错554,求解答。
  20. eclipse列名无效_java – SQLException:列名无效……?

热门文章

  1. 网站哪些功能可以提高用户体验度?
  2. 计算机a类论文有哪些,a类核心期刊有哪些
  3. 小习题:猴子吃桃问题
  4. 阿里云服务器Intel Xeon Platinum 8269CY(Cascade Lake)处理器CPU性能评测
  5. 2021届Java开发求职-------面试实战之Vivo提前批
  6. 采购订单历史表EKBE和物料凭证表MSEG的关系
  7. echarts饼图设置每个扇形的颜色
  8. Oracle练习题(九)
  9. python-猜数字游戏
  10. 设计模式:里氏替换原则(详解)