Kruskal次小生成树 :The Unique MST

本文探讨如何通过最小生成树算法判断给定无向图是否有唯一的最小生成树,涉及图论基础知识及算法实现。
The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14402 Accepted: 4981

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!

题意是问有没有耗费相同的不同的最小生成树

此题的数据都是连通的,先最小生成树一次,把树里每条边标记,然后依次删除来看新生成的最小树的耗费是否与原来相同,相同则不唯一

唯一要注意的是删边之后要注意判断这次图还连不连通,如果不连通那这次得到的结果就不能拿来比较

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

const long long INF = 1 << 31 -1;
int p[11000];
long long mm;
long long tmp;
int n, m;
int fflag;
int del;

struct edge{
    int u;
    int v;
    long long w;
    int flag;
}e[10001000];

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

int cmp(edge a, edge b){
    return a.w < b.w;
}

int Kruskal(){
    long long ans = 0;
    int i;
    for(i = 1; i <= n; i ++)
        p[i] = i;
    sort(e + 1, e + m + 1, cmp);
    for(i = 1; i <= m; i ++){
        int x = find(e[i].u);
        int y = find(e[i].v);
        if(x != y){
            e[i].flag = 1;
            ans += e[i].w;
            p[x] = y;
        }
    }
    return ans;
}

long long Kruskalm(){
    long long ans = 0;
    int i;
    for(i = 1; i <= n; i ++)
        p[i] = i;
    sort(e + 1, e + m + 1, cmp);
    int j = 0;
    fflag = 0;
    for(i = 1; i <= m; i ++){
        if(i == del)
            continue;
        int x = find(e[i].u);
        int y = find(e[i].v);
        if(x != y){
            ans += e[i].w;
            p[x] = y;
            j ++;
        }
    }
    if(j < n - 1) //如果最后生成树的边数小于n-1,显然就是因为删边之后不连通了,所以这次的结果直接赋为无穷大,相当于不行
        ans = INF;
    return ans;
}

int main(){
    int t, i, tt;
    cin >> t;
    while(t --){
        int ff = 0;
        scanf("%d %d", &n ,&m);
        for(i = 1; i <= m; i ++){
            scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
            e[i].flag = 0;
        }
        mm = Kruskal();
        for(i = 1; i <= m; i ++){
            if(e[i].flag){
                //tt = e[i].w;
                del = i;
                tmp = Kruskalm();
                if(tmp == mm){
                    ff = 1;
                    break;
                }
            }
        }
            if(ff){
                printf("Not Unique!\n");
            }
            else
                printf("%I64d\n", mm);
    }
    return 0;
}


### 关于无向图最小生成树唯一性的条件 对于无向图的最小生成树(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` 可帮助判断特定条件下最小生成树是否唯一。 #### 结论 综上所述,无向图的最小生成树唯一与否主要依赖于边权重的独特性和算法实现过程中的决策自由度。只要每一步操作都严格遵循最优原则,并且不存在可替代选项,则最小生成树必然唯一;反之则可能出现多样性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值