The Unique MST_生成树2018_3_5

本文介绍了一种算法,用于判断给定的带权无向图的最小生成树是否唯一。通过输入节点数、边数及各边权重,该算法能够确定最小生成树的总成本并检查其唯一性。

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

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!


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=105;
const int inf=1<<30;
int g[N][N],dis[N];
bool vis[N];
int main(){
	int t;
	scanf("%d",&t);
	int n,m,ans;
	while(t--){
		fill(&g[0][0],&g[0][0]+N*N,inf);
		scanf("%d%d",&n,&m);
		while(m--){
			int u,v,d;
			scanf("%d%d%d",&u,&v,&d);
			g[u][v]=g[v][u]=d;
		}
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		dis[i]=g[1][i];
		vis[1]=1;
		int u;
		ans=0;
		int i;
		for(i=1;i<n;i++){
			int mm=inf;
			for(int j=1;j<=n;j++)
			if(!vis[j]&&dis[j]<mm){
				mm=dis[j];u=j;
			}
			if(mm==inf)break;
			int num=0;
			for(int j=1;j<=n;j++)
			if(g[j][u]==mm&&vis[j])
			num++;
			if(num>1)break;
			vis[u]=1;
			ans+=mm;
			for(int j=1;j<=n;j++)
			if(!vis[j]&&g[j][u]<dis[j])
			dis[j]=g[j][u];
		}
		if(i==n)printf("%d\n",ans);
		else puts("Not Unique!");
	}
} 

### 关于无向图最小生成树唯一性的条件 对于无向图的最小生成树(Minimum Spanning Tree, MST),其唯一性取决于边权重的具体分布情况。以下是关于无向图最小生成树唯一性的详细分析: #### 1. **唯一性的基本条件** 当且仅当所有边的权重互不相同,无向图的最小生成树才是唯一的[^1]。这是因为,在这种情况下,任何一种基于贪心策略的算法(如 Kruska 算法或 Prim 算法)都会按照固定的顺序选取边,从而得到相同的最小生成树。 #### 2. **存在多重权重的情况** 如果某些边具有相同的权重,则可能存在多种不同的最小生成树。具体来说,假设一条未使用的边 \( e \) 连接了两个顶点 \( a \) 和 \( b \),将其加入当前的最小生成树并移除原树中连接 \( a \) 和 \( b \) 的路径上的某条边 \( f \)[^2]。此时,若新生成树的总权重等于原始最小生成树的总权重,则表明该最小生成树并非唯一。 #### 3. **Kruskal 算法下的验证方法** 利用 Kruskal 算法可以进一步探讨最小生成树的唯一性问题。在执行过程中,每当遇到两条或多条权重相等的候选边时,选择其中任意一条可能导致最终生成的不同最小生成树[^3]。因此,只有当这些候选边的选择不影响整体结构及其总权重时,才能认为最小生成树是唯一的。 ```python def is_mst_unique(edges, n_vertices): """ 判断给定边集是否能形成唯一的最小生成树 参数: edges (list): 边列表 [(u,v,w), ...], u/v 表示节点编号, w 是权重. n_vertices (int): 图中的顶点数 返回: bool: 是否唯一 """ from collections import defaultdict parent = list(range(n_vertices)) def find(u): while parent[u] != u: parent[u] = parent[parent[u]] u = parent[u] return u def union(u, v): pu, pv = find(u), find(v) if pu == pv: return False parent[pu] = pv return True sorted_edges = sorted(edges, key=lambda x:x[2]) mst_weight = 0 count = 0 unique_check = set() for u, v, weight in sorted_edges: if union(u, v): mst_weight += weight unique_check.add(weight) count += 1 if count >= n_vertices - 1: break # 验证是否存在其他可能组合使得总权重不变但构成不同 alternative_weights_sum = sum([w for _,_,w in sorted_edges[:n_vertices-1]]) return mst_weight == alternative_weights_sum and len(unique_check)==len(sorted(set([e[2] for e in edges]))) ``` 此函数 `is_mst_unique` 可帮助判断特定条件下最小生成树是否唯一。 #### 结论 综上所述,无向图的最小生成树唯一与否主要依赖于边权重的独特性和算法实现过程中的决策自由度。只要每一步操作都严格遵循最优原则,并且不存在可替代选项,则最小生成树必然唯一;反之则可能出现多样性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值