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. linux下 Mysql 安装使用

    .安装mysql sudo apt-get install mysql-server mysql-client sudo service mysql restart 2.5.7版本以上修修改updat ...

  2. docker实验--redis集群搭建

    背景介绍: 我经常在做一些小项目的时候,采用了Redis来做缓存,但是都是基于单节点的,一旦redis挂了,整个项目就挂了.于是乎,想到了多节点集群的方式来使用,就开始折腾着怎么去搭建这个集群.在网上 ...

  3. PageObjec页面对象模式(理论)

    ui自动化测试的分层思想:实现测试数据与业务数据分离 1. 基础层 2. 对象层:每个页面的操作元素封装为一个文件 3.测试用例层:调用对象层封装的方法进行测试用例编写

  4. django-ckedit

    (转载) 在django项目中使用django-ckeditor   安装django-ckeditor pip install django-ckeditor 安装Pillow Pillow是pyt ...

  5. Java网络编程(一)Socket套接字

    一.基础知识 1.TCP:传输控制协议. 2.UDP:用户数据报协议. 二.IP地址封装 1.InetAddress类的常用方法 getLocalHost() 返回本地主机的InetAddress对象 ...

  6. 两种unity双击事件

    有时候需要用到双击事件,而unity未提供双击控件,在此提供两种双击事件方法,进攻参考: 1)此方法为通过unityevent来实现 首先新建image(或其他不带点击事件的控件),添加如下脚本,然后 ...

  7. vue 列表的排序过渡 shuffle遇到的问题

    内部的实现,Vue 使用了一个叫 FLIP 简单的动画队列使用 transforms 将元素从之前的位置平滑过渡新的位置 需要注意的是使用 FLIP 过渡的元素不能设置为 display: inlin ...

  8. marquee滚动标签

    marquee语法    <marquee></marquee> 实例一<marquee>Hello, World</marquee> marquee常 ...

  9. SpringBoot整合FastJson(七)

    一.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson&l ...

  10. Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException 异常

    Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException 报此异常是应为有相同的bean ...