【题目链接】

点击打开链接

【算法】

先求出图的最小生成树

枚举不在最小生成树上的边,若加入这条边,则形成了一个环,如果在环上且在最小生成树上的权值最大的边等于

这条边的权值,那么,显然最小生成树不唯一

树上倍增可以解决这个问题

【代码】

  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cctype>
  4. #include <cerrno>
  5. #include <clocale>
  6. #include <cmath>
  7. #include <complex>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <ctime>
  12. #include <deque>
  13. #include <exception>
  14. #include <fstream>
  15. #include <functional>
  16. #include <limits>
  17. #include <list>
  18. #include <map>
  19. #include <iomanip>
  20. #include <ios>
  21. #include <iosfwd>
  22. #include <iostream>
  23. #include <istream>
  24. #include <ostream>
  25. #include <queue>
  26. #include <set>
  27. #include <sstream>
  28. #include <stdexcept>
  29. #include <streambuf>
  30. #include <string>
  31. #include <utility>
  32. #include <vector>
  33. #include <cwchar>
  34. #include <cwctype>
  35. #include <stack>
  36. #include <limits.h>
  37. using namespace std;
  38. #define MAXN 110
  39. #define MAXM 1000010
  40. #define MAXLOG 20
  41.  
  42. struct Edge
  43. {
  44. int x,y;
  45. long long w;
  46. } edge[MAXM];
  47.  
  48. int T,n,m,i;
  49. long long val;
  50. vector< pair<int,long long> > e[MAXN];
  51. bool on_mst[MAXM];
  52. int fa[MAXN],anc[MAXN][MAXLOG],dep[MAXN];
  53. long long mx[MAXN][MAXLOG];
  54. bool not_unique;
  55.  
  56. inline bool cmp(Edge a,Edge b) { return a.w < b.w; }
  57. inline int get_root(int x)
  58. {
  59. if (fa[x] == x) return x;
  60. return fa[x] = get_root(fa[x]);
  61. }
  62. inline void kruskal()
  63. {
  64. int i,x,y,sx,sy;
  65. long long w;
  66. for (i = ; i <= n; i++) fa[i] = i;
  67. for (i = ; i <= m; i++) on_mst[i] = false;
  68. sort(edge+,edge+m+,cmp);
  69. for (i = ; i <= m; i++)
  70. {
  71. x = edge[i].x;
  72. y = edge[i].y;
  73. w = edge[i].w;
  74. sx = get_root(x);
  75. sy = get_root(y);
  76. if (sx != sy)
  77. {
  78. on_mst[i] = true;
  79. val += w;
  80. fa[sx] = sy;
  81. e[x].push_back(make_pair(y,w));
  82. e[y].push_back(make_pair(x,w));
  83. }
  84. }
  85. }
  86. inline void build(int u)
  87. {
  88. int i,v;
  89. for (i = ; i < MAXLOG; i++)
  90. {
  91. anc[u][i] = anc[anc[u][i-]][i-];
  92. mx[u][i] = max(mx[u][i-],mx[anc[u][i-]][i-]);
  93. }
  94. for (i = ; i < e[u].size(); i++)
  95. {
  96. v = e[u][i].first;
  97. if (anc[u][] != v)
  98. {
  99. dep[v] = dep[u] + ;
  100. anc[v][] = u;
  101. mx[v][] = e[u][i].second;
  102. build(v);
  103. }
  104. }
  105. }
  106. inline long long get(int x,int y)
  107. {
  108. int i,t;
  109. long long ans = ;
  110. if (dep[x] > dep[y]) swap(x,y);
  111. t = dep[y] - dep[x];
  112. for (i = ; i < MAXLOG; i++)
  113. {
  114. if (t & ( << i))
  115. {
  116. ans = max(ans,mx[y][i]);
  117. y = anc[y][i];
  118. }
  119. }
  120. if (x == y) return ans;
  121. for (i = MAXLOG - ; i >= ; i--)
  122. {
  123. if (anc[x][i] != anc[y][i])
  124. {
  125. ans = max(ans,max(mx[x][i],mx[y][i]));
  126. x = anc[x][i];
  127. y = anc[y][i];
  128. }
  129. }
  130. return max(ans,max(mx[x][],mx[y][]));
  131. }
  132. int main()
  133. {
  134.  
  135. scanf("%d",&T);
  136. while (T--)
  137. {
  138. scanf("%d%d",&n,&m);
  139. val = ;
  140. not_unique = false;
  141. for (i = ; i <= n; i++)
  142. {
  143. dep[i] = ;
  144. e[i].clear();
  145. memset(anc[i],,sizeof(anc[i]));
  146. memset(mx[i],,sizeof(mx[i]));
  147. }
  148. for (i = ; i <= m; i++) scanf("%d%d%lld",&edge[i].x,&edge[i].y,&edge[i].w);
  149. kruskal();
  150. build();
  151. for (i = ; i <= m; i++)
  152. {
  153. if (!on_mst[i])
  154. not_unique |= (get(edge[i].x,edge[i].y) == edge[i].w);
  155. }
  156. if (not_unique) printf("Not Unique!\n");
  157. else printf("%lld\n",val);
  158. }
  159.  
  160. return ;
  161.  
  162. }

