首页 技术 正文
技术 2022年11月18日
0 收藏 614 点赞 3,709 浏览 2307 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901