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. IOS学习笔记之获取Plist文件读取数据

    @property(nonatomic,strong) NSArray *pic; //创建数组属性 @property(nonatomic,assign) int index; //创建索引属性 @ ...

  2. VS2015开发Android,自带模拟器无法调试、加载程序,算是坑吗

    VS2015出来后,确定变化很大,什么android.ios的,不在话下.对于我这样传统型的人,也第一时间试用了一下(vs2003->vs2008->vs2012->vs2015). ...

  3. java web学习总结(十一) -------------------基本概念使用Cookie进行会话管理

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  4. Html5绘制饼图统计图

    这里要介绍的是一个jQuery插件:jquery.easysector.js Html5提供了强大的绘图API,让我们能够使用javascript轻松绘制各种图形.本文将主要讲解使用HTML5绘制饼图 ...

  5. SQL多表查询

    设置主键:点击右键设为主键,在默认值那设置newid(),即可自动生成 Join inner join(内连接):select * from 表1 inner join 表2 on 表1.列 = 表2 ...

  6. 春节快乐!推荐一个关于 SharePoint 和 BI 的视频,笑死我了

    在春节即将来临的日子里,我偶然看到了这个相见恨晚的视频:Attractive Business Intelligence 我几乎是从头笑到尾看完的,太有趣儿了!特别适合放假.过节的时候看.本来今天下午 ...

  7. 友盟SDK实现分享

    友盟SDK文档已经写得很详细了,这边整理笔记,先过一遍流程: 1⃣️注册友盟账号以获取Appkey,下面以分享到微信为例 2⃣️申请第三方账号是因为要进行分享.授权这样的操作肯定是要通过第三方的审核( ...

  8. iOS开发之功能模块--长方形UIImage截取中间最大正方形区域

    这里直接用CoreGraphics的一些处理图片的方法,本身不难,但是有些时候用的不多,就会遗忘掉使用方法的细节.下面就直接展示关键源码,以便下次重复需求,就可以立马找回. 该方法中在UIImage的 ...

  9. HashSet 浅析示例

    * 1.继承自抽象类 AbstractSet,实现接口 Set.Cloneable.Serializable: * 2.元素无顺序: * 3.元素不可重复: * 4.采用哈希算法插入数据,插入速度快: ...

  10. MVC学习系列3--怎么从控制器向视图传递数据

    在MVC中,从控制器到视图,传递数据,可以使用 ViewData 和 ViewBag:同样从视图到控制器,传递数据,可以使用Post,QueryString,或者隐藏域:最后从控制器到控制器,传递数据 ...