The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23309 | Accepted: 8268 |
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'.
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!题意:给一个无向图,判断最小生成树是否唯一,若唯一则输出最小花费,不唯一输出"Not Unique!"。
思路:最小生成树是否唯一,也就是判断次小生成树花费是否等于最小生成树。
对于次小生成树,我习惯用prim来求,每次添加一个新的结点时,更新一遍这个点到所有已添加结点的路径上的最大值,并保存下来。求出最小生成树后,遍历每条不在其中的边,添加这条边,并删除这条边两个节点在最小生成树中路径上的最大边值,再求最小值,最终最小值就是次小生成树。
但是这题没必要求出次小生成树,只需要判断不在最小生成树中的边是否等于最小生成树中相同结点路径上的最大边值即可。
代码:
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define maxn 110
#define inf 0x3f3f3f3f
int n,m;
int d[maxn][maxn];
int max1[maxn][maxn];
bool g[maxn][maxn];
int prim(){
bool vis[maxn];
memset(vis,false,sizeof(vis));
memset(max1,0,sizeof(maxn));
vector<int> vec;
vec.push_back(1);
vis[1] = true;
int sum = 0;
while(true){
int u,v,temp=inf;
for(int i=0;i<vec.size();i++){
int p = vec[i];
for(int j=1;j<=n;j++){
if(!vis[j] && d[p][j] < temp){
u = p,v = j;
temp = d[p][j];
}
}
}
if(temp==inf) return sum;
vis[v] = true;
g[u][v] = g[v][u] = false;
sum += temp;
for(int i=0;i<vec.size();i++){
int p = vec[i];
max1[v][p] = max1[p][v] = max(max1[u][p],temp);
}
vec.push_back(v);
}
}
int main(){
int T,a,b,c;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
bool flag = false;
memset(g,false,sizeof(g));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
d[i][j] = inf;
}
}
for(int i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
d[b][a] = d[a][b] = c;
g[a][b] = true;
g[b][a] = true;
}
int res = prim();
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(g[i][j] && d[i][j] == max1[i][j]){
flag = true;
break;
}
}
if(flag) break;
}
if(flag) printf("Not Unique!\n");
else printf("%d\n",res);
}
return 0;
}