The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35999   Accepted: 13145

Description

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!

C/C++:

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int my_max_edge = , my_max_node = ; int t, n, m, my_book_edge[my_max_edge], my_pre[my_max_node], my_first; struct edge
{
int a, b, val;
}P[my_max_edge]; bool cmp(edge a, edge b)
{
return a.val < b.val;
} int my_find(int x)
{
int n = x;
while (n != my_pre[n])
n = my_pre[n];
int i = x, j;
while (n != my_pre[i])
{
j = my_pre[i];
my_pre[i] = n;
i = j;
}
return n;
} int my_kruskal(int my_flag)
{
int my_ans = ;
for (int i = ; i <= n; ++ i)
my_pre[i] = i; for (int i = ; i < m; ++ i)
{
int n1 = my_find(P[i].a), n2 = my_find(P[i].b);
if (n1 == n2 || my_flag == i) continue;
my_pre[n1] = n2;
if (my_first)my_book_edge[i] = ;
my_ans += P[i].val;
} int temp = my_find();
for (int i = ; i <= n; ++ i)
if (temp != my_find(i))
return -;
return my_ans;
} int main()
{
scanf("%d", &t);
while (t --)
{
scanf("%d%d", &n, &m);
for (int i = ; i < m; ++ i)
scanf("%d%d%d", &P[i].a, &P[i].b, &P[i].val);
sort(P, P + m, cmp);
memset(my_book_edge, , sizeof(my_book_edge)); my_first = ;
int mst = my_kruskal(-), flag = ;
if (mst == -)
{
printf("0\n");
continue;
}
my_first = ;
for (int i = ; i < m; ++ i)
{
if (my_book_edge[i])
{
if (mst == my_kruskal(i))
{
printf("Not Unique!\n");
flag = ;
break;
}
}
}
if (flag) printf("%d\n", mst);
}
return ;
}

poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)的更多相关文章

  1. POJ 1679 The Unique MST (次小生成树)

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

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

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

  3. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

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

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

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

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

  6. POJ1679 The Unique MST —— 次小生成树

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  7. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  8. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  9. poj 1679 The Unique MST (判定最小生成树是否唯一)

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

随机推荐

  1. php 加入 unless 语法

    1. php 的版本 :PHP 7.3.0-dev (cli) (built: Mar 18 2018 00:28:55) ( NTS ) 2. unless 语法结构: unless($cond){ ...

  2. phpfpm的原理是什么?

    php-fpm是一种master(主)/worker(子)多进程架构,与nginx设计风格有点类似.master进程主要负责CGI及PHP环境初始化.事件监听.子进程状态等等,worker进程负责处理 ...

  3. django2-创建项目

    方式一:cmd或linux命令行下创建django项目(不常用,此处不做详细介绍) django-admin.py startproject autotest 方式二:使用pycharm专业版创建dj ...

  4. python3.x以上 爬虫 使用问题 urllib(不能使用urllib2)

    问题一: python 3.x 以上版本揽括了 urllib2,把urllib2 和 urllib 整合到一起. 并且引入模块变成一个,只有 import urllib # import urllib ...

  5. 关于ESET占用CPU严重 的解决方案||ESET CPU 100%||用迅雷时ESET占用CPU(6月22日再次更新)

    关于ESET占用CPU严重 的解决方案 本文根据原帖有适量删改. ESET 的杀毒软件历来以占用资源少,CPU消耗少著称,可是很多朋友(特别是中国大陆的朋友)反应ESS & EAV 间歇性占用 ...

  6. PHP 输出XML字符串

    <?php echo htmlentities( $xml_string);?>

  7. 用c语言实现前序创建二叉树(递归),分别用前序,中序,后序遍历,以及分别输出节点个数和叶子节点个数

    本人c语言小白一枚,近期在学习数据结构(c语言版),特写此随笔,做一些总结和分享,如有不当之处,请各位技术大牛斧正 首先我们用一个结构体来抽象树的结点,代码如下(这里我们存放的数据为char型,大家可 ...

  8. C语言博客作业006

    问题 答案 这个作业属于那个课程 C语言程序设计1 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/ 我在这个课程的目的是 学习并掌握 ...

  9. 【XSY2564】sequence

    Description [题目描述] 给定一个长度为n的由['0'..'9']组成的字符串s,v[i,j]表示由字符串s第i到第j位组成的十进制数字. 将它的某一个上升序列定义为:将这个字符串切割成m ...

  10. Pandas 筛选操作

    # 导入相关库 import numpy as np import pandas as pd 在数据处理过程中,经常会遇到要筛选不同要求的数据.通过 Pandas 可以轻松时间,这一篇我们来看下如何使 ...