CodeForces - 1108F MST Unification

http://codeforces.com/contest/1108/problem/F

You are given an undirected weighted connected graph with nn vertices and mm edges without loops and multiple edges.

The ii -th edge is ei=(ui,vi,wi)ei=(ui,vi,wi) ; the distance between vertices uiui and vivi along the edge eiei is wiwi (1≤wi1≤wi ). The graph is connected, i. e. for any pair of vertices, there is at least one path between them consisting only of edges of the given graph.

A minimum spanning tree (MST) in case of positive weights is a subset of the edges of a connected weighted undirected graph that connects all the vertices together and has minimum total cost among all such subsets (total cost is the sum of costs of chosen edges).

You can modify the given graph. The only operation you can perform is the following: increase the weight of some edge by 11 . You can increase the weight of each edge multiple (possibly, zero) times.

Suppose that the initial MST cost is kk . Your problem is to increase weights of some edges with minimum possible number of operations in such a way that the cost of MST in the obtained graph remains kk , but MST is unique (it means that there is only one way to choose MST in the obtained graph).

Your problem is to calculate the minimum number of operations required to do it.

Input

The first line of the input contains two integers nn and mm (1≤n≤2⋅105,n−1≤m≤2⋅1051≤n≤2⋅105,n−1≤m≤2⋅105 ) — the number of vertices and the number of edges in the initial graph.

The next mm lines contain three integers each. The ii -th line contains the description of the ii -th edge eiei . It is denoted by three integers ui,viui,vi and wiwi (1≤ui,vi≤n,ui≠vi,1≤w≤1091≤ui,vi≤n,ui≠vi,1≤w≤109 ), where uiui and vivi are vertices connected by the ii -th edge and wiwi is the weight of this edge.

It is guaranteed that the given graph doesn't contain loops and multiple edges (i.e. for each ii from 11 to mm ui≠viui≠vi and for each unordered pair of vertices (u,v)(u,v) there is at most one edge connecting this pair of vertices). It is also guaranteed that the given graph is connected.

Output

Print one integer — the minimum number of operations to unify MST of the initial graph without changing the cost of MST.

 

在此强烈推荐这篇题解:https://blog.csdn.net/Floraqiu/article/details/86630053

思路非常清晰。

大致就是:按照Kruskal的方式,对于边权相等的边,首先边的两点已连接的就不算了,未连接的选择最多的边使其不成环,剩下一些冲突的就要+1,+1后也不会影响以后,因为这条边加上就成环了。复杂度是排序的O(mlogm)。

应该Prim也能做。

#include<bits/stdc++.h>
using namespace std;

struct Edge{
	int u,v,w;
	bool operator < (Edge x){
		return w<x.w;
	}
}e[200000+1000];
int n,m,p[200000+1000],ans;

int findset(int x){return x==p[x]?x:p[x]=findset(p[x]);}

int main()
{
//	freopen("input.in","r",stdin);
	cin>>n>>m;
	for(int i=1;i<=m;i++)cin>>e[i].u>>e[i].v>>e[i].w;
	sort(e+1,e+1+m);
	for(int i=1;i<=n;i++)p[i]=i;
	for(int i=1;i<=m;)
	{
		int j=i;
		while(j+1<=m&&e[i].w==e[j+1].w)j++;
		int num=0;
		for(int k=i;k<=j;k++)if(findset(e[k].u)!=findset(e[k].v))num++;
		for(int k=i;k<=j;k++)
		{
			int x=findset(e[k].u),y=findset(e[k].v);
			if(x!=y)p[x]=y,num--;
		}
		ans+=num;
		i=j+1;
	}
	cout<<ans<<endl;
	return 0;
}

下面是O(n^2)的TLE代码

#include<bits/stdc++.h>
using namespace std;

struct Edge{
	int u,v,w;
	bool operator < (Edge x){
		return w<x.w;
	}
}e[200000+1000];
int n,m,p[200000+1000],ans,circle[200000+1000];

int findset(int x){return x==p[x]?x:p[x]=findset(p[x]);}

int main()
{
//	freopen("input.in","r",stdin);
	cin>>n>>m;
	for(int i=1;i<=m;i++)cin>>e[i].u>>e[i].v>>e[i].w;
	sort(e+1,e+1+m);
	for(int i=1;i<=n;i++)p[i]=i;
	for(int i=1;i<=m;i++)
	{
		if(circle[i])continue;
		for(int j=i;j<=m&&e[j].w==e[i].w;j++)
			if(findset(e[j].u)==findset(e[j].v))circle[j]=1;
		if(circle[i])continue;

		int x=findset(e[i].u),y=findset(e[i].v);
		p[x]=y;
		for(int j=i+1;j<=m&&e[j].w==e[i].w;j++)
			if(findset(e[j].u)==findset(e[j].v)&&circle[j]==0)circle[j]=1,ans++;
	}
	cout<<ans<<endl;
	return 0;
}

 

全部评论

相关推荐

“无名小卒,还是名扬天下?”我知道很多人都不觉得我能走到今天这一步,当然,也包括我自己。在我的人生里,有两部作品刻下了最深的烙印:《斗破苍穹》与《龙族》。它们总被人拿来对照:一边是萧炎的桀骜轻狂,一边是路明非的怯懦衰颓。有人说,天蚕土豆没见过魂天帝,但江南见过真凯撒。我时常觉得,自己就是那个衰小孩路明非。可路明非可以开挂,我不可以;我也无数次幻想过,能拥有萧炎那般年少轻狂的人生,可我没有他与生俱来的逆天天赋。我只是个平庸的普通人,一个看过《斗破苍穹》却开不了挂的路明非,只能一步一步往上爬。从我下定决心找实习的那一刻起,我就给自己定下了目标:“我一定要为字节跳动卖命.jpg”。萧炎有他的三年之约,我有我的两年半之约(其实是一年半)。2024.11.20,科大讯飞的第一封实习offer落进邮箱,我迈出了这场奔赴的第一步。2025.8.18,放弃百度转正的安稳机会,转身走进前路未卜的不确定里。2025.11.14,我选择走进字节跳动,以实习生的身份重新出发。2026.3.25&nbsp;-&nbsp;3.31,一周速通上海飞书,幸遇赏识我的伯乐,斩获Special&nbsp;Offer。被告知面试通过的那一刻,我的内心无比平静,就像这个offer本就该属于我。不是侥幸,是应得的。这一路,有人看轻过我的出身,不相信我能走到这里;也有人在我看不见前路的时候,替我举过灯。没有他们的鼓励与支撑,就没有今天站在这里的我。我看到了自强不息的激荡,那是一个双非的伟大乐章!我是雨夜迈巴赫,我要开启属于我的新篇章了。
在看牛客的本杰明很勇...:真心祝贺l总 我永远的偶像 我滴神
春招至今,你收到几个面试...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务