step 1. 先用prim求出最小生成树T.
在prim的同时,用一个矩阵max[u][v] 记录 在T中连结任意两点u,v的唯一的
路中权值最大的那条边的权值. (注意这里).
这是很容易做到的,因为prim是每次增加一个结点s, 而设已经标号了的结点
集合为W, 则W中所有的结点到s的路中的最大权值的边就是当前加入的这条边.
step 1 用时 O(V^2).
step 2. 枚举所有不在T中的边uv, 加入边uv则必然替换权为max[u][v]的边。
路中权值最大的那条边的权值. (注意这里).
这是很容易做到的,因为prim是每次增加一个结点s, 而设已经标号了的结点
集合为W, 则W中所有的结点到s的路中的最大权值的边就是当前加入的这条边.
step 1 用时 O(V^2).
step 2. 枚举所有不在T中的边uv, 加入边uv则必然替换权为max[u][v]的边。
The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29427 | Accepted: 10529 |
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
3Not Unique!
code:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int N=510; #define inf 0x3f3f3f3f int n,E; int vis[N],used[N][N],map[N][N],MAX[N][N],low[N],pre[N]; //used标记边是否在最小生成树中,MAX[i][j]存i-j在MST中的最大边权 int prim() { memset(vis,0,sizeof(vis)); memset(used,0,sizeof(used)); memset(pre,0,sizeof(pre)); memset(MAX,0,sizeof(MAX)); memset(low,0,sizeof(low)); int p,minn,ans=0; vis[1]=1; p=1; for(int i=1;i<=n;i++) { low[i]=map[p][i]; pre[i]=p; } for(int i=1;i<n;i++) { minn=inf; for(int j=1;j<=n;j++) { if(!vis[j]&&minn>low[j]) { minn=low[j]; p=j; } } ans+=minn; used[p][pre[p]]=used[pre[p]][p]=1;//标记,与MST的不同之处 vis[p]=1; for(int j=1;j<=n;j++) { if(vis[j]) MAX[p][j]=MAX[j][p]=max(MAX[j][pre[p]],low[p]);//与MST的不同之处 if(!vis[j]&&low[j]>map[p][j]) { low[j]=map[p][j]; pre[j]=p;//更新,与MST的不同之处 } } } return ans; } int main() { int T,x,y,z; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&E); 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]=inf; } } while(E--) { scanf("%d%d%d",&x,&y,&z); if(map[x][y]>z) { map[x][y]=z; } if(map[y][x]>z) { map[y][x]=z; } } int result=prim(); int flag=0; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(!used[i][j]) { if(map[i][j]==MAX[i][j])//寻找是否有不在MST中,但和MST中权值相同的边 { flag=1;break; } } } if(flag) break; } if(flag) printf("Not Unique!\n"); else printf("%d\n",result); } }