题意:构成MST是否唯一

思路:

问最小生成树是否唯一。我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足:他不是最小生成树中的边,并且它的权值等于Max[i][j],那么他就可以代替MST中的这条边,所以MST不唯一。

次小生成树总结

代码:

  1. #include<cmath>
  2. #include<stack>
  3. #include<queue>
  4. #include<string>
  5. #include<vector>
  6. #include<cstdio>
  7. #include<cstdlib>
  8. #include<cstring>
  9. #include<iostream>
  10. #include<algorithm>
  11. const int N = 100+5;
  12. const int INF = 0x3f3f3f3f;
  13. using namespace std;
  14. int n,m;
  15. int mp[N][N],dis[N],pre[N];
  16. bool vis[N];
  17. int Max[N][N]; //最大权值边
  18. bool is[N][N]; //是否在MST中
  19. void init(){
  20. int x,y,w;
  21. memset(mp,INF,sizeof(mp));
  22. for(int i = 0;i < m;i++){
  23. scanf("%d%d%d",&x,&y,&w);
  24. mp[x][y] = mp[y][x] = w;
  25. }
  26. memset(vis,false,sizeof(vis));
  27. vis[1] = true;
  28. for(int i = 2;i <= n;i++){
  29. dis[i] = mp[i][1];
  30. pre[i] = 1;
  31. }
  32. }
  33. void Prim(){
  34. int mincost = 0;
  35. memset(Max,0,sizeof(Max));
  36. memset(is,false,sizeof(is));
  37. for(int i = 1;i <= n - 1;i++){
  38. int MIN = INF;
  39. int point;
  40. for(int j = 1;j <= n;j++){
  41. if(!vis[j] && dis[j] < MIN){
  42. MIN = dis[j];
  43. point = j;
  44. }
  45. }
  46. vis[point] = true;
  47. mincost += MIN;
  48. is[point][pre[point]] = is[pre[point]][point] = true; //在MST中
  49. for(int j = 1;j <= n;j++){
  50. if(vis[j] && j != point){ //在MST中
  51. Max[j][point] = Max[point][j] = max(Max[pre[point]][j],dis[point]);
  52. //更新j到point的最大权值边
  53. }
  54. if(!vis[j] && dis[j] > mp[j][point]){
  55. pre[j] = point;
  56. dis[j] = mp[j][point];
  57. }
  58. }
  59. }
  60. //判断MST是否唯一
  61. for(int i = 1;i <= n;i++){
  62. for(int j = 1;j < i;j++){
  63. if(mp[i][j] != INF && !is[i][j]){
  64. if(mp[i][j] == Max[i][j]){
  65. printf("Not Unique!\n");
  66. return;
  67. }
  68. }
  69. }
  70. }
  71. printf("%d\n",mincost);
  72. }
  73. int main() {
  74. int T;
  75. scanf("%d",&T);
  76. while(T--){
  77. scanf("%d%d",&n,&m);
  78. init();
  79. Prim();
  80. }
  81. return 0;
  82. }

POJ 1679 The Unique MST (次小生成树)题解的更多相关文章

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

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

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

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

  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. POJ1679 The Unique MST —— 次小生成树

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

  8. poj 1679 The Unique MST

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

  9. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  10. poj 1679 The Unique MST (判定最小生成树是否唯一)

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

随机推荐

  1. Shuffle'm Up---poj3087

    题目链接 题意:有两个字符串s1,s2:经过交叉问是否得到字符串s,不能输出-1,能就输出交叉的次数 每次重组的串都是s2开始,重新组合时,前面一半是s1,后一半s2: #include<std ...

  2. requests库的get请求(加上head,加上get参数请求)

    #coding:utf-8 # 导入requests import requests # 构建url url = 'http://www.baidu.com' # 发送请求,获取响应 # respon ...

  3. mysql 权限管理 对所有库 所有表 授权 *.*

    对miek这个账号localhost 授予了所有库,所表的select权限 mysql> grant select on *.* to 'mike'@'localhost'; Query OK, ...

  4. Linuxer-&quot;Linux开发人员自己的媒体&quot;第五月稿件和赠书名单

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/juS3Ve/article/details/78859630 Linuxer已经从一个单纯的读者服务 ...

  5. [django实践]投票app

    code: https://github.com/lannyMa/toupiao polls app介绍 这个例子来源于django官网,恰好2.x版本有中文版. https://docs.djang ...

  6. [LeetCode] 796. Rotate String_Easy **KMP

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  7. [LeetCode] 197. Rising Temperature_Easy tag: SQL

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  8. win10 + cuda(v9.0) 安装TensorFlow-gpu版

    之前在实习公司的电脑上装过TensorFlow-gpu,那时候很快就装好了.但在自己的笔记本上装时,却搞了很久... 一部分原因是因为用校园网下载cuda toolkit 和cudnn ,总是在最后时 ...

  9. 如何用softmax和sigmoid来做多分类和多标签分类

    首先,说下多类分类和多标签分类的区别 多标签分类:一个样本可以属于多个类别(或标签),不同类之间是有关联的,比如一个文本被被划分成“人物”和“体育人物”两个标签.很显然这两个标签不是互斥的,而是有关联 ...

  10. Docker深入浅出3-容器管理

    docker客户端非常简单,我们可以直接输入docker命令来查看Docker客户端所有的命令项 [root@admin-fxr ~]# docker Usage: docker COMMAND A ...