poj1679 次最小生成树 kruskal(暴力枚举)

本文介绍了一种通过暴力枚举的方法来判断给定图的最小生成树是否唯一的算法实现。该方法首先找到一棵最小生成树,并记录其总权重,然后逐一删除已使用过的边,尝试寻找另一棵总权重相同的生成树。

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!

这题直接暴力枚举,找到一颗最小生成树,标记一下,依次删边


 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 using namespace std;
 8 const int maxn = 1010 ;
 9 const int INF = 0x7fffffff;
10 struct node {
11     int u, v, w;
12     int used, num, flag;
13 } qu[10 * maxn];
14 int fa[maxn], n, m;
15 int cmp(node a, node b) {
16     return a.w < b.w;
17 }
18 void init() {
19     for (int i = 0 ; i <= n ; i++) fa[i] = i;
20 }
21 int Find(int x) {
22     return fa[x] == x ? x : fa[x] = Find(fa[x]);
23 }
24 int combine(int x, int y) {
25     int nx = Find(x);
26     int ny = Find(y);
27     if (nx != ny) {
28         fa[nx] = ny;
29         return 1;
30     }
31     return 0;
32 }
33 int kruskal(int flag) {
34     init();
35     int sum = 0, cnt = 0;
36     for (int i = 0 ; i < m ; i++) {
37         if (qu[i].flag) continue;
38         if (combine(qu[i].v, qu[i].u)) {
39             if (!flag) qu[i].used = 1;
40             sum += qu[i].w;
41             cnt++;
42             if (cnt == n - 1) break;
43         }
44     }
45     if (cnt != n - 1) return -1;
46     return sum;
47 }
48 int main() {
49     int t;
50     scanf("%d", &t);
51     while(t--) {
52         scanf("%d%d", &n, &m);
53         for (int  i = 0 ; i < m ; i++) {
54             scanf("%d%d%d", &qu[i].u, &qu[i].v, &qu[i].w);
55             qu[i].flag = 0, qu[i].used = 0;
56         }
57         sort(qu, qu + m, cmp);
58         int sum = kruskal(0);
59         int flag = 0;
60         for (int i = 0 ; i < m ; i++) {
61             if (qu[i].used ) {
62                 qu[i].flag = 1;
63                 int temp = kruskal(1);
64                 qu[i].flag = 0;
65                 if (temp == sum) {
66                     flag = 1;
67                     break;
68                 }
69             }
70         }
71         if (flag) printf("Not Unique!\n");
72         else printf("%d\n", sum);
73     }
74     return 0;
75 }

 




转载于:https://www.cnblogs.com/qldabiaoge/p/9080058.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值