POJ 1679 The Unique MST (次小生成树)

网友投稿 537 2022-10-03

POJ 1679 The Unique MST (次小生成树)

POJ 1679 The Unique MST (次小生成树)

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: 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’.

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

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not Unique!

题意

给定一张图,求它的最小生成树是否唯一,若唯一输出最小生成树权值和,否则输出 ​​Not Unique!​​ 。

思路

直接求给定图的次小生成树,然后枚举所有不在 ​​MST​​​ 中的边,替换掉最大边权的边以后若此时的结果仍是原 ​​MST​​ 边权和,说明最小生成树不唯一,否则唯一。

AC 代码

#include#include#include#include#include#includeusing namespace std;const int maxn = 110;const int inf = 0x3f3f3f;bool vis[maxn];int lowc[maxn];int pre[maxn];int Max[maxn][maxn];bool used[maxn][maxn];int cost[maxn][maxn];int prim(int n){ int ans=0; memset(vis,false,sizeof(vis)); memset(Max,0,sizeof(Max)); memset(used,false,sizeof(used)); vis[0]=true; pre[0]=-1; for(int i=1; ilowc[j]) { minc=lowc[j]; p=j; } } if(minc==inf)return -1; ans+=minc; vis[p]=true; used[p][pre[p]]=used[pre[p]][p]=true; for(int j=0; jcost[p][j]) { lowc[j]=cost[p][j]; pre[j]=p; } } } return ans;}void init(int n){ memset(cost,inf,sizeof(cost)); for(int i=0; i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HDU 5974 A Simple Math Problem (数论+解方程组)
下一篇:微信小程序如何进行异步处理(小程序异步解决方案)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~