The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28673   Accepted: 10239

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!

Source


求次小生成树,看看是不是和最小一样
方法:
kruskal求MST同时建图,dfs转有根树同时递推f[i][j]为i到j在树上的路径中权值最大是多少O(n^2)
次小一定是最小加一条边减一条边得到,枚举加哪一条边比较w和f[u][v]
//
// main.cpp
// poj1679
//
// Created by Candy on 10/11/2016.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,u,v,w;
struct edge{
int v,w,ne;
}e[N<<];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
struct data{
int u,v,w;
bool operator <(const data &r)const{return w<r.w;}
}a[M];
int fa[N];
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int use[N];
int kruskal(){
sort(a+,a++m);
int cnt=,ans=;
for(int i=;i<=m;i++){
int u=a[i].u,v=a[i].v,w=a[i].w;
int f1=find(u),f2=find(v);
if(f1==f2) continue;
fa[f1]=f2;
ins(u,v,w);
use[i]=;
cnt++; ans+=w;
if(cnt==n-) break;
}
return ans;
}
int f[N][N],vis[N];
void dfs(int u){
vis[u]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(vis[v]) continue;
for(int x=;x<=n;x++) if(vis[x]) f[x][v]=f[v][x]=max(f[x][u],w);
dfs(v);
}
}
void sol(){
cnt=;memset(h,,sizeof(h));
memset(f,,sizeof(f));
memset(vis,,sizeof(vis));
memset(use,,sizeof(use));
for(int i=;i<=n;i++) fa[i]=i; int ans=kruskal();
dfs();
int mn=INF;
for(int i=;i<=m;i++) if(!use[i]){
int u=a[i].u,v=a[i].v,w=a[i].w;//printf("hi %d %d %d %d\n",u,v,w,f[u][v]);
mn=min(mn,w-f[u][v]);
}
if(mn==) puts("Not Unique!");
else printf("%d\n",ans);
}
int main(int argc, const char * argv[]){
int T=read();
while(T--){
n=read();m=read();
for(int i=;i<=m;i++){a[i].u=read();a[i].v=read();a[i].w=read();}
sol();
}
return ;
}
 
 

POJ1679 The Unique MST[次小生成树]的更多相关文章

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

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

  2. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

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

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

  4. POJ_1679_The Unique MST(次小生成树)

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

  5. POJ_1679_The Unique MST(次小生成树模板)

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

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

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

  7. poj1679The Unique MST(次小生成树模板)

    次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...

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

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

  9. poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

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

随机推荐

  1. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  2. php实现设计模式之 原型模式

    <?php /* * 原型模式:通过复制已经存在的对象来创建新对象. * 通过原型实例指定创建对象的种类,并且通过copy这些原型创建信的对象 * 是创建型模式 * 有的时候创建一个对象有很多步 ...

  3. Golang(笔记) 顺序编程

    package main import ( "fmt" "bufio" "io" "os" "strconv& ...

  4. iOS源代码管理工具

    源代码管理工具简介 1.为什么会出现源代码管理工具? 为了解决在软件开发过程中,由源代码引发的各种蛋疼.繁琐的问题 2.源代码管理不当可能会引起的后果? 无法后悔:做错了一个操作后,不能回到之前的操作 ...

  5. TextInputLayout setError() setErrorEnable()

    public void afterTextChanged(Editable s) { if (textInputLayout.getEditText().getText().toString().le ...

  6. vim中tab转为空格

    :set ts=4:set expandtab:%retab!

  7. ORACLE字符集基础知识

    概念描叙    ORACLE数据库有国家字符集(national character set)与数据库字符集(database character set)之分.两者都是在创建数据库时需要设置的.国家 ...

  8. native2ascii 使用说明

    native2ascii.exe 是Java的一个文件转码工具,是将特殊各异的内容转为用指定的编码标准文体形式统一的表现出来,它通常位于JDK_home\bin目录下,安装好Java SE后,可在命令 ...

  9. .NET项目开发—浅谈面向对象的纵横向关系、多态入口,单元测试(项目小结)

    阅读目录: 1.开篇介绍 2.使用委托消除函数串联调用 2.1.使用委托工厂转换两个独立层面的对象 3.多态入口(面向对象继承体系是可被扩展的) 4.多态的受保护方法的单元测试(Protected成员 ...

  10. 从零自学Hadoop(06):集群搭建

    阅读目录 序 集群搭建 监控 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 上一 ...