The Unique MST
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 21737 |
|
Accepted: 7692 |
Description
Given a connected undirected graph, tell if it minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of 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, which has the and 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 <=), the number of test cases. Each case represents a graph. It begins with a line containing, integers n and m (1 <= n <=), the number of nodes and edges. Each of the following m lines contains a triple (xi, Yi, WI), indicating that Xi and Yi is connected by an edge with Weig HT = wi. For any of the nodes, there is at the most one edge connecting them.
Output
For each input, if the MST was unique, print the total cost of it, or otherwise print the string ' not unique! '.
Sample Input
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not unique!
Reprint please declare the original blog address: http://blog.csdn.net/lionel_d
How can you tell if the spanning tree is unique?
Just judge his sub-niche into the total weight of the tree and the total weight of the minimum spanning tree if you want to wait on it, if it is equal, it means that the minimum spanning tree is not unique, not equal,, natural only!
The second niche into the tree of the method, is to enumerate the edges, and then delete the edge, and constantly seek secondary small weight;
Specific blog: http://www.cnblogs.com/hxsyl/p/3290832.html
Look at the code:
#include <stdio.h> #include <string.h> #define MAX 110#define INF 1000000000int Graph[max][max], max[max][ Max];bool Used[max][max],visited[max]; int prim (int n) {int Lowcost[max],closest[max];memset (visited,false,sizeof ( visited)); Memset (used,false,sizeof (used)); for (int i = 1; I <= n; ++i) {lowcost[i] = Graph[1][i]; closest[i] = 1;} Visited[1] = true; int sum=0;for (int i = 0; i < n-1; ++i) {int min = INF, index =-1; for (int j = 1; J <= N; + +) j) {if (!visited[j] && min > Lowcost[j]) {min = Lowcost[j];index = j;}} if (index = =-1) {break;} Sum + = Lowcost[index]; Visited[index] = true; Used[index][closest[index]] = used[closest[index]][index] = true; for (Int J = 1; J <= N; ++J) {if (Visited[j] && Index! = j) {Max[j][index] = max[index][j] = Lowcost[index]>max[j][closest[index]]? Lowcost[index]:max[j][closest[index]];} if (!visited[j] && lowcost[j]>graph[index][j]) {lowcost[j] = Graph[index][j]; closest[j] = index;}}} return sum;} int MaiN () {int t; scanf ("%d", &t), while (t--) {int n, m;scanf ("%d%d", &n,&m), for (int i = 0; I <= N; ++i) {Graph[i] [i] = inf; for (int j = 0; J < i; ++j) {graph[i][j] = graph[j][i] = inf;}} for (int i = 0; i < m; ++i) {int xi,yi,wi;scanf ("%d%d%d", &xi,&yi,&wi); Graph[xi][yi] = graph[yi][xi] = WI;} int sum = PRIM (n), t = INF, for (int i = 1; I <= n; ++i) {for (int j = 1; j < i; ++j) {if (!used[i][j] && g RAPH[I][J]! = INF) {int r = Sum-max[i][j]+graph[i][j];t = T>r?r:t;}}} if (sum = = t) {puts ("not unique!");} else{printf ("%d\n", Sum);}} return 0;}
with June
Hdu 1679 the Unique MST sub-niche into a tree simple problem