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!
方法1:首先算出最小生成树的权值和ans,然后枚举删除最小生成树中的每一条边,若还可以达到相同的效果,就说明最小生成树不唯一,
因为两个不同的最小生成树至少有一条边不同,所以我们才可以枚举删除每一条边.
方法2:判断最小生成树和次小生成树的权值是否相同.
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=;
int f[maxn];
struct node
{
int u,v,w;
bool operator < (const node &r)const{
return w<r.w;
}
}q[maxn];
int Find(int x)
{
return f[x]==x?x:f[x]=Find(f[x]);
}
int Merge(int u,int v)
{
u=Find(u);
v=Find(v);
if(u!=v)return f[u]=v,;
return ;
}
vector<int>v;
int main()
{
int T;
cin>>T;
while(T--){
v.clear();
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++)f[i]=i;
for(int i=;i<=m;i++){
cin>>q[i].u>>q[i].v>>q[i].w;
}
sort(q+,q++m);
int ans=;
for(int i=;i<=m;i++){
int x=Merge(q[i].u,q[i].v);
if(x){
v.push_back(i);
ans+=q[i].w;
}
}
int flag=;
for(int i=;i<v.size();i++){
int sum=,cnt=;
for(int j=;j<=n;j++)f[j]=j;
for(int j=;j<=m;j++){
if(j==v[i])continue;
int x=Merge(q[j].u,q[j].v);
if(x){
sum+=q[j].w;
cnt++;
}
}
if(cnt==n-&&ans==sum){
flag=;
break;
}
}
if(flag)cout<<ans<<endl;
else printf("Not Unique!\n"); }
return ;
}
#include<iostream>
#include<cstring> using namespace std;
typedef long long ll;
const int maxn=;
const int INF=0x3f3f3f3f;
int Maxlen[maxn][maxn];
int dis[maxn],vis[maxn];
int pre[maxn],MAP[maxn][maxn];
int used[maxn][maxn];
int n,m; int Prim(int x)
{
memset(Maxlen,,sizeof(Maxlen));
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
memset(pre,,sizeof(pre));
memset(used,,sizeof(used));
for(int i=;i<=n;i++){
dis[i]=MAP[x][i];
pre[i]=x;
}
dis[x]=;
vis[x]=;
pre[x]=;
int ans=;
for(int i=;i<=n;i++){
int u=,minn=INF;
for(int j=;j<=n;j++){
if(!vis[j]&&dis[j]<minn){
u=j;
minn=dis[j];
}
}
vis[u]=;
ans+=minn;
used[u][pre[u]]=used[pre[u]][u]=;
for(int v=;v<=n;v++){
if(vis[v]){
Maxlen[u][v]=Maxlen[v][u]=max(Maxlen[v][pre[u]],dis[u]);
}
else{
if(dis[v]>MAP[u][v]){
dis[v]=MAP[u][v];
pre[v]=u;
}
}
}
}
return ans;
}
void sst(int ans)
{
int sum=INF;
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(!used[i][j]&&MAP[i][j]!=INF){
sum=min(sum,ans+MAP[i][j]-Maxlen[i][j]);
}
}
}
if(sum==ans)cout<<"Not Unique!"<<endl;
else cout<<ans<<endl;
}
int main()
{
int T;
cin>>T;
while(T--){
cin>>n>>m;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j)MAP[i][j]=;
else MAP[i][j]=INF;
}
}
for(int i=;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
MAP[u][v]=MAP[v][u]=min(MAP[u][v],w);
}
int ans=Prim();
sst(ans);
}
return ;
}

K - The Unique MST (最小生成树的唯一性)的更多相关文章

  1. The Unique MST(最小生成树的唯一性判断)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  2. K - The Unique MST

    K - The Unique MST #include<iostream> #include<cstdio> #include<cstring> #include& ...

  3. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  4. POJ 1679 The Unique MST (最小生成树)

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

  5. K - The Unique MST - poj 1679

    题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...

  6. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

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

  7. poj1679 The Unique MST(最小生成树唯一性)

    最小生成树的唯一性,部分参考了oi-wiki 如果一条不在最小生成树边集内的边,它可以替换一条在最小生成树边集内,且权值相等的边,那么最小生成树不是唯一的 同过kruskal来判断 考虑权值相等的边, ...

  8. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

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

  9. The Unique MST (判断是否存在多个最小生成树)

    The Unique MST                                                                        Time Limit: 10 ...

随机推荐

  1. NumPy - 数组(定义,拼接)

    NumPy 教程(数组) set_printoptions(threshold='nan') NumPy的数组中比较重要ndarray对象属性有: ndarray.ndim:数组的维数(即数组轴的个数 ...

  2. IE8Get请求中文不兼容:encodeURI的使用

    IE8Get请求中文不兼容:encodeURI的使用 在开发过程中遇到在IE8下,请求出错. 后发现Get请求中含有中文字符. 使用js自带的encodeURI函数对中文进行编码,问题解决. enco ...

  3. JZOJ-TGB817-SOL

    T1 题面 "封印大典启动,请出Nescafe魂珠!"随着圣主applepi一声令下,圣剑护法rainbow和魔杖护法freda将Nescafe魂珠放置于封印台上.封印台是一个树形 ...

  4. 运行xv6

    我们使用Qemu在Ubuntu下运行 1. 安装Qemu sudo apt-get install qemu 执行 qemu-system-i386 ,如果弹出Qemu界面说明安装成功了 2. 编译x ...

  5. Python中Opencv和PIL.Image读取图片的差异对比

    近日,在进行深度学习进行推理的时候,发现不管怎么样都得不出正确的结果,再仔细和正确的代码进行对比了后发现原来是Python中不同的库读取的图片数组是有差异的. image = np.array(Ima ...

  6. c++ 深度优先算法

    #include <iostream> using namespace std; #define VertexNum 9 /*定义顶点数*/ struct Node /*声明图形顶点结构* ...

  7. 谈Web前端-html

    什么是HTML?      HTML 是用来描述网页的一种语言: HTML 值得是超文本标记语言:Hyper Text Markup Language      HTML 不是一种编程语言,而是一种标 ...

  8. 吴裕雄--天生自然 JAVASCRIPT开发学习: HTML DOM - 改变CSS

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. python numpy和矩阵

    2.numpy数据选取 lst=[[1, 2, 3], [4, 5, 6]] np.array(lst)[:-1] Out[32]: array([[1, 2, 3]]) np.array(lst)[ ...

  10. uni-app真机调试报错request:fail abort解决方法

    Android端真机调试访问本地接口数据时报错:request:fail abort 报错代码 onLoad: function(e) { uni.request({ url: 'http://loc ...