The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 34518 | Accepted: 12600 |
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!
Source
POJ Monthly--2004.06.27 srbga@POJ
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 0x3f3f3f3f
int map[110][110];//用邻接矩阵存图
int used[110][110];//表示边是否被用过,因为求次小生成树时候要把边去掉然后再加另一条边替换
int pre[110];//记录与i点相连的点
int lowcost[110];//表示顶点到最小生成树的最小权值
int visit[110];//标记了节点是否加入了最小生成树
int Max[110][110];//记录i到j之间加入到生成树中的最大的权值边
int n;
int prim(){//用prim算法求出最小权值
int sum=0;
memset(used,0,sizeof(used));
memset(visit,0,sizeof(visit));
memset(Max,0,sizeof(0));
for(int i=1;i<=n;i++){
lowcost[i]=map[1][i];
pre[i]=1;
}
pre[1]=0;
visit[1]=1;
lowcost[1]=0;
for(int i=2;i<=n;i++){
int min=maxn;
int minn=0;
for(int j=1;j<=n;j++){
if(!visit[j]&&lowcost[j]<min){
min=lowcost[j];
minn=j;
}
}
sum+=min;
visit[minn]=1;
used[minn][pre[minn]]=1;
used[pre[minn]][minn]=1;
for(int j=1;j<=n;j++){
if(visit[j]){
Max[j][minn]=max(Max[pre[minn]][j],lowcost[minn]);
Max[minn][j]=max(Max[pre[minn]][j],lowcost[minn]);
}
if(!visit[j]&&lowcost[j]>map[minn][j]){
lowcost[j]=map[minn][j];
pre[j]=minn;
}
}
}
return sum;
}
int smst(int n,int sum){//sum是最小生成树的权值,此函数的作用是求次小生成树的权值
int ans=maxn;
for(int i=1;i<=n;i++){//枚举最小生成树之外的边
for(int j=i+1;j<=n;j++){
if(map[i][j]!=maxn&&!used[i][j]){//如果有边并且此条边没有在最小生成树内
ans=min(ans,sum+map[i][j]-Max[i][j]);//加入一条边之后一定构成环,需要去掉该环中新加入的边中最长的一条
}
}
}
if(ans==maxn)
return -1;
return ans;
}
int main(){
int T,m,xi,yi,wi;
scanf("%d",&T);
while(T--){
memset(map,0,sizeof(map));
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]=maxn;
}
}
for(int i=0;i<m;i++){
scanf("%d %d %d",&xi,&yi,&wi);
map[xi][yi]=wi;
map[yi][xi]=wi;
}
int ans=prim();
if(ans==smst(n,ans)){
printf("Not Unique!\n");
}
else{
printf("%d\n",ans);
}
}
return 0;
}