poj 1679题目链接
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 34429 Accepted: 12559
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:
V’ = V.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’.
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.
For each input, if the MST is unique, print the total cost of it, or otherwise print the string ‘Not Unique!’.
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
3 Not Unique!
POJ Monthly–2004.06.27 srbga@POJ
题意比较简单,就是说最小生成树是否唯一。也就是求次小生成树,判断是否有次小生成树和最小生成树的值相同。
这里我用的Kurskal ,比较容易理解。次小生成树,在最小生成树的基础上,加上一条边,让他构成一个环,然后在这个环里面去掉一个最大的边,剩下的树就是次小生成树了。不过要记得就是对每一条边都要进行替换,看是否能找到相等的值的生成树。
Kurskal 实现的话比 prime 更容易理解一点的,也更容易实现。因为Kurskal 算法就是根据边来找生成树的。
