http://poj.org/problem?id=1679

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


Sample Output

Not Unique!

题意:

给n点m边无重边,求最小生成树是否唯一,如果这棵最小生成树是唯一的那么就输出最小生成树上权的和,不是唯一的就输出Not Unique!

思路:

求次小生成树,如果和最小生成树结果一样则不唯一。

次小生成树:

次小生成树由最小生成树变化而来,通过最小生成树概念可以知道“次小”只需要通过变化最小生成树上的一条边实现,并且要使得这种变化是最小。

给出一篇写的挺好的博客:https://blog.csdn.net/qq_27437781/article/details/70821413


from:https://blog.csdn.net/u011721440/article/details/38735547

判断最小生成树是否唯一:

1、对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边。

2、用kruskal或prim求出MST。

3、如果MST中无标记的边,则MST唯一;否则,在MST中依次去掉标记的边,再求MST,若求得MST权值和原来的MST权值相同,则MST不唯一。


from:https://blog.csdn.net/blue_skyrim/article/details/51338375

次小生成树的求法是枚举最小生成树的每条边,把其中一条边去掉,找到这两点上其他的边,剩下的边形成最小生成树


kuangbin大佬的博客:https://www.cnblogs.com/kuangbin/p/3147329.html

思路:

求最小生成树时,用数组maxval[i][j]来表示MST中i到j最大边权,求完后,直接枚举所有不在MST中的边,替换掉最大边权的边,更新答案
,注意点的编号从0开始

原理:

最小生成树上的不相邻的两点相连必定成成为一个环,所以我们可以尝试枚举这些不相邻的点使他们相连,再删除环中属于最小生成树的最大边(令当前被确定的点为u,已经被确定的点为v,则u--v路径中最大的边要么来自v--pre[u]路径中的最大,要么就是当前被确定的边lowval[u],dp的思想),这样既保证树的结构又能使树的变化最小。这些枚举中最小的结果即为次小生成树。

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxn=;
using namespace std; int G[maxn][maxn];//邻接矩阵
int vis[maxn];//判断点有没在最小生成树中
int pre[maxn];//每个点的双亲
int lowval[maxn];//辅助数组
int maxval[maxn][maxn];//maxval[i][j]表示在最小生成树中从i到j的路径中的最大边权
int used[maxn][maxn];//判断这条边是否在最小生成树中使用过
int MST;//最小生成树权值和 int Prim(int n,int st)//n为顶点的个数,st为最小生成树的开始顶点
{
fill(lowval,lowval+n,INF);
memset(maxval,,sizeof(maxval));
memset(pre,-,sizeof(pre));
memset(used,,sizeof(used));
memset(vis,,sizeof(vis));
int ans=;
lowval[st]=;
vis[st]=;
for(int i=;i<n;i++)
{
if(i!=st&&G[st][i]!=INF)
{
lowval[i]=min(lowval[i],G[st][i]);
pre[i]=st;
}
}
for(int k=;k<n-;k++)
{
int MIN=INF;
int t=-;
for(int i=;i<n;i++)
{
if(vis[i]==&&lowval[i]<MIN)
{
MIN=lowval[i];
t=i;
}
}
// if(MIN==INF) return -1;
ans+=MIN;
vis[t]=;
used[t][pre[t]]=used[pre[t]][t]=;//标记这条边在最小生成树中
for(int i=;i<n;i++)
{
if(vis[i])
maxval[t][i]=maxval[i][t]=max(maxval[i][pre[t]],lowval[t]);
if(i!=t&&!vis[i]&&G[t][i]<lowval[i])
{
pre[i]=t;
lowval[i]=G[t][i];
}
}
}
return ans;
} int Judge(int n)
{
int MIN=INF;
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
if(G[i][j]!=INF && !used[i][j])//边不在最小生成树中
MIN=min(MIN,MST-maxval[i][j]+G[i][j]);
}
}
return MIN;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d %d",&n,&m);
memset(G,INF,sizeof(G));
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
u--;v--;//使标号从0开始
G[u][v]=w;
G[v][u]=w;
}
MST=Prim(n,);
if(MST==Judge(n))//最小生成树和次小生成树总权值相等
printf("Not Unique!\n");
else
printf("%d\n",MST);
}
return ;
}

POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)的更多相关文章

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

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

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

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

  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 (次小生成树(sec_mst)【kruskal】)

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

  5. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  6. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

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

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

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

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

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

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

  10. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

随机推荐

  1. 基础语法-循环结构while

    基础语法-循环结构while 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.while语句格式 while(条件表达式){ 执行语句; } 二.while语句案例 /** * ...

  2. UVA - 820 Internet Bandwidth (因特网带宽)(最大流)

    题意:给出所有计算机之间的路径和路径容量后,求出两个给定结点之间的流通总容量.(假设路径是双向的,且两方向流动的容量相同) 分析:裸最大流.标号从1开始,初始化的时候注意. #pragma comme ...

  3. 每天一点点之laravel框架开发 - API通过access_token获取用户id报 Unauthenticated. 错误(passport)

    1.首先保证你的config/auth.php 中 guards 的 api 的 driver 选项改为 passport 2.注册中间件,在 app/Http/Kernel.php 文件中的 $ro ...

  4. JAVAEE 和项目开发(第二课:HTTP协议的特点和交互流程)

    HTTP 的概念和介绍 概念:超文本传输协议(Hyper Text Transfer Protocol) 作用:规范了浏览器和服务器的数据交互 特点: 简单快速:客户向服务器请求服务时,只需传送请求方 ...

  5. 吴裕雄--天生自然 JAVASCRIPT开发学习:语句

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

  6. PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]

    题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...

  7. Spring Boot作为Spring Cloud基础设施

    spring cloud包含的核心特性: Distributed/versioned configuration(分布式配置) Service registration and discovery(服 ...

  8. C语言备忘录——向上取整

    众所周知,C语言的取整方式是向下取整,昨天老师留了一道思考题,问我们C语言怎么向上取整,当时我第一反应就是ceil(),老师说不能用if……else之类的,函数也不行.当时想了想没事不用就不用,去ma ...

  9. torch文档学习笔记

    下面为官方文档学习笔记    http://pytorch.org/docs/0.3.0/index.html 1.torch.Tensor from __future__ import print_ ...

  10. 创建简单spring boot项目

    简介 使用spring boot可以轻松创建独立的,基于Spring框架的生产级别应用程序.Spring boot应用程序只需要很少的spring配置 特点 创建独立的Spring应用程序 直接嵌入t ...