博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ_1679_The Unique MST(次小生成树模板)
阅读量:4350 次
发布时间:2019-06-07

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

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23942   Accepted: 8492

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

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

Sample Output

3Not Unique!

题意:问最小生成树是否唯一。

分析:求次小生成树,推断次小生成树和最小生成树是否相等。留作模板。

次小生成树的步骤:

(1)先用Prime求出最小生成树T,在Prime的同一时候用一个矩阵max_edge[u][v]记录在T中连接随意两点u,v的唯一路径中权

值最大的那条边的权值。注意这里是非常easy做到的。由于Prime是每次添加一个节点t。而设已经标了号的节点集合为S,则S

中全部节点到t的路径中最大权值的边就是当前增加的这条边。

(2)枚举最小生成树以外的边,并删除该边所在环上权值最大的边。

(3)取得的全部生成树中权值最小的一棵即为所求。

算法的时间复杂度为O(n^2)。

题目链接:

代码清单:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxn = 100 + 5;const int maxv = 10000 + 5;const int max_dis = 1e9 + 5;int T;int n,m;int a,b,c;int MST,_MST;bool vis[maxn];int father[maxn];int dist[maxn];int graph[maxn][maxn];bool used[maxn][maxn];int max_edge[maxn][maxn];void init(){ memset(vis,false,sizeof(vis)); memset(used,false,sizeof(used)); memset(max_edge,-1,sizeof(max_edge)); memset(graph,0x7f,sizeof(graph));}void input(){ scanf("%d%d",&n,&m); for(int i=0;i

posted on
2017-07-05 21:35 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/mthoutai/p/7123582.html

你可能感兴趣的文章
面向 例题
查看>>
RSA加密
查看>>
HTML5游戏开发进阶指南
查看>>
ASP.NET GBK读取QueryString
查看>>
[LintCode] 159 Find Minimum in Rotated Sorted Array
查看>>
在reshard过程中,将会询问reshard多少slots:
查看>>
地精排序-最简单的排序算法
查看>>
跑路啦 跑路啦 这个博客废掉啦
查看>>
JQuery 实现返回顶部效果
查看>>
辛苦挣钱买房,结果房产证一直办不下来
查看>>
Which kind of aspects of OIL PRESS MACHINERY would you like best
查看>>
[转载] 晓说——第17期:揭秘战争秘闻 朝鲜战争62年祭(下)
查看>>
java集合系列之ArrayList源码分析
查看>>
nyoj 518取球游戏(博弈)
查看>>
实验5
查看>>
luogu P1252 马拉松接力赛 P1803 凌乱的yyy / 线段覆盖
查看>>
11. 鼠标移到物体上高亮显示轮廓
查看>>
VS C++ DLL速度问题
查看>>
JavaScript弹出式日历控件 无jquery
查看>>
HTML5方向键控制会奔跑的马里奥大叔
查看>>