The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 30841 Accepted: 11086
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
POJ Monthly–2004.06.27 srbga@POJ
引用别人博客里的话:
1.先求出来最小生成树。并将最小生成树任意两点之间路径当中的权值最大的那一条找出来,为什么要找最大的呢,因为生成树加入一条边之后一定构成了回路,那么肯定要去掉这个回路当中一条边才是生成树,那么,怎么去边才是次小的,那就去掉除了刚刚添加的一条边之外回路当中权值最大的一个,所以留下的就是最小的。
2.枚举最小生成树外的每一条边。找出最小的就是次小生成树。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define maxn 105
using namespace std;
const int inf=0x3f3f3f3f3f;//不止是数组赋初值就这样写,写成宏定义数组和int变量赋值不一样。。。
int maxe[maxn][maxn];
int used[maxn][maxn];
int n,m;
int pre[maxn];
int map[maxn][maxn];
void init()
{
memset(map,inf,sizeof(map));
for(int i=0;i<n;i++)map[i][i]=0;
memset(used,0,sizeof(used));
memset(maxe,0,sizeof(used));
for(int i=0;i<n;i++)pre[i]=i;
}
int prim()
{
int mindis;
int dis[maxn];
int vis[maxn];
int ans=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
dis[i]=map[i][1];
pre[i]=1;//初始化前置节点都为1,从1开始找边
}
dis[1]=0;
for(int i=0;i<n;i++)
{
mindis=inf;
int k=-1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&lowdis>dis[j])
{
lowdis=dis[j];
k=j;
}
}
vis[k]=1;
ans+=lowdis;
used[pre[k]][k]=used[k][pre[k]]=1;
if(k==-1)return -1;
for(int j=1;j<=n;j++)
{
if(!vis[j])
{
if(dis[j]>map[k][j])//注意不要一不留神写成了dijkstra
{
dis[j]=map[k][j];
pre[j]=k;
}
}
else maxe[k][j]=maxe[j][k]=max(dis[k],maxe[j][pre[k]]);//记录k到j间的最大边
}
}
return ans;
}
int ans;
int umst()//替换掉i到 j间的最大边,看能不能得到相同的结果。如果能最小生成树就不是唯一的
{
int mindis=inf;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(!used[i][j]&&map[i][j]!=inf)
{
mindis=min(mindis,ans-maxe[i][j]+map[i][j]);
}
}
}
//if(mindis==inf)return -1;
return mindis;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int a,b,c;
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
map[a][b]=map[b][a]=c;
}
ans=prim();
if(ans==-1)
{
printf("Not Unique!\n");
}
else
{
int t=umst();
if(t==ans) printf("Not Unique!\n");
else printf("%d\n",ans);
}
}
return 0;
}