Codeforces Round #600 (Div. 2) 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.


并查集+思维;

对每个集合的最大最小值之间的值进行枚举,如果父亲不相同,则连接起来,并且边数加一;

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=200100;
const LL mod=998244353;
int n,m;
int fa[N];
int find(int p){
	if(p==fa[p]) return p;
	return fa[p]=find(fa[p]);
}
int vis[N];
struct Node{
	int mmin,mmax;
}bin[N];
bool cmp(Node p,Node q){
	return p.mmin<q.mmin;
}
bool d[N];
int main(){
//	ios::sync_with_stdio(false);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		bin[i].mmax=0;
		bin[i].mmin=2e9;
		fa[i]=i;
	}
	int u,v;
	int ans=0;//并查集的数量 
	for(int i=1;i<=m;i++){
		scanf("%d%d",&u,&v);
		if(!vis[u]&&!vis[v]){
			ans++;
			vis[u]=vis[v]=ans;
		}
		else if(vis[u]||vis[v]){
			if(vis[u]) vis[v]=vis[u];
			else vis[u]=vis[v];
		}
		if(u>v){
			fa[find(v)]=find(fa[u]);
			bin[vis[u]].mmax=bin[vis[u]].mmax>u?bin[vis[u]].mmax:u;
			bin[vis[u]].mmin=bin[vis[u]].mmin<v?bin[vis[u]].mmin:v;
		}
		else{
			fa[find(u)]=find(fa[v]);
			bin[vis[u]].mmax=bin[vis[u]].mmax>v?bin[vis[u]].mmax:v;
			bin[vis[u]].mmin=bin[vis[u]].mmin<u?bin[vis[u]].mmin:u;
		}
	}
	sort(bin+1,bin+1+ans,cmp);
	int sum=0;
	int last=0;
	for(int i=1;i<=ans;i++){
		int f=find(bin[i].mmin);//整个集合的父亲 
		last=max(last,bin[i].mmin);
		for(int j=last;j<bin[i].mmax;j++){
			if(d[j]) continue;//剪枝 
			d[j]=true;
			if(find(fa[j])!=f){
				if(fa[j]==j){//如果是连接的自己 
					fa[j]=f;
					sum++;
				}
				else{
					if(fa[j]>f){//如果在其他的集合里面 
						fa[find(f)]=find(fa[j]);
						f=find(fa[j]);
					}
					else{
						fa[find(fa[j])]=find(f);
					}
					sum++;
				}
			}
		}
		last=bin[i].mmax;
	}
	printf("%d\n",sum);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值