The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 25572 | Accepted: 9121 |
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
解体思路:生成树的第一道题目,这题属于次小生成树的模板题.如何判断次小生生成树是否存在,现在在网上找了人家写的算法思想,如下:
算法核心思想:在prime求MST的过程中 用数组存储MST里面任意两点间的唯一的路中 权值最大的那条边的权值。最后枚举不在MST里面的边<i,,j>,判断<i,j>的权值 是否 和 MST里面 i 到 j 的最大权值相等,只要有一条边满足就可以说明MST不唯一。(因为我们可以用这条不在MST的边来 代替 在MST的边,这样MST肯定不唯一)
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int map[110][110];
int inMST[110][110];
int MST[110][110];
int vis[110];
int per[110];
int low[110];
void inin(){
for(int i=1;i<=n;i++){
map[i][i]=0;
for(int j=1;j<i;j++){
map[i][j]=map[j][i]=INF;
}
}
}
void getMap(){
int a,b,c;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c){
map[a][b]=map[b][a]=c;
}
}
}
int slove(){
int temp,v,i,j,k,fa;
v=0;
memset(vis,0,sizeof(vis));
memset(inMST,0,sizeof(inMST));
memset(MST,0,sizeof(MST));
for(i=1;i<=n;i++){
low[i]=map[1][i];
per[i]=1;
}
per[1]=0;
vis[1]=1;
for(i=2;i<=n;i++){
temp=INF;
for(j=1;j<=n;j++){
if(!vis[j]&&low[j]<temp){
temp=low[j];
k=j;
}
}
if(temp==INF)return -1;
vis[k]=1;
v+=temp;
fa=per[k];
inMST[k][fa]=inMST[fa][k]=1;
for(j=1;j<=n;j++){
if(j!=k&&vis[j])
MST[k][j]=MST[j][k]=max(MST[j][fa],low[k]);
if(!vis[j]&&low[j]>map[k][j]){
low[j]=map[k][j];
per[j]=k;
}
}
}
for(i=1;i<=n;i++){
for(j=1;j<i;j++){
if(!inMST[i][j]&&map[i][j]!=INF){
if(MST[i][j]==map[i][j])return -1;
}
}
}
return v;
}
int main(){
int t,ans;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
inin();
getMap();
ans=slove();
if(ans==-1)
printf("Not Unique!\n");
else printf("%d\n",ans);
}
return 0;
}