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
using namespace std;

struct Edge{
	int u,v,w;
	bool operator < (Edge x){
		return w>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<

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

#include
using namespace std;

struct Edge{
	int u,v,w;
	bool operator < (Edge x){
		return w>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<

 

你可能感兴趣的:(MST,Codeforces)