poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35999   Accepted: 13145

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!

 

C/C++:

 1 #include <map>
 2 #include <queue>
 3 #include <cmath>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <climits>
 9 #include <iostream>
10 #include <algorithm>
11 #define INF 0x3f3f3f3f
12 using namespace std;
13 const int my_max_edge = 10010, my_max_node = 110;
14 
15 int t, n, m, my_book_edge[my_max_edge], my_pre[my_max_node], my_first;
16 
17 struct edge
18 {
19     int a, b, val;
20 }P[my_max_edge];
21 
22 bool cmp(edge a, edge b)
23 {
24     return a.val < b.val;
25 }
26 
27 int my_find(int x)
28 {
29     int n = x;
30     while (n != my_pre[n])
31         n = my_pre[n];
32     int i = x, j;
33     while (n != my_pre[i])
34     {
35         j = my_pre[i];
36         my_pre[i] = n;
37         i = j;
38     }
39     return n;
40 }
41 
42 int my_kruskal(int my_flag)
43 {
44     int my_ans = 0;
45     for (int i = 1; i <= n; ++ i)
46         my_pre[i] = i;
47 
48     for (int i = 0; i < m; ++ i)
49     {
50         int n1 = my_find(P[i].a), n2 = my_find(P[i].b);
51         if (n1 == n2 || my_flag == i) continue;
52         my_pre[n1] = n2;
53         if (my_first)my_book_edge[i] = 1;
54         my_ans += P[i].val;
55     }
56 
57     int temp = my_find(1);
58     for (int i = 2; i <= n; ++ i)
59         if (temp != my_find(i))
60             return -1;
61     return my_ans;
62 }
63 
64 int main()
65 {
66     scanf("%d", &t);
67     while (t --)
68     {
69         scanf("%d%d", &n, &m);
70         for (int i = 0; i < m; ++ i)
71             scanf("%d%d%d", &P[i].a, &P[i].b, &P[i].val);
72         sort(P, P + m, cmp);
73         memset(my_book_edge, 0, sizeof(my_book_edge));
74 
75         my_first = 1;
76         int mst = my_kruskal(-1), flag = 1;
77         if (mst == -1)
78         {
79             printf("0\n");
80             continue;
81         }
82         my_first = 0;
83         for (int i = 0; i < m; ++ i)
84         {
85             if (my_book_edge[i])
86             {
87                 if (mst == my_kruskal(i))
88                 {
89                     printf("Not Unique!\n");
90                     flag = 0;
91                     break;
92                 }
93             }
94         }
95         if (flag) printf("%d\n", mst);
96     }
97     return 0;
98 }

 

posted @ 2018-08-18 15:55  GetcharZp  阅读(131)  评论(0编辑  收藏  举报