题目链接:http://poj.org/problem?id=1679

题意:给你一组数据,让你判断是否是唯一的最小生成树。

题解:这里用的是kuangbin大佬的次小生成树的模板。直接判断一下次小生成树的最小花费和最小生成树的是否一样即可。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6. const int maxn = ;
  7. const int inf = 0x3f3f3f3f;
  8.  
  9. bool vis[maxn];
  10. int lowc[maxn];
  11. int pre[maxn];
  12. int Max[maxn][maxn];//Max[i][j]表示在最小生成树中从i到j的路径中的最大边权
  13. bool used[maxn][maxn];
  14. int mp[maxn][maxn];
  15. int n,m;
  16.  
  17. int prim(){
  18. int ans = ;
  19. memset(vis,false,sizeof(vis));
  20. memset(Max,,sizeof(Max));
  21. memset(used,false,sizeof(used));
  22.  
  23. vis[] = true;
  24. pre[] = -;
  25.  
  26. for(int i = ; i < n; i++){
  27. lowc[i] = mp[][i];
  28. pre[i] = ;
  29. }
  30.  
  31. lowc[] = ;
  32. for(int i = ; i < n;i++){
  33. int minc = inf;
  34. int p = -;
  35. for(int j = ; j < n;j++)
  36. if(!vis[j] && minc > lowc[j]){
  37. minc = lowc[j];
  38. p = j;
  39. }
  40. if(minc == inf) return -;
  41. ans += minc;
  42. vis[p] = true;
  43. used[p][ pre[p] ] = used[ pre[p] ][p] = true;
  44. for(int j = ; j < n; j++){
  45. if(vis[j])
  46. Max[j][p] = Max[p][j] = max(Max[j][ pre[p] ],lowc[p]);
  47. if(!vis[j] && (lowc[j] > mp[p][j] ) ){
  48. lowc[j] = mp[p][j];
  49. pre[j] = p;
  50. }
  51. }
  52. }
  53. return ans;
  54. }
  55. int ans;
  56. int smst(){
  57. int minn = inf;
  58. for(int i = ; i < n; i++)
  59. for(int j = i+ ; j < n;j++)
  60. if(mp[i][j] != inf && !used[i][j]){
  61. minn = min(minn, ans + mp[i][j] - Max[i][j]);
  62. }
  63. if(minn == inf) return -;//不存在
  64. return minn;
  65. }
  66.  
  67. int main(){
  68. int T;
  69. cin>>T;
  70. while(T--){
  71. cin>>n>>m;
  72. int x,y,w;
  73. memset(mp,inf,sizeof(mp));
  74. for(int i = ; i < n ;i++){
  75. mp[i][i] = ;
  76. }
  77. while(m--){
  78. cin>>x>>y>>w;
  79. x--,y--;
  80. mp[x][y] = mp[y][x] = w;
  81. }
  82. ans = prim();
  83. //cout<<smst()<<endl;
  84. if(ans == -){
  85. cout<<"Not Unique!"<<endl;
  86. }
  87. else if( ans == smst()){
  88. cout<<"Not Unique!"<<endl;
  89. }
  90. else{
  91. cout<<ans<<endl;
  92. }
  93. }
  94.  
  95. return ;
  96. }

【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. Definit ...

  2. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

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

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

  4. poj 1679 The Unique MST

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

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

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

  6. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  7. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  8. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  9. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

随机推荐

  1. linux中errno及perror的应用

    1 perror 定义在头文件<stdlib.h>中 void perror(const char *s);函数说明 perror ( )用 来 将 上 一 个 函 数 发 生 错 误 的 ...

  2. mvnw简介

  3. Mysq sql语句教程

    mysql管理命令  show  databases;  显示服务器上当前所有的数据库  use  数据库名称;  进入指定的数据库  show  tables;  显示当前数据库中所有的数据表  d ...

  4. CF914D

    CF914D 用线段树乱搞一下就行qwq #include<iostream> #include<cstring> #include<cstdio> #includ ...

  5. XStream的简单使用

    XStream XStream是一个java对象和xml相互转换的工具 创建XStream对象:XStream stream = new XStream() Java对象转换成xml:stream . ...

  6. 服务bindService()方法启动服务

    public class MainActivity extends Activity { private EditText studentno; private ServiceConnection c ...

  7. 《构建之法》需求分析 读书笔记 Week6

    本周选读<构建之法>第8章——需求分析.由于有团队项目初期调研阶段做调查问卷的经历,这一章节中很多知识点我都比较有体会.对我而言,这一章节最有价值的内容就是厘清了关于需求分析的两个误解和近 ...

  8. Javascript权威指南——读书笔记

    一.JavaScript核心语法 1.字符串中接受RegExp参数的方法 (1)text.search(pattern)返回首次匹配成功的位置 (2)text.match(pattern)返回匹配组成 ...

  9. termios, tcgetattr, tcsetattr, tcsendbreak, tcdrain, tcflush, tcflow, cfmakeraw, cfgetospeed, cfgetispeed, cfsetispeed, cfsetospeed - 获取和设置终端属性,行控制,获取和设置波特率

    SYNOPSIS 总览 #include <termios.h> #include <unistd.h> int tcgetattr(int fd, struct termio ...

  10. android thread Runnable

    原文链接: http://blog.csdn.net/boyupeng/article/details/6208072 这篇文章中有三点需要提前说明一下, 一: 在android中有两种实现线程thr ...