The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9446 | Accepted: 3142 |
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!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
///次小生成树 如果等于最小生成树:Not Unique,表示最小生成树不唯一;否则输出
///最小生成树的值
const int maxn=600;//最大点数
const int inf=(1<<27);
int V,E;//点数(从1开始),边数
int mat[maxn][maxn];//邻接矩阵
//T 最小生成树
int max_val[maxn][maxn];//在T中连结任意两点u,v的唯一的路中权值最大的那条边的权值。
//prime
int vis[maxn],lowc[maxn];
int pre[maxn];//记录到i点的最短距离的点
bool visited[maxn][maxn];//判断是否是T中的边,visited[i][j]=0&&mat[i][j]!=inf表示是
void presolve()
{
for(int i=1;i<=V;i++) for(int j=1;j<=V;j++) mat[i][j]=inf;
memset(max_val,0,sizeof(max_val));
memset(visited,0,sizeof(visited));
}
///记录最小生成树的边,以及任意两点在T中的路中权值最大的值
int prime()//假设图是连通的
{
int res=0;
memset(vis,0,sizeof(vis));
memset(pre,0,sizeof(pre));
vis[1]=1;pre[1]=1;
for(int i=2;i<=V;i++)
{
lowc[i]=mat[1][i];
pre[i]=1;
}
for(int i=2;i<=V;i++)
{
int minc=inf,p=-1;
for(int j=2;j<=V;j++)
{
if(vis[j]==0&&lowc[j]<minc)
{
minc=lowc[j],p=j;
}
}
if(minc==inf) return -1;//不连通
max_val[pre[p]][p]=minc;
visited[pre[p]][p]=visited[p][pre[p]]=0;
for(int k=1;k<=V;k++)
{
max_val[k][p]=max(max_val[k][pre[p]],max_val[pre[p]][p]);
}
res+=minc;
vis[p]=1;
for(int j=2;j<=V;j++)
{
if(vis[j]==0&&lowc[j]>mat[p][j])
{
lowc[j]=mat[p][j];
pre[j]=p;
}
}
}
return res;
}
int unique_tree(int ans)
{
int minc=inf;
bool flag=true;
for(int i=1;i<=V;i++)
{
for(int j=1;j<=V;j++)
{
if(mat[i][j]==inf||visited[i][j]==0) continue;
int cnt=ans+mat[i][j]-max_val[i][j];
if(cnt<minc) minc=cnt;
}
}
//return minc; 次小生成树
if(minc==ans) return 0;
else return minc;
}
int main()
{
int ci;scanf("%d",&ci);
while(ci--)
{
scanf("%d%d",&V,&E);//点数,边数
presolve();//预处理
//输入边
for(int l=0;l<E;l++)
{
int u,v,w;scanf("%d%d%d",&u,&v,&w);
mat[u][v]=mat[v][u]=w;//邻接矩阵
visited[u][v]=visited[v][u]=1;//存在边
}
int ans=prime();//最小生成树
//如果最小生成树是唯一的,即次小生成树小于最小生成树
if(unique_tree(ans)) printf("%d/n",ans);
//最小生成树等于次小生成树
else printf("Not Unique!/n");
}
return 0;
}