POJ 1679 The Unique MST(最小生成树)

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!'.
 
题目大意:判断最小生成树是否唯一(或者说判断次小生成树与最小生成树是否具有同样的权值)
思路:用Kruskal加边的时候,每次判断是否有其他边和当前边具有同样的功能(同样的边权,连接的集合相同),有则输出Not Unique!
 
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int MAXE = 251000;
 7 const int MAXN = 105;
 8 
 9 struct Edge {
10     int from, to, val;
11     bool operator < (const Edge &rhs) const {
12         return val < rhs.val;
13     }
14 } edge[MAXE];
15 
16 int fa[MAXN], deg[MAXN];
17 int n, ecnt;
18 
19 void init() {
20     ecnt = 0;
21     for(int i = 1; i <= n; ++i) {
22         fa[i] = i;
23         deg[i] = 0;
24     }
25 }
26 
27 void add_edge(int u, int v, int c) {
28     edge[ecnt].from = u;
29     edge[ecnt].to = v;
30     edge[ecnt++].val = c;
31 }
32 
33 int getfather(int x) {
34     return fa[x] == x ? x : getfather(fa[x]);
35 }
36 
37 void union_set(int x, int y) {
38     int a = getfather(x);
39     int b = getfather(y);
40     if(a == b) return ;
41     if(deg[a] <= deg[b]) swap(a, b);
42     ++deg[a]; fa[b] = a;
43 }
44 
45 int kruskal() {
46     int sum = 0;
47     int xa, ya;
48     sort(edge, edge + ecnt);
49     for(int i = 0; i < ecnt; ++i) {
50         xa = getfather(edge[i].from);
51         ya = getfather(edge[i].to);
52         if(xa == ya) continue;
53         for(int j = i + 1; j < ecnt; ++j) {
54             if(edge[j].val != edge[i].val) break;
55             if(xa == getfather(edge[j].from) && ya == getfather(edge[j].to)) {
56                 return -1;
57                 break;
58             }
59         }
60         union_set(edge[i].from, edge[i].to);
61         sum += edge[i].val;
62     }
63     return sum;
64 }
65 
66 int main() {
67     int T, m, a, b, c;
68     scanf("%d", &T);
69     while(T--) {
70         scanf("%d%d", &n, &m);
71         init();
72         for(int i = 0; i < m; ++i) {
73             scanf("%d%d%d", &a, &b, &c);
74             if(a > b) add_edge(b, a, c);
75             else add_edge(a, b, c);
76         }
77         int ans = kruskal();
78         if(ans < 0) printf("Not Unique!\n");
79         else printf("%d\n", ans);
80     }
81 }
View Code

 

posted @ 2013-07-30 16:57  Oyking  阅读(187)  评论(0编辑  收藏  举报