POJ 1679 - The Unique MST(次小生成树)
題目鏈接 https://vjudge.net/problem/POJ-1679
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!
【題意】
給定一張無向圖,判斷這個圖的最小生成樹是否唯一,如果唯一則輸出最小生成樹的權值,否則輸出 “Not Unique!”
【思路】
題目實際上想問的是次小生成樹的權值,如果次小生成樹的權值和最小生成樹一樣,那么最小生成樹不唯一。最小生成樹可以在求出每對點之間的最小瓶頸路后枚舉剩下的邊求出。具體做法是先求最小生成樹,同時用dfs求出任意兩點的最小瓶頸路,然后枚舉所有不在最小生成樹中的邊,假設這條邊的端點是u和v,用這條邊去替換原來最小生成樹中u,v的最小瓶頸路,迭代取最小值。所以次小生成樹就是最小瓶頸路的一個簡單應用。
轉載于:https://www.cnblogs.com/wafish/p/10465403.html
總結
以上是生活随笔為你收集整理的POJ 1679 - The Unique MST(次小生成树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring MVC modelandv
- 下一篇: 创建布局