题目链接 https://vjudge.net/problem/POJ-1679

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!

【题意】
给定一张无向图,判断这个图的最小生成树是否唯一,如果唯一则输出最小生成树的权值,否则输出 “Not Unique!”

【思路】
题目实际上想问的是次小生成树的权值,如果次小生成树的权值和最小生成树一样,那么最小生成树不唯一。最小生成树可以在求出每对点之间的最小瓶颈路后枚举剩下的边求出。具体做法是先求最小生成树,同时用dfs求出任意两点的最小瓶颈路,然后枚举所有不在最小生成树中的边,假设这条边的端点是u和v,用这条边去替换原来最小生成树中u,v的最小瓶颈路,迭代取最小值。所以次小生成树就是最小瓶颈路的一个简单应用。

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;const int inf = 0x3f3f3f3f;
const int maxn = 105;
const int maxm = 10050;struct Edge {int from, to, dist;Edge(int f = 0, int t = 0, int d = 0) :from(f), to(t), dist(d) {}bool operator<(const Edge& e) const {return dist < e.dist;}
};int n, m;
int par[maxn];
bool used[maxn];//dfs的访问标记数组
bool vis[maxm];//某条边是否使用过
int f[maxn][maxn];
vector<Edge> edges, g[maxn];int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }int kruscal() {int cnt = 0, ans = 0;for (int i = 0; i < n; ++i) {par[i] = i;g[i].clear();}for (int i = 0; i < m; ++i) {int u = edges[i].from;int v = edges[i].to;int x = find(u);int y = find(v);if (x != y) {par[x] = y;ans += edges[i].dist;vis[i] = true;g[u].push_back(Edge(u, v, edges[i].dist));g[v].push_back(Edge(v, u, edges[i].dist));if (++cnt == n - 1) break;}}return ans;
}void dfs(int u) {used[u] = 1;for (int i = 0; i < g[u].size(); ++i) {int v = g[u][i].to;if (!used[v]) {for (int x = 0; x < n; ++x) {if (used[x]) f[x][v] = f[v][x] = max(f[x][u], g[u][i].dist);}dfs(v);}}
}int main() {int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);edges.clear();memset(vis, 0, sizeof(vis));for (int i = 0; i < m; ++i) {int u, v, c;scanf("%d%d%d", &u, &v, &c);--u, --v;edges.push_back(Edge(u, v, c));}sort(edges.begin(), edges.end());int mst = kruscal();memset(used, 0, sizeof(used));memset(f, 0, sizeof(f));dfs(0);int ans = inf;//枚举所有没有用过的边for (int i = 0; i < m; ++i) {if (!vis[i]) {              ans = min(ans, mst - f[edges[i].from][edges[i].to] + edges[i].dist);}}if (ans == mst) printf("Not Unique!\n");else printf("%d\n", mst);}return 0;
}

转载于:https://www.cnblogs.com/wafish/p/10465403.html

POJ 1679 - The Unique MST(次小生成树)相关推荐

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

    求次小生成树的两种方法. 第一种:Kruskal算法比较好操作.先求一遍最小生成树,然后再记录最小生成树上的边.然后再枚举删去最小生成树上的边,再求最小生成树,如果求出的最小生成树的花费等于第一次最小 ...

  2. poj 1679 The Unique MST

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

  3. POJ - 1679 The Unique MST

    题意: 给定一个有 n 个点的无向图,判断最小生成树是否唯一. 链接: https://vjudge.net/problem/POJ-1679 解题思路: 考虑在 Kruskal 算法基础上进行判断. ...

  4. poj 1679: The Unique MST【次小生成树】

    题目链接 参考博客 希望注释足够清楚..欢迎指出不足~ #include<cstdio> #include<cstring> #include<algorithm> ...

  5. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但"异构"的生成树.(并不一定是最小生成树) 分 ...

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

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

  7. poj 1679 次小生成树

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

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

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

  9. `Computer-Algorithm` 最小生成树MST,Prim,Kruskal,次小生成树

    Contents 最小生成树 Algorithm Prim Code Kruskal Prim&KruskalPrim \& KruskalPrim&Kruskal算法的性质 ...

最新文章

  1. 回顾2021,展望2022
  2. typedef VS #define —— C语言中的 关键字 与 C指令
  3. 初学者可能不知道的vue技巧
  4. Spring Integration 4.3.10 发布,Spring 消息通信
  5. 块级元素和行内元素的区别
  6. python全局名称空间_python之名称空间知识点整理
  7. ajax后台怎么取mapp,后台管理实现
  8. jnativecpp.dll一定要放到系统目录下吗_「实用」室内甲醛到底该如何去除?关键要做到这两点...
  9. java AES加密解密
  10. 图像处理VintaSoftImaging.NET SDK控件发布v7.0版本
  11. iOS开发——源代码管理——git(分布式版本控制和集中式版本控制对比,git和SVN对比,git常用指令,搭建GitHub远程仓库,搭建oschina远程仓库 )...
  12. 中孚保密检查客户端 完全卸载_中孚计算机终端保密检查工具
  13. 关于两仪、三才、四象、五行、六合、七星、八卦、九宫、十方、中医的现代科学猜想全解
  14. svn提示没有设置冲突_SVN冲突解决方法大全
  15. 预计招收300人,北京大学信息工程学院2022年夏令营开启报名
  16. 有线以太网RJ45网口转无线WiFi,网卡转wifi,有线转无线,RJ45转wifi方案
  17. vue-element-admin安装依赖失败问题
  18. 【公众号技能+】添加白名单,允许其他公众号转载文章
  19. 网页中遇到的src=quot;data:image/png;base64,xxxxquot;知识点了解
  20. Flutter 解决App登录页面软键盘遮挡住登录按钮或顶起底部控件的问题

热门文章

  1. A Guide to Python's Magic Methods
  2. TabControl控件和TabPage
  3. TCP/IP详解学习笔记(12)-TCP的超时与重传
  4. BZOJ 1251: 序列终结者( splay )
  5. VB高效导入Excel2003和Excel2007文件到MSHFlexGrid控件显示
  6. 片(Slice)结构
  7. SQLServer数据库的备份/恢复的3中策略实例
  8. 利用均值漂移实现图像分割的原理和OpenCV代码
  9. 利用vector定义MAT类对象数组的方法
  10. 源码解读Mybatis List列表In查询实现的注意事项