The Unique MST

本文介绍了一种算法,用于判断给定的无向连通图是否拥有唯一最小生成树(MST)。通过使用Kruskal算法变体,文章详细阐述了如何检查MST的唯一性,并提供了一个C++实现示例。

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

The Unique MST

http://poj.org/problem?id=1679

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36744 Accepted: 13395

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!

Source

 

模板题

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<string>
  6 #include<cstring>
  7 #include<vector>
  8 #include<queue>
  9 #define lson l,mid,rt<<1
 10 #define rson mid+1,r,rt<<1|1
 11 #define N 250500
 12 #define MOD 1e9+7
 13 #define INF 0x3f3f3f3f
 14 typedef long long ll;
 15 using namespace std;
 16 struct sair{
 17     int x,y,v;
 18 }a[100005];
 19 int fa[1005];
 20 int n,m;
 21 bool cmp(sair a,sair b){
 22     return a.v<b.v;
 23 }
 24 
 25 int Find(int x){
 26     int r=x,y;
 27     while(x!=fa[x]){
 28         x=fa[x];
 29     }
 30     while(x!=r){
 31         y=fa[r];
 32         fa[r]=x;
 33         r=y;
 34     }
 35     return x;
 36 }
 37 
 38 int join(int x,int y){
 39     int xx=Find(x);
 40     int yy=Find(y);
 41     if(xx==yy){
 42         return 0;
 43     }
 44     fa[xx]=yy;
 45     return 1;
 46 }
 47 
 48 vector<int>v;
 49 
 50 
 51 int check(int xxx){
 52     int ans=0;
 53     int xxxx=1;
 54     for(int i=1;i<=n;i++){
 55         fa[i]=i;
 56     }
 57     for(int i=1;i<=m;i++){
 58         if(i!=xxx){
 59             if(join(a[i].x,a[i].y)){
 60                 ans+=a[i].v;
 61                 xxxx++;
 62             }
 63         }
 64     }
 65     if(xxxx==n)
 66         return ans;
 67     return -1;
 68 }
 69 
 70 int main(){
 71     std::ios::sync_with_stdio(false);
 72     int t;
 73     cin>>t;
 74     while(t--){
 75         cin>>n>>m;
 76         for(int i=1;i<=n;i++){
 77             fa[i]=i;
 78         }
 79         for(int i=1;i<=m;i++){
 80             cin>>a[i].x>>a[i].y>>a[i].v;
 81 
 82         }
 83         int ans1=0;
 84         v.clear();
 85         sort(a+1,a+m+1,cmp);
 86         for(int i=1;i<=m;i++){
 87             if(join(a[i].x,a[i].y)){
 88                 ans1+=a[i].v;
 89                 v.push_back(i);
 90             }
 91         }
 92         int flag=1;
 93         for(int i=0;i<v.size();i++){
 94             if(check(v[i])==ans1){
 95                 flag=0;
 96                 break;
 97             }
 98         }
 99         if(flag){
100             cout<<ans1<<endl;
101         }
102         else{
103             cout<<"Not Unique!"<<endl;
104         }
105     }
106 }
View Code

 

转载于:https://www.cnblogs.com/Fighting-sh/p/9740837.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值