The Unique MST
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!
算法过程:

先求出最小生成树,然后枚举每一条不再最小生成树上的边,并把这条边放到最小生成树上面,那么我们在这条环路上取出一条最长的路,处理新加入的那一条边,最终得到的权值就是最小生成树的权值

#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3fusing namespace std;
int n,m;
struct data
{int u,v,w;bool vis;
} p[20010];
vector<int>G[110];
int per[110],maxd[110][110];
bool cmp(data a,data b)
{return a.w < b.w;
}
int Union_Find(int x)
{return x == per[x] ? x: per[x] = Union_Find(per[x]);
}
void kruskal()
{sort(p,p+m,cmp);for(int i=0; i<=n; i++)//初始化{G[i].clear();G[i].push_back(i);per[i]=i;}int sum=0,k=0;//sum是最小生成树的值for(int i=0; i<m; i++){if(k==n-1)  break;//n个点,最多n-1条边int x1=Union_Find(p[i].u),x2=Union_Find(p[i].v);if(x1!=x2){k++;p[i].vis=1;//这条边已经用过了sum+=p[i].w;int len_x1=G[x1].size();int len_x2=G[x2].size();for(int j=0; j<len_x1; j++)//更新两点之间距离的最大值for(int k=0; k<len_x2; k++)maxd[G[x1][j]][G[x2][k]]=maxd[G[x2][k]][G[x1][j]]=p[i].w;//因为后面的边会越来越大,所以这里可以直接等于当前边的长度per[x1]=x2;int tem[110];for(int j=0; j<len_x2; j++)//现在已经属于一棵树了,那么我们就将点添加到相应的集合中tem[j]=G[x2][j];for(int j=0; j<len_x1; j++)G[x2].push_back(G[x1][j]);for(int j=0; j<len_x2; j++)G[x1].push_back(tem[j]);}}int cisum=INF;//次小生成树的权值for(int i=0; i<m; i++)if(!p[i].vis)cisum=min(cisum,sum+p[i].w-maxd[p[i].u][p[i].v]);if(cisum>sum)printf("%d\n",sum);elseprintf("Not Unique!\n");
}
int main()
{int T;scanf("%d\n",&T);while(T--){scanf("%d%d",&n,&m);for(int i=0; i<m; i++){scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);p[i].vis = false;}kruskal();}return 0;
}

POJ1679 Luogu4180 次小生成树相关推荐

  1. (POJ-1679)次小生成树模板

    原题链接:http://poj.org/problem?id=1679 The Unique MST Given a connected undirected graph, tell if its m ...

  2. poj1679(次小生成树)

    题意:判断最小生成树是否唯一,如果唯一则是输出最小长度:否则输出 Not Unique! 其中:枚举每条不在最小生成树上的边,并把这条边放到最小生成树上面,然后就一定形成环,那么我们将这条环中取出最长 ...

  3. 次小生成树(POJ1679/CDOJ1959)

    POJ1679 首先求出最小生成树,记录权值之和为MinST.然后枚举添加边(u,v),加上后必形成一个环,找到环上非(u,v)边的权值最大的边,把它删除,计算当前生成树的权值之和,取所有枚举加边后生 ...

  4. (luogu4180) [Beijing2010组队]次小生成树Tree

    严格次小生成树 首先看看如果不严格我们怎么办. 非严格次小生成树怎么做 由此,我们发现一个结论,求非严格次小生成树,只需要先用kruskal算法求得最小生成树,然后暴力枚举非树边,替换路径最大边即可. ...

  5. 【POJ1679】The Unique MST(非严格次小生成树)

    problem 给出一个连通无向图,判断它的最小生成树是否唯一 如果唯一,输出生成树的大小,否则输出"Not Unique!" solution 直接求非严格次小生成树 如果次小生 ...

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

    题目链接 https://vjudge.net/problem/POJ-1679 Given a connected undirected graph, tell if its minimum spa ...

  7. poj 1679 次小生成树

    次小生成树的求法: 1.Prime法 定义一个二维数组F[i][j]表示点i到点j在最小生成树中的路径上的最大权值.有个知识就是将一条不在最小生成树中的边Edge加入最小生成树时,树中要去掉的边就是E ...

  8. HDU4081(次小生成树)

    1.其中:枚举每条不在最小生成树上的边,并把这条边放到最小生成树上面,然后就一定形成环,那么我们将这条环中取出最长的一条路.最终我们得到的权值便是最小生成树的权值. Max[i][j]表示的最小生成树 ...

  9. 模板 - LCA最近公共祖先(倍增法、Tarjan、树上差分、LCA优化的次小生成树)

    整理的算法模板合集: ACM模板 注意x和y的LCA可以是x或者y本身 一.LCA的在线倍增算法 /*给定一棵包含 n个节点的有根无向树,有 m个询问,每个询问 给出了一对节点的编号 x和 y,询问 ...

最新文章

  1. sql server 清除日志
  2. ML之NBLoR:利用NB(朴素贝叶斯)、LoR(逻辑斯蒂回归)算法(+CountVectorizer)对Rotten Tomatoes影评数据集进行文本情感分析—五分类预测
  3. 4.1.9 OS之文件系统的层次结构
  4. Win11系统无法安装GPT分区的解决方法
  5. c语言uint8的数组怎么转换为uint32_剖析JS和Redis的数据结构设计:数组
  6. win7 修改欢迎登录界面
  7. 从最新的ACL、NAACL和EMNLP中详解知识增强的语言预训练模型
  8. 中国应该建设大型粒子对撞机
  9. 毕设题目:Matlab图像评价
  10. 游戏开发完整学习路线,都在这里了
  11. red5流媒体服务器系统,red5 流媒体服务器配置
  12. android View和ViewGroup创建以及绘制流程
  13. 基于MFAC无模型自适应控制的无人艇航向控制
  14. Python3从搜狐国际新闻抓取---完整版
  15. Android仿今日头条图片滑动退出效果
  16. 10个程序员必上的网站
  17. python证书微软认证_怎样考取微软工程师?
  18. 2、虚拟机的快照与克隆
  19. 基于AMESim的列车制动测试系统ETest
  20. jsp图书管理系统 myeclipse 开发 mysql 数据库 bs结构

热门文章

  1. 汉字转拼音---PHP
  2. 防抖动函数(debounce)的原理
  3. rac下asm管理的表空间-数据文件的重命名
  4. BAT笔试试题常见试题总结含答案(持续更新。。。)
  5. 2015-4-20 BAV推广页面修改前后对比-安全网购
  6. android 连接 asp.net webservice 简单记录
  7. C# hashtable
  8. DotNetNuke与MemberShip的结合(五年版) 三步汇总
  9. js 对一个字段去重_JS单行、多行文本字符去重和行去重
  10. flink 1-个人理解