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 }

原文地址:https://www.cnblogs.com/GetcharZp/p/9497554.html

时间: 2024-12-08 17:01:14

poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)的相关文章

poj 1679 The Unique MST (次小生成树)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20293   Accepted: 7124 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ - 1679 The Unique MST(次小生成树)

版权声明:本文为博主原创文章,未经博主允许不得转载.

hdu 1679 The Unique MST 次小生成树 简单题

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21737   Accepted: 7692 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

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 followin

poj 1679 The Unique MST 【次小生成树】【模板】

题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后添加到最小生成树上,然后把原树上添加了之后形成环的最长的边删去,知道一个最小的.就是次小生成树. 这些需要的都可以在求解最小生成树的时候处理出来. AC代码: #include <cstdio> #include <cstring> #include <iostream> #i

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST(求最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 7186 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST 【最小生成树/次小生成树】

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST (判定最小生成树是否唯一)

题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29408   Accepted: 10520 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spannin