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. Head First设计模式——装饰者模式

    前言:对于设计模式我们有时候在想是否有必要,因为实际开发中我们没有那么多闲工夫去套用这么多设计模式,也没有必要为了模式而模式. 通常这些模式会引入新的抽象层,增加代码的复杂度,但是当我们掌握了这些设计 ...

  2. POJ 1035 Spell checker(串)

    题目网址:http://poj.org/problem?id=1035 思路: 看到题目第一反应是用LCS ——最长公共子序列 来求解.因为给的字典比较多,最多有1w个,而LCS的算法时间复杂度是O( ...

  3. 研究了3天,终于将 Shader 移植到 Cocos Creator 2.2.0 上了!

    预览 扫光特效-Fluxay2 马赛克像素特效-Mosaic 过渡效果-Transfer Shawn 花了3天时间,研究了Cocos Creator 2.2.0 的 Effect 语法,终于在1024 ...

  4. FTPClient连续读取文件

    最近在使用FTPClient连续读取ftp上的多个文件内容时,遇到了两个问题: 1. 在for循环中,FTPClient只能读取到第一个文件内容,读取第二个时遇到NPE问题. 2. 遇到程序锁死. 下 ...

  5. Django之视图层的简介与使用

    Django的View(视图) 一个视图函数(可以是类),简称视图,是一个简单的Python 函数(可以是类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个 ...

  6. dapper之连接数据库(Oracle,SQL Server,MySql)

    因为项目需求,需要项目同时可以访问三个数据库,因此本人经过一番研究,得出以下代码. 1.建立公共连接抽象类(DataBase) public abstract class DataBase { /// ...

  7. JVM 垃圾收集与内存分配

    判断对象是否还活着 引用计数法 给对象添加引用计数器,添加加1,引用失效减1,如果为0就是不可使用的.问题是不能解决互相引用带来的问题 可达性分析法 以GC Roots为起点,判断到一个对象是否有引用 ...

  8. [Tarjan系列] Tarjan算法与有向图的SCC

    前面的文章介绍了如何用Tarjan算法计算无向图中的e-DCC和v-DCC以及如何缩点. 本篇文章资料参考:李煜东<算法竞赛进阶指南> 这一篇我们讲如何用Tarjan算法求有向图的SCC( ...

  9. N042第一周

    1.按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别. slackware:SUSE Linux Enterprise Server,OpenSuse debian:ubuntu,dee ...

  10. redis入门(一)

    目录 redis入门(一) 前言 特性 速度快 简单稳定 丰富的功能 历史 历史版本 安装与启动 安装 数据类型与内部编码 数据结构 内部编码 常用API与使用场景 常用命令 字符串 列表 哈希 集合 ...