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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+;
typedef long long ll;
using namespace std;
int pre[maxn],n,m,first; struct node
{
int x,y,val;
int u;
int e;
int d;
} p[maxn];
int find(int x)
{
if(pre[x]==x)
{
return x;
}
else
{
return pre[x]=find(pre[x]);
}
}
int prime()
{
int i,j,k,sum,num;
sum=;num=;
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<=m;i++) {
if(p[i].d) continue;
int fx=find(p[i].x);
int fy=find(p[i].y);
if(fx!=fy) {
num++;
pre[fx]=fy;
sum+=p[i].val;
if(first)
p[i].u=;
}
if(num==n-) break;
}
return sum;
}
bool cmp(node x,node y)
{
if(x.val<y.val)
return true;
else
return false;
}
int main()
{
int k,u,v,w,sum1,sum2;
int T;
scanf("%d",&T);
while(T--)
{
sum1=sum2=;
memset(p,,sizeof(p));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].val);
}
for(int i=;i<=m;i++) {
for(int j=i+;j<=m;j++)
{
if(p[i].val==p[j].val) p[i].e=;
}
}
sort(p+,p++m,cmp);
first=;
sum1=prime();
first=;
bool flag=false;
for(int i=;i<=m;i++)
{
if(p[i].u && p[i].e)
{ p[i].d=;
sum2=prime();
if(sum1==sum2)
{
flag=true;
printf("Not Unique!\n");
break;
}
}
}
if(!flag)
printf("%d\n",sum1);
}
}

The Unique MST(最小生成树的唯一性判断)的更多相关文章

  1. K - The Unique MST (最小生成树的唯一性)

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

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

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

  3. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  4. POJ 1679 The Unique MST (最小生成树)

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

  5. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

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

  6. The Unique MST (判断是否存在多个最小生成树)

    The Unique MST                                                                        Time Limit: 10 ...

  7. POJ 1679 The Unique MST 【判断最小生成树是否唯一】

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

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

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

  9. poj1679 The Unique MST(最小生成树唯一性)

    最小生成树的唯一性,部分参考了oi-wiki 如果一条不在最小生成树边集内的边,它可以替换一条在最小生成树边集内,且权值相等的边,那么最小生成树不是唯一的 同过kruskal来判断 考虑权值相等的边, ...

随机推荐

  1. HiddenHttpMethodFilter进行请求过滤

    基于 HiddentHttpMethodFilter 的示例 作用: 由于浏览器 form 表单只支持 GET 与 POST 请求,而 DELETE.PUT 等 method 并不支持,Spring3 ...

  2. Python解决网吧收费系统,远控网吧电脑设备!

    python破解网吧收费系统,远控网吧电脑设备! 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更 ...

  3. 微信小程序之回到顶部的两种方式

    第一种:使用view标签形式回到顶部 WXML: <image src='../../img/button-top.png' class='goTop' hidden='{{!floorstat ...

  4. 关于import android.support.v4.app.ContextCompat;找不到contextcompat的解决方法

    android迁移到了androidx,那么相关库的import就有问题了,需要转变为androidx的,这里比如 import android.support.v4.app.ContextCompa ...

  5. 主成分分析PCA数据降维原理及python应用(葡萄酒案例分析)

    目录 主成分分析(PCA)——以葡萄酒数据集分类为例 1.认识PCA (1)简介 (2)方法步骤 2.提取主成分 3.主成分方差可视化 4.特征变换 5.数据分类结果 6.完整代码 总结: 1.认识P ...

  6. C#算法设计排序篇之10-桶排序(附带动画演示程序)

    桶排序(Bucket Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/693 访问. 桶排序的工作原理是将数组 ...

  7. 一个基于 Beego 的,能快速创建个人博客,cms 的系统

    学习beego时候开发的一个博客系统,在持续完善,有不足之处,望大佬们多多体谅,并且指出.感谢! Go Blog 一个基于Beego的,能快速创建个人博客,cms 的系统 包含功能 查看 Go Blo ...

  8. Java的注解浅析

    人的一生就像一篇文章,只有经过多次精心修改,才能不断完善 Java注解概念理解: Java注解又称为Java标注,是JDK5引入的一中注释机制,Java中大家熟悉的五种注解分别是:@Override, ...

  9. 深度学习 | sklearn的train_test_split()各函数参数含义解释(超级全)

    在机器学习中,我们通常将原始数据按照比例分割为"测试集"和"训练集",从 sklearn.model_selection 中调用train_test_split ...

  10. eric4 编译 中文 控件 报错 解决

    eric4 在qt设计师界面, 设计 中文名控件 时,有时候不能编译,报错如下: 解决办法: 打开eric4---setting----preferences 按下图操作后 ,重新启动eric4即可解 ...