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. Mybitis根据工具类反射数据库生成映射+整合springboot

    一 反向生成数据库mapper的工具类: 添加依赖 <dependency> <groupId>org.mybatis.generator</groupId> &l ...

  2. Linux学习笔记之配置网络

    1.打开VMware Workstation虚拟机 2.在VMware下安装虚拟ubunt系统后配置网络,如图所示配置,即可. 3.检查笔记本所使用的网段 ①按 “win + R ”键,并输入“cmd ...

  3. Windows下使用命令行编译Qt项目(解决DLL丢失问题)

    一.前言 我之前用Qt做了个hello world,结果各种报错,一大堆DLL找不到,今天用命令行编译就通过了 二.准备工作 1.Visual Studio(有nmake就行) 2.Qt 3.把qma ...

  4. SpringBoot2 整合Ehcache组件,轻量级缓存管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.Ehcache缓存简介 1.基础简介 EhCache是一个纯Java的进程内缓存框架,具有快速.上手简单等特点,是Hibernate中默认 ...

  5. java方法中参数传递与随机点名器、库存管理案例

    一 参数传递 1.定义: 参数传递,可以理解当我们要调用一个方法时,我们会把指定的数值,传递给方法中的参数, 这样方法中的参数就拥有了这个指定的值,可以使用该值,在方法中运算了.这种传递方式,我 们称 ...

  6. 解决pgAdmin4启动失败方法

    1. 问题现象 有时pgadmin 4启动仅显示启动界面, 或者 点击图标一直都没反应,启动界面用鼠标点击下就消失了, 然后过很长时间就保错: the application server could ...

  7. Vulnhub篇Photographerr

    0x00 靶机信息 靶机:Photographerr:1 难度:中 下载:https://www.vulnhub.com/entry/photographer-1,519/ 0x01 信息收集 靶场网 ...

  8. python设计模式之模板模式

    python设计模式之模板模式 编写优秀代码的一个要素是避免冗余.在面向对象编程中,方法和函数是我们用来避免编写冗余代码的重要工具. 现实中,我们没法始终写出100%通用的代码.许多算法都有一些(但并 ...

  9. 用过 mongodb 吧, 这三个大坑踩过吗?

    一:背景 1. 讲故事 前段时间有位朋友在微信群问,在向 mongodb 中插入的时间为啥取出来的时候少了 8 个小时,8 在时间处理上是一个非常敏感的数字,又吉利又是一个普适的话题,后来我想想初次使 ...

  10. HotSpot的垃圾回收器

    如果说收集算法是内存回收的方法论,那么垃圾收集器就是内存回收的具体实现.这里讨论的收集器基于JDK 1.7 Update 14之后的 HotSpot 虚拟机,这个虚拟机包含的所有收集器如下图所示 上图 ...