首页 技术 正文
技术 2022年11月13日
0 收藏 862 点赞 2,782 浏览 2759 个字

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

POJ Monthly–2004.06.27 srbga@POJ


求次小生成树,看看是不是和最小一样方法: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 ;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287