【POJ 1679】 The Unique MST的更多相关文章

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

    找出最小生成树,同时用Max[i][j]记录i到j的唯一路径上最大边权.然后用不在最小生成树里的边i-j来替换,看看是否差值为0. #include <algorithm> #includ ...

  2. POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

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

  3. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  4. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  5. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  6. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  7. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  8. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  9. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

随机推荐

  1. 分布式集群算法 memcached 如何实现分布式?

    memcached 是一个”分布式缓存”,然后 memcached 并不像 mongoDB 那 样,允许配置多个节点,且节点之间”自动分配数据”. 就是说--memcached 节点之间,是不互相通信 ...

  2. 洛谷 3106 [USACO14OPEN]GPS的决斗Dueling GPS's 3720 [AHOI2017初中组]guide

    [题解] 这两道题是完全一样的. 思路其实很简单,对于两种边权分别建反向图跑dijkstra. 如果某条边在某一种边权的图中不是最短路上的边,就把它的cnt加上1.(这样每条边的cnt是0或1或2,代 ...

  3. Windows 下安装 Node.js

    搭建博客系列的 Node.js 环境安装.Windows 下面安装可以通过图形化界面进行安装,非常方面. 1.打开 Node.js 官网,下载对应版本的安装包(msi 后缀的) 2.双击运行下载的程序 ...

  4. Leetcode 208.实现前缀树

    实现前缀树 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert ...

  5. 添物不花钱学JavaEE(基础篇)-XML

    XML(Extensible Markup Language) XML在日常工作中经常用到,必须有个了解,不过认识一下即可,不要太浪费时间.实际用到 参考图书 <XML入门经典>大而全,不 ...

  6. 7-19 求链式线性表的倒数第K项(20 分)(单链表定义与尾插法)

    给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字. 输入格式: 输入首先给出一个正整数K,随后是若干正整数,最后以一个负整数表示结尾(该负数不算在序列内,不要处理). 输出格式 ...

  7. android listVIew实现button按钮监听程序

    1.重写simpleAdapter 方法@Override public HashMap<String,String> getItem(int position) { // TODO Au ...

  8. ****Call to a member function item() on a non-object

    A PHP Error was encountered Severity: Error Message: Call to a member function item() on a non-objec ...

  9. codeforces 762E(cdq分治)

    题意: n个电台,每个电台有三个属性xi, ri, fi.分别代表电台的坐标,电台的播报范围,以及播报的频率. 对于一对电台i, j,若min(ri, rj) >= |xi - xj|,那么他们 ...

  10. Mycat集群方案收集(待实践)

    先收集,后续再实践. 我想,市面上开源方案中,涉及到高可用和负载均衡的部署,无论是哪一个产品应用,都基本离不开LVS+Keepalived+HAProxy+Nginx等等. 下面是收集的教程: htt ...