博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1679 The Unique MST(最小生成树)
阅读量:5230 次
发布时间:2019-06-14

本文共 3251 字,大约阅读时间需要 10 分钟。

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 
2 #include
3 #include
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

 

转载于:https://www.cnblogs.com/oyking/p/3225552.html

你可能感兴趣的文章
解决 sublime text3 运行python文件无法input的问题
查看>>
javascript面相对象编程,封装与继承
查看>>
Atlas命名空间Sys.Data下控件介绍——DataColumn,DataRow和DataTable
查看>>
Java中正则表达式的使用
查看>>
算法之搜索篇
查看>>
新的开始
查看>>
java Facade模式
查看>>
NYOJ 120校园网络(有向图的强连通分量)(Kosaraju算法)
查看>>
SpringAop与AspectJ
查看>>
Leetcode 226: Invert Binary Tree
查看>>
http站点转https站点教程
查看>>
解决miner.start() 返回null
查看>>
bzoj 2007: [Noi2010]海拔【最小割+dijskstra】
查看>>
BZOJ 1001--[BeiJing2006]狼抓兔子(最短路&对偶图)
查看>>
C# Dynamic通用反序列化Json类型并遍历属性比较
查看>>
128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
查看>>
定制jackson的自定义序列化(null值的处理)
查看>>
auth模块
查看>>
javascript keycode大全
查看>>
前台freemark获取后台的值
查看>>