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. GIT 分支管理:创建与合并分支、解决合并冲突

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...

  2. windows平台快速搭建Linux(CentOS)

    VMware简介 VMware Workstation(简称 “虚拟机”)是一款功能强大的桌面虚拟计算机软件,用户可在Windows平台通过VMware软件同时运行不同的操作系统.这对IT开发人员而言 ...

  3. 2018 Multi-University Training Contest 2 部分简单题解析

    Preface 多校第二场,依靠罚时优势打到了校内的Rank 2 暴力分块碾标算系列 T4 Game 题目大意:在一个数集\([1,n]\)中两个人轮流选择其中的一个数,并从数集中删去这个数所有约数. ...

  4. .Net架构篇:思考如何设计一款实用的分布式监控系统?

    前言 无论从最早期的unix操作系统,还是曾经大行其道的单体式应用,还是现在日益流行的微服务架构,始终都离不开监控的身影.如windows的任务管理器,linux的top命令,都可以看作是监控的面板. ...

  5. bootstrapTable使用场景及方式

    场景一:混合开发,适用jsp.php前端不足  纯html结构 <div> <table id="table" data-toggle="table&q ...

  6. Rancher + k8s + docker 部署资料

    一.k8s 文档: https://jimmysong.io/kubernetes-handbook/concepts/deployment.html 命令行大全 https://kubernetes ...

  7. PHP 设置调试工具XDebug PHPStorm IDE

    先下载PHP扩展Xdebug https://xdebug.org, 可以复制自己的phpinfo粘贴到https://xdebug.org/wizard.php中, 会生成需要下载的版本, php. ...

  8. Linux下DNS简单部署(主从域名服务器)

    一.DNS简介DNS(Domain Name System),域名系统,因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通 ...

  9. haproxy反向代理环境部署(http和https代理)

    操作背景:前方有一台haproxy代理机器(115.100.120.57/192.168.1.7),后方两台realserver机器(192.168.1.150.192.168.1.151,没有公网i ...

  10. 使用thinkphp框架实现Excel导入数据库

    之前讲过php实现Excel导出数据库的随笔,链接:https://www.cnblogs.com/nuanai/p/6727711.html 之前的项目用到较多的就是Excel导出,现在用到了Exc ...