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'. 

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!

题意:就是求一个最小生成树是否是唯一的,是就输出最小生成树的值,否则输出Not Unique!
思路:先求出一个最小生成树的权值ans,标记最小生成树的边,然后枚举1-m,去掉之前的一条边生成的最小生成树的权值是否等于ans
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<stdlib.h>
  5 #include<math.h>
  6 #include<algorithm>
  7 #define LL long long
  8 #define mod 1e9 + 7
  9 const int M = 10005;
 10 
 11 using namespace std;
 12 
 13 struct node{
 14     int x;
 15     int y;
 16     int value;
 17 }a[M];
 18 
 19 int cun[105];
 20 int m, n, cnt;
 21 int visit[M];
 22 int visit1[M];
 23 
 24 int cmp(node a, node b)
 25 {
 26     return a.value < b.value;
 27 }
 28 
 29 int find(int x)
 30 {
 31     return cun[x] == x ? x : cun[x] = find(cun[x]);
 32 }
 33 
 34 void Union(int x,int y)
 35 {
 36     x = find(x);
 37     y = find(y);
 38     if(x != y)
 39         cun[x] = y;
 40 }
 41 int shu(int x)
 42 {
 43     int ans = 0;
 44     cnt = 0;
 45     for(int i = 1; i <= m; ++i)
 46     {
 47         if(i != x)
 48         {
 49             if(find(a[i].x) != find(a[i].y))
 50             {
 51                 Union(a[i].x,a[i].y);
 52                 ans += a[i].value;
 53                 visit[i] = 1;
 54                 cnt++;
 55             }
 56         }
 57     }
 58     if(cnt == n - 1)
 59         return ans;
 60     else
 61         return -1;
 62 }
 63 
 64 int main()
 65 {
 66     int t;
 67     cin >> t;
 68     while( t-- )
 69     {
 70         scanf("%d%d",&n,&m);
 71         for(int i = 1; i <= m; ++i)
 72             scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].value);
 73         for(int i = 0; i <= n; ++i)
 74             cun[i] = i;
 75         sort(a + 1,a + m + 1,cmp);
 76         memset(visit,0,sizeof(visit));
 77         memset(visit1,0,sizeof(visit1));
 78         int ans = shu(-1);
 79         for(int i = 1; i <= m; ++i)
 80             visit1[i] = visit[i];
 81         int flag = 0;
 82         for(int i = 1; i <= m; ++i)
 83         {
 84             if(visit1[i])
 85             {
 86                 for(int j = 0; j <= n; ++j)
 87                     cun[j] = j;
 88                 if(shu(i) == ans)
 89                 {
 90                     flag = 1;
 91                     break;
 92                 }
 93             }
 94             if(flag)
 95                 break;
 96         }
 97         if(flag)
 98             puts("Not Unique!");
 99         else
100             cout << ans << endl;
101     }
102     return 0;
103 }
View Code

 

posted on 2014-08-06 22:53  Unico  阅读(130)  评论(0)    收藏  举报