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!
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 101
#define INF 0x3f3f3f3f
/*
判断最小生成树是否唯一。
求次小生成树,若两个权值相等说明not unique
次小生成树算法,在prim()算法求解的时候,求出MST中u到v最大边权值
,然后用不在MST中的边依次枚举取最小值
*/
int g[MAXN][MAXN],Max[MAXN][MAXN],lowcost[MAXN],pre[MAXN],n,m,t;
bool used[MAXN][MAXN],been[MAXN];
int Prim()
{
int ret = ;
memset(been,false,sizeof(been));
memset(Max,,sizeof(Max));
memset(used,false,sizeof(used));
been[] = true;
pre[] = -;
for(int i=;i<=n;i++)
{
pre[i] = ;
lowcost[i] = g[][i];
}
lowcost[] = ;
for(int i=;i<n;i++)
{
int minc = INF,k =- ;
for(int j=;j<=n;j++)
{
if(!been[j]&&lowcost[j]<minc)
{
minc = lowcost[j];
k = j;
}
}
if(k==-) return -;
been[k] = true;
ret+=minc;
used[k][pre[k]] = used[pre[k]][k] = true;
for(int j=;j<=n;j++)
{
if(been[j])
Max[j][k] = Max[k][j] = max(Max[j][pre[k]],lowcost[k]);
if(!been[j]&&lowcost[j]>g[k][j])
{
lowcost[j] = g[k][j];
pre[j] = k;
}
}
}
return ret;
}
int cixiao(int ans)
{
int tmp = INF;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(!used[i][j]&&g[i][j]!=INF)
tmp = min(tmp,ans-Max[i][j]+g[i][j]);
}
if(tmp==INF)
return -;
return tmp;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
g[i][j] = INF;
}
int x,y,d;
for(int t=;t<m;t++)
{
cin>>x>>y>>d;
g[x][y] = g[y][x] = d;
}
int ans = Prim();
int tmp = cixiao(ans);
if(tmp==ans||ans==-)
cout<<"Not Unique!\n";
else
cout<<ans<<endl;
}
return ;
}

次小生成树 判断 unique MST的更多相关文章

  1. POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

  2. POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)

    http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...

  3. The Unique MST(次小生成树)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22335   Accepted: 7922 Description Give ...

  4. poj 1679 The Unique MST【次小生成树】

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

  5. poj 1679 判断MST是不是唯一的 (次小生成树)

    判断MST是不是唯一的 如果是唯一的 就输出最小的权值和 如果不是唯一的 就输出Not Unique! 次小生成树就是第二小生成树  如果次小生成树的权值和MST相等  那么MST就不是唯一的 法一: ...

  6. POJ 1679 The Unique MST (次小生成树)题解

    题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...

  7. POJ_1679_The Unique MST(次小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

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

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

  9. poj 1679 The Unique MST 【次小生成树+100的小数据量】

    题目地址:http://poj.org/problem?id=1679 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 Outpu ...

随机推荐

  1. deepin 安装版本管理工具

    在Linux下我们可以使用RapidSVN.RapidSVN是一款轻量级的免费.开源 SVN 客户端,相比tortoise svn它更加小巧而且占系统资源少运行速度快. 一:安装RapidSVN版本控 ...

  2. git 详细部署及其应用

    第1章 版本控制系统 自动生成备份.随时回滚.知道改动的地方. 1.1 svn和git的区别 1.1.1 svn 集中式的版本控制系统,只有一个中央数据仓库,如果中央数据库仓库挂了或者不可访问,所有的 ...

  3. Java多线程(十)线程间通信 join

    若果主线程想等待子线程执行完成之后再结束,可以用join方法 join 和sleep区别 join内部有wait实现,所以当执行join方法后,当前线程的锁被释放,那么其他线程就可以调用此线程的同步方 ...

  4. Java多线程(一) Thread和 Runnable

    http://www.cnblogs.com/lwbqqyumidi/p/3804883.html 1.继承Thread 2.实现Runnable接口 public class MyRunnable ...

  5. [转]ASP.NET MVC的帮助类HtmlHelper和UrlHelper

    本文转自:http://www.cnblogs.com/greatandforever/archive/2010/04/20/1715914.html?login=1 在ASP.NET MVC框架中没 ...

  6. 20 如何在C#中存一批数据,数组

    使用软件的一个重要原因,是因为软件可以帮我们重复处理很多事情.在前面我们已经讲到了循环.循环就是为了重复处理一个事情.那么我们有没有想过,我们要重复处理的一批数据怎么在程序里存放呢? 举个例子吧. 我 ...

  7. leetcode126 Word Ladder II

    思路: 宽搜过程中分层记录路径,递归还原.实现: class Solution { public: void getPath(string now, string beginWord, string ...

  8. CSS垂直居中和水平居中的几种方法

    垂直居中 方法一 这个方法把div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align属性. <!DOCTYPE html> <html lang=" ...

  9. SpringBoot开源项目学习总结

    一.实现日期格式数据类型的转换 首先,定义DateConverter实现Converter<String, Date>接口: package com.stevlu.common; impo ...

  10. C语言scanf函数详细解释(转载)

    原文地址:https://blog.csdn.net/21aspnet/article/details/174326 scanf 函数名: scanf 功 能: 执行格式化输入 用 法: int sc ...