题目链接: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. spring boot MVC

    1 spring boot的用途 第一,spring boot可以用来开发mvc web应用. 第二,spring boot可以用来开发rest api. 第三,spring boot也可以用来开发w ...

  2. Scrapy框架: 登录网站

    一.使用cookies登录网站 import scrapy class LoginSpider(scrapy.Spider): name = 'login' allowed_domains = ['x ...

  3. Struts2的Action访问

    ● 示例项目结构 ●  demo1.jsp <%@ page language="java" import="java.util.*" pageEncod ...

  4. 2019-9-2-win10-uwp-Markdown

    title author date CreateTime categories win10 uwp Markdown lindexi 2019-09-02 12:57:38 +0800 2018-2- ...

  5. Java中this的基础用法

    update on 2019-07-07 在Java核心技术一书中看到调用方法时this作为隐式参数传入的. 突然间许多问题都懂了 比如:方法的多态 父类变量指向子类对象的引用 对象变量指向的实际类型 ...

  6. windows server 2016 支持多用户远程登录

    服务器设置多用户同时远程桌面,可以提高访问效率,避免人多抢登服务器. 1. 首先需要先安装远程桌面服务  配置组策略,运行框输入gpedit.msc,打开计算机配置–>管理模板—>wind ...

  7. HTML事件处理程序---内联onclick事件

    HTML事件处理程序绑定方法: <input type="button" value="click me" onclick="show(this ...

  8. Java的HashMap和Hashtable有什么区别HashSet和HashMap有什么区别?使用这些结构保存的数需要重载的方法是哪些?

    HashMap与Hashtable实现原理相同,功能相同,底层都是哈希表结构,查询速度快,在很多情况下可以互用 两者的主要区别如下 1.Hashtable是早期JDK提供的接口,HashMap是新版J ...

  9. FromBase64String(String)和Encoding.Default.GetBytes(String)

    今天突然被问FromBase64String(String)和Encoding.Default.GetBytes(String)有啥区别,我刚开始学C#对这个一脸懵逼,于是总结一下今天查资料的内容. ...

  10. 【转】java即时消息推送

    整个例子的源码下载:http://pan.baidu.com/s/1gfFYSbp 下载服务端jar文件 Comet4J目前仅支持Tomcat6.7版本,根据您所使用的Tomcat版本下载[comet ...