The Unique MST

本文介绍了一种通过Prim算法判断图是否拥有唯一最小生成树的方法,并提供了完整的代码实现。通过比较未使用的边与生成树中边的最大权重,来确定是否存在另一生成树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!


题解:次小生成树,未优化的普利姆做法。每次把当前的最小生成树的任意两点之间的最大值保存,然后遍历没有用到的边,遍历i与j之间,如果i,j的权等于最小生成树上i->j的最大值,肯定存在第二个生成树,因为我可以删除原来那一条,用现在这一条顶替。


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int INF = 0x3fffffff;

int map[203][203];
bool visited[203];
int d[203];
int w[203][203];  //生成树上两点之间的最大值 
bool s[203][203]; //记录生成树的边 
int pre[203];
int ans;

int max(int a,int b)
{
	return a > b ? a : b;
}

int min(int a,int b)
{
	return a > b ? b : a;
}

bool prim(int n)
{
	memset(visited,false,sizeof(visited));
	memset(w,0,sizeof(w));
	memset(s,false,sizeof(s));
	for(int i = 1;i <= n;i++)
	{
		d[i] = map[1][i];
		w[1][i] = w[i][1] = map[1][i];  
		pre[i] = 1;
	}
	visited[1] = true;
	ans = 0;
	for(int i = 1;i < n;i++)
	{
		int min = INF;
		int k;
		for(int j = 1;j <= n;j++)
		{
			if(!visited[j] && min > d[j])
			{
				min = d[j];
				k = j;
			}
		}
		s[pre[k]][k] = s[k][pre[k]] = true;
		if(min == -1)
		{
			return false;
		}
		ans += min;
		visited[k] = true;
		for(int j = 1;j <= n;j++)
		{
			if(visited[j])  //更新j到其他点的最大值,其他访问了的点和该点的生成树路径上的边已经得到 
			{
				w[j][k] = w[k][j] = max(w[j][pre[k]],map[pre[k]][k]);
			}
			if(!visited[j] && d[j] > map[k][j])
			{
				d[j] = map[k][j];
				pre[j] = k;
			}
		}
	}
	return true;
}

int main()
{
	int n,m;
	int ncase;
	cin>>ncase;
	while(ncase--)
	{
		scanf("%d%d",&n,&m);
		for(int i = 1;i <= n;i++)
		{
			for(int j = 1;j <= n;j++)
			{
				if(i == j)
				{
					map[i][j] = 0;
				}
				else
				{
					map[i][j] = INF;
				}
			}
		}
		int u,v,c;
		for(int i = 0;i < m;i++)
		{
			scanf("%d%d%d",&u,&v,&c);
			map[u][v] = map[v][u] = min(map[u][v],c);
		}
		prim(n);
		bool flag = true;
		for(int i = 1;i <= n;i++)
		{
			for(int j = 1;j <= n;j++)
			{
				if(map[i][j] != INF && !s[i][j] && i != j)
				{
					if(w[i][j] == map[i][j])  //i-j的边(没有在生成树上)等于生成树上i->j路径上的最大值 
					{
						flag = false;
						break;
					}
				}
			}
		}
		if(flag)
		{
			printf("%d\n",ans);
		}
		else
		{
			printf("Not Unique!\n");
		}
	}
	
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值