D. Harmonious Graph【并查集】

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
inputCopy
14 8
1 2
2 7
3 4
6 3
5 7
3 8
6 8
11 12
outputCopy
1

inputCopy
200000 3
7 9
9 8
4 5
outputCopy
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.

//package Main;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

public class Main {
	
	static int maxn = 1000010; 
	static int[] mi = new int[maxn];
	static int[] mx = new int[maxn];
	static int[] pre = new int[maxn];
	static class Point implements Comparable<Point>{
		int mi,mx;
		public Point(){}
		public Point(int mi,int mx){
			this.mi=mi;
			this.mx=mx;
		}
		public int compareTo(Point a){
			if(this.mi-a.mi!=0) return this.mi-a.mi;
			return this.mx-a.mx;
		}
	};
	static Point[] p = new Point[maxn];
	public static int find(int x) {
		if(pre[x]==x) return x;
		return pre[x]=find(pre[x]);
	}
	public static void unite(int a,int b){
		int fx=find(a);
		int fy=find(b);
		if(fx!=fy) pre[fx]=fy;
	}
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int m = input.nextInt();
		for(int i=1;i<=n;i++){
			pre[i]=i;
			mi[i]=n+1;
			p[i]=new Point();
		}
		for(int i=1;i<=m;i++){
			int a=input.nextInt();
			int b=input.nextInt();
			unite(a,b);
		}
		for(int i=1;i<=n;i++){
			int fi=find(i);
			mi[fi]=Math.min(mi[fi],i);
			mx[fi]=Math.max(mx[fi],i);
		}
		int cnt=0;
		for(int i=1;i<=n;i++){
			if(pre[i]==i){
				cnt++;
				p[cnt].mi=mi[i];
				p[cnt].mx=mx[i];
			}
		}
		Arrays.sort(p,1,cnt+1);
		int fi=p[1].mi,fx=p[1].mx,ans=0;
		for(int i=2;i<=cnt;i++){
			if(p[i].mi<fx){
				ans++;
				fx=Math.max(fx,p[i].mx);
			}
			else{
				fi=p[i].mi;
				fx=p[i].mx;
			}
		}
		System.out.println(ans);
	}
}

发布了444 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zt2650693774/article/details/103109116