题目链接: http://poj.org/problem?id=1679
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!
判断一张图的最小生成树是否唯一,即判断其次小生成树是否和最小生成树的边权值相等。
题目中图的点从1到n;
次小生成树的寻找方法 用二维数组Max记录最小生成树中点i到点j所经过的路中最长的一条的权值,且边<i,j>不在该最小生成树中,然后将<i,j>加入最小生成树,删除max[i][j],处理完所有最小生成树之外的边后取最小值即为次小生成树。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 int n, m; 8 int map[105][105]; 9 bool pass[105]; 10 int dis[105]; 11 int ans; 12 int Max[105][105];//记录从i到j所经过的路中最长的一条 13 bool used[105][105]; 14 int pre[105]; 15 16 void prim(){ 17 ans = 0; 18 memset( used, 0, sizeof( used ) ); 19 memset( Max , 0, sizeof( Max ) ); 20 memset( pass, false, sizeof( pass ) ); 21 for( int i = 1; i <= n; i++ ){ 22 dis[i] = map[1][i]; 23 pre[i] = 1; 24 } 25 pre[1] = -1; 26 pass[1] = true; 27 28 for( int t = 1; t < n; t++ ){ 29 int min = INF; 30 int mini = -1; 31 32 for( int i = 1; i <= n; i++ ){ 33 if( pass[i] ) continue; 34 if( dis[i] < min ){ 35 min = dis[i]; 36 mini = i; 37 } 38 } 39 40 pass[mini] = true; 41 ans += min; 42 used[mini][pre[mini]] = used[pre[mini]][mini] = true; 43 44 for( int i = 1; i <= n; i++ ){ 45 if( pass[i] ) Max[i][mini] = Max[mini][i] = max( dis[mini], Max[i][pre[mini]]); 46 else if( map[mini][i] < dis[i] ){ 47 dis[i] = map[mini][i]; 48 pre[i] = mini; 49 } 50 } 51 } 52 } 53 54 int ask(){ 55 int temp = INF; 56 57 for( int i = 1; i <= n ; i++ ) 58 for( int j = i + 1; j <= n; j++ ) 59 if( map[i][j] != INF && !used[i][j] ){ 60 temp = min( temp, ans + map[i][j] - Max[i][j] ); 61 } 62 63 if( ans == temp ) return 1; 64 else return 0; 65 } 66 67 int main(){ 68 int T; 69 70 cin >> T; 71 while( T-- ){ 72 cin >> n >> m; 73 74 for( int i = 1; i <= n; i++ ) 75 for( int j = 1; j <= n; j++ ){ 76 if( i == j ) map[i][j] = 0; 77 else map[i][j] = INF; 78 } 79 80 int a, b, c; 81 for( int i = 0; i < m; i++ ){ 82 cin >> a >> b >> c; 83 map[a][b] = map[b][a] = min( map[a][b], c ); 84 } 85 86 prim(); 87 88 if( ask() ) cout << "Not Unique! "; 89 else cout << ans << endl; 90 } 91 }