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. CodeForces - 782B The Meeting Place Cannot Be Changed(精度二分)

    题意:在一维坐标轴上,给定n个点的坐标以及他们的最大移动速度,问他们能聚到某一点处的最短时间. 分析: 1.二分枚举最短时间即可. 2.通过检查当前时间下,各点的最大移动范围之间是否有交集,不断缩小搜 ...

  2. UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)

    题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成.第一项任务开始的时间不早于时刻0. 分析:按截止时间 ...

  3. tomcat迁移到weblogic的几个问题

    第1个问题: 异常描述:VALIDATION PROBLEMS WERE FOUND problem: cvc-enumeration-valid: string value '3.0' is not ...

  4. C++的模板类:不能将定义与声明写在不同文件中

    问题来源 今天看了orbslam2自带的第三方库DBoW2的TemplatedVocabulary.h文件,发现其中模板类的函数成员的定义与声明放在了同一个文件:同时发现,DBoW2的CMakeLis ...

  5. python3+Opencv 搭建环境和基本操作

    一.必备前提: Python3.5及以上版本.pip.windows环境 二.搭建opencv 该部分可以创建隔绝的Python环境来引入,参照virtualenv的使用 在目标的cmd窗口,依次输入 ...

  6. shell字典使用

    1.shell界面或脚本中 pcode=(["10"]="BeiJing" ["22"]="TianJin") echo ...

  7. SHIDOU

    1. arp    i  指定网卡     a 查看arp表,显示主机名称和ip      n 查看arp表,并且用ip显示而不是主机名称   2. 119 ~/M2/image-installer- ...

  8. Navicat for Mysql 11.2 的下载,安装与激活

    1. Navicat for Mysql 11.2 的下载 链接:https://pan.baidu.com/s/1w54F-MYTLuy4TQwpzUE7bQ       密码:zsfu 2.下载的 ...

  9. nodejs(9)使用arttemplate渲染动态页面

    使用arttemplate渲染动态页面 安装 两个包 npm i art-template express-art-template -S 自定义一个模板引擎 app.engine('自定义模板引擎的 ...

  10. 4. git目录探秘

    HEAD当前指向的分支信息.cconfig,当前仓库的配置信息,core,用户,远程,分支等信息.(命令操作其实就是修改当前config文件)refs---heads,其实就是分支,里面包含所有的分支 ...