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! 求次小生成树 看与最小生成树是否相同
prime求次小生成树
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
typedef long long LL;
int graph[][], d[maxn], vis[maxn], maxd[][], pre[maxn];
int n, m; int prime(int s)
{
int temp, sum = ;
mem(vis, );
for(int i=; i<=n; i++) d[i] = graph[s][i], pre[i] = s;
vis[s] = ;
d[s] = ;
for(int i=; i<n; i++)
{
int mincost = INF;
for(int j=; j<=n; j++)
{
if(!vis[j] && mincost > d[j])
mincost = d[j], temp = j;
}
for(int j=; j<=n; j++)
if(vis[j]) maxd[temp][j] = maxd[j][temp] = max(mincost, maxd[pre[temp]][j]);
vis[temp] = ;
sum += mincost;
for(int j=; j<=n; j++)
{
if(!vis[j] && d[j] > graph[temp][j])
d[j] = graph[temp][j], pre[j] = temp;
}
}
// for(int i=1; i<=n; i++)
// sum += d[i];
return sum;
} int main()
{
int T;
cin>> T;
while(T--)
{
cin>> n >> m;
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(i == j) graph[i][j] = ;
else graph[i][j] = graph[j][i] = INF;
for(int i=; i<m; i++)
{
int u, v, w;
cin>> u >> v >> w;
graph[u][v] = graph[v][u] = w;
}
int sum = prime();
int lsum = INF;
for(int i=; i<=n; i++)
for(int j=i+; j<=n; j++)
{
if(i != pre[j] && j != pre[i] && graph[i][j] != INF)
if(sum - maxd[i][j] + graph[i][j] < lsum)
lsum = sum - maxd[i][j] + graph[i][j];
} if(lsum == sum)
cout<< "Not Unique!" <<endl;
else
cout<< sum <<endl; } return ;
}
												

The Unique MST POJ - 1679 (次小生成树)的更多相关文章

  1. The Unique MST POJ - 1679 次小生成树prim

    求次小生成树思路: 先把最小生成树求出来  用一个Max[i][j] 数组把  i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过   把没有使用过的一条边加 ...

  2. Day5 - G - The Unique MST POJ - 1679

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  3. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  4. poj1679 The Unique MST(判定次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23180   Accepted: 8235 D ...

  5. POJ-1679.The Unique MST.(Prim求次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39561   Accepted: 14444 ...

  6. poj 1679 次小生成树

    次小生成树的求法: 1.Prime法 定义一个二维数组F[i][j]表示点i到点j在最小生成树中的路径上的最大权值.有个知识就是将一条不在最小生成树中的边Edge加入最小生成树时,树中要去掉的边就是E ...

  7. K - The Unique MST - poj 1679

    题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...

  8. (最小生成树 次小生成树)The Unique MST -- POJ -- 1679

    链接: http://poj.org/problem?id=1679 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  9. The Unique MST POJ - 1679 最小生成树判重

    题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...

随机推荐

  1. (原创)odoo在docker环境下无法备份

    odoo容器内置postgresql-client版本和数据库版本不一致,安装和数据库版本相同或者更高版本的客户端 参考:https://www.postgresql.org/download/lin ...

  2. xhtml和html的区别 html5和xhtml的区别

    xhtml和html的区别 - 分为两大类比较:一个是功能上的差别,另外是书写习惯的差别.关于功能上的差别,主要是XHTML可兼容各大浏览器.手机以及PDA,并且浏览器也能快速正确地编译网页,- XH ...

  3. Roslyn入门(一)-C#语法分析

    演示环境 Visual Studio 2017 .NET Compiler Platform SDK 简介 今天,Visual Basic和C#编译器是黑盒子:输入文本然后输出字节,编译管道的中间阶段 ...

  4. Flutter - 本地化启动列表中App名字

    上一篇讲了 Flutter - 本地化语言 但是这还有一点小欠缺,就是启动器中的App名字还是无法本地化. 比如英文系统中Play Store,在中文手机中就会显示Play 商店 那么为了解决这个问题 ...

  5. Python代码转c#部分参考样例

    最近在做一部分Pyhton代码转c#代码的工作,以下案例亲自都测试过,现整理出来希望对有帮助的同学提供参考: Python | C# *:first-child{margin-top:0 !impor ...

  6. 【nodejs】让nodejs像后端mvc框架(asp.net mvc)一样处理请求--目录(8/8 完结)

    为什么要做这个 在使用nodejs开发过程中,总是发现需要做很多重复性的体力劳动,且因为自身是服务端程序员出身,感觉有一些服务端好的东西其实可以在nodejs上得到应用并能提高一些开发工作效率. 本系 ...

  7. Python常见字符编码间的转换

    主要内容:     1.Unicode 和 UTF-8的爱恨纠葛     2.字符在硬盘上的存储     3.编码的转换     4.验证编码是否转换正确     5.Python bytes类型 前 ...

  8. javascript数据类型以及类型间的转化函数

    js 有五种基本数据类型,还有个引用类型 1.undefined 类型,只有一个志undefined 当变量未初始化时都会是这个类型. 2.null 类型,也是只有一个值null,null类型的typ ...

  9. Junit测试用例

    一.题目简介 返回一个给定整数参数的绝对值. 二.源码的github链接 https://github.com/liyan941016/test/blob/master/FileTest.java h ...

  10. <构建之法>8,9,10章的读后感

    第八章 这一章主要讲的是需求分析,主要介绍在客户需求五花八门的情况下,软件团队如何才能准确而全面地找到这些需求. 第九章 问题:我们现在怎样培养才能成为一名合格的PM呢? 第十章 问题:如果典型用户吴 ...