可以依次枚举MST上的各条边并删去再求最小生成树,如果结果和第一次求的一样,那就是最小生成树不唯一。

用prim算法,时间复杂度O(n^3)。

 #include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 111
#define INF (1<<30)
struct Edge{
int u,v;
}edge[MAXN];
int NE; int n,G[MAXN][MAXN];
int lowcost[MAXN],nearvex[MAXN];
int prim(bool statue){
for(int i=;i<=n;++i) lowcost[i]=INF;
lowcost[]=;
int res=;
for(int i=; i<n; ++i){
int u=-,min=INF;
for(int v=; v<=n; ++v){
if(lowcost[v]!=- && lowcost[v]<min){
min=lowcost[v];
u=v;
}
}
if(u==-) return -;
if(statue && u!=){
edge[NE].u=nearvex[u]; edge[NE].v=u;
++NE;
}
lowcost[u]=-;
res+=min;
for(int v=; v<=n; ++v){
if(lowcost[v]!=- && lowcost[v]>G[u][v]){
lowcost[v]=G[u][v];
nearvex[v]=u;
}
}
}
return res;
} bool isUnique(int res){
for(int i=; i<NE; ++i){
int tmp=G[edge[i].u][edge[i].v];
G[edge[i].u][edge[i].v]=G[edge[i].v][edge[i].u]=INF;
if(prim()==res) return ;
G[edge[i].u][edge[i].v]=G[edge[i].v][edge[i].u]=tmp;
}
return ;
} int main(){
int t,m,a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i){
for(int j=; j<=n; ++j) G[i][j]=INF;
}
for(int i=; i<m; ++i){
scanf("%d%d%d",&a,&b,&c);
G[a][b]=G[b][a]=c;
}
NE=;
int res=prim();
if(isUnique(res)) printf("%d\n",res);
else puts("Not Unique!");
}
return ;
}

有O(n^2)的算法,详见http://www.cnblogs.com/hxsyl/p/3290832.html

算法,需要求出MST上任意两点路径上的最长边,删除这条边会形成两个连通分支而那两点就分别在这两个连通分支里;

然后依次枚举所有不在MST上的边,删除边上两点路径上的最长边并加入MST,这样就构成另外一颗生成树了,并且是存在该边的前提下的最小生成树。

计算MST任意两点路径上的最长边,可以在prim算法的过程中求出:每当一个点加入T集合前,计算出所有T集合的点到该点的最长边。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111
#define INF (1<<30) int n,G[MAXN][MAXN];
bool vis[MAXN][MAXN];
int lowcost[MAXN],nearvex[MAXN],maxedge[MAXN][MAXN];
int T[MAXN],NT;
int prim(){
memset(vis,,sizeof(vis));
memset(maxedge,,sizeof(maxedge));
NT=;
nearvex[]=;
for(int i=; i<=n; ++i) lowcost[i]=INF;
lowcost[]=; int res=;
for(int i=; i<n; ++i){
int u=-,mincost=INF;
for(int v=; v<=n; ++v){
if(lowcost[v]!=- && lowcost[v]<mincost){
mincost=lowcost[v];
u=v;
}
} vis[nearvex[u]][u]=vis[u][nearvex[u]]=;
for(int i=; i<NT; ++i) maxedge[T[i]][u]=maxedge[u][T[i]]=max(maxedge[T[i]][nearvex[u]],mincost);
T[NT++]=u; res+=mincost;
lowcost[u]=-;
for(int v=; v<=n; ++v){
if(lowcost[v]!=- && lowcost[v]>G[u][v]){
lowcost[v]=G[u][v];
nearvex[v]=u;
}
}
}
return res;
} int SMST(){
int res=INF;
for(int i=; i<=n; ++i){
for(int j=i+; j<=n; ++j){
if(vis[i][j] || G[i][j]==INF) continue;
res=min(res,G[i][j]-maxedge[i][j]);
}
}
return res;
} int main(){
int t,m,a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i){
for(int j=; j<=n; ++j) G[i][j]=INF;
}
while(m--){
scanf("%d%d%d",&a,&b,&c);
G[a][b]=G[b][a]=c;
}
int res=prim();
if(SMST()==) puts("Not Unique!");
else printf("%d\n",res);
}
return ;
}

POJ1679 The Unique MST(次小生成树)的更多相关文章

  1. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

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

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

  3. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

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

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

  5. POJ_1679_The Unique MST(次小生成树)

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

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

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

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

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

  8. poj1679The Unique MST(次小生成树模板)

    次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...

  9. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  10. poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

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

随机推荐

  1. Ninth scrum meeting - 2015/11/3

    今天课上老师询问了每个团队的进度,我们发现有好多团队都已经基本完成了, 距离预定的alpha版本开发完成时间也越来越近了,我们的工作也都在有条不紊的进行着.今天又出现了git pull时有冲突的情况, ...

  2. 使用 nginx + thin 的配置启动 rails server

    http://www.iwangzheng.com 在大师的指导下配置了新的服务器的nginx,通过top命令查看了服务器是8个cpu的,所以起了8个端口,把它们都映射到一个总的端口3600上,需要在 ...

  3. bwa的使用方法

    bwa的使用需要两中输入文件:    Reference genome data(fasta格式 .fa, .fasta, .fna)    Short reads data (fastaq格式 .f ...

  4. twisted udp编程

    概述 Unlike TCP, UDP has no notion of connections. A UDP socket can receive datagrams from any server ...

  5. 反转字符串--C和Python

    将字符串反转,即“abcde”->"edcba" C语言实现: [转自http://www.kanzhun.com/mianshiti/456.html?sid=mail_1 ...

  6. Ubuntu系统如何查看硬件配置信息

    查看ubuntu硬件信息 1, 主板信息 .查看主板的序列号 -------------------------------------------------- #使用命令 dmidecode | ...

  7. wireshark http抓包命令行详解

    This article is a quick and easy HowTo detailing the use of Wireshark or another network sniffing pr ...

  8. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  9. canvas API ,通俗的canvas基础知识(三)

    全文说到了三角形,圆形等相关图形的画法,不熟悉的同学可以出门右转,先看看前文,接下来继续我们的图形——曲线. 学过数学,或者是比较了解js 的同学都知道贝塞尔曲线,当然,在数学里面,这是一门高深的学问 ...

  10. July 21st, Week 30th Thursday, 2016

    What youth deemed crystal, age finds out was dew. 年少时的水晶,在岁月看来不过是露珠. As time goes by, we are gradual ...