http://poj.org/problem?id=1679
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
题意:
给n点m边无重边,求最小生成树是否唯一,如果这棵最小生成树是唯一的那么就输出最小生成树上权的和,不是唯一的就输出Not Unique!
思路:
求次小生成树,如果和最小生成树结果一样则不唯一。
次小生成树:
次小生成树由最小生成树变化而来,通过最小生成树概念可以知道“次小”只需要通过变化最小生成树上的一条边实现,并且要使得这种变化是最小。
给出一篇写的挺好的博客:https://blog.csdn.net/qq_27437781/article/details/70821413
from:https://blog.csdn.net/u011721440/article/details/38735547
判断最小生成树是否唯一:
1、对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边。
2、用kruskal或prim求出MST。
3、如果MST中无标记的边,则MST唯一;否则,在MST中依次去掉标记的边,再求MST,若求得MST权值和原来的MST权值相同,则MST不唯一。
from:https://blog.csdn.net/blue_skyrim/article/details/51338375
次小生成树的求法是枚举最小生成树的每条边,把其中一条边去掉,找到这两点上其他的边,剩下的边形成最小生成树
kuangbin大佬的博客:https://www.cnblogs.com/kuangbin/p/3147329.html
思路:
求最小生成树时,用数组maxval[i][j]来表示MST中i到j最大边权,求完后,直接枚举所有不在MST中的边,替换掉最大边权的边,更新答案 ,注意点的编号从0开始
原理:
最小生成树上的不相邻的两点相连必定成成为一个环,所以我们可以尝试枚举这些不相邻的点使他们相连,再删除环中属于最小生成树的最大边(令当前被确定的点为u,已经被确定的点为v,则u--v路径中最大的边要么来自v--pre[u]路径中的最大,要么就是当前被确定的边lowval[u],dp的思想),这样既保证树的结构又能使树的变化最小。这些枚举中最小的结果即为次小生成树。
1 #include
2 #include <string.h>
3 #include
4 #include <string>
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include <set>
11 #include