The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36692   Accepted: 13368

Description

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

  1. 2
  2. 3 3
  3. 1 2 1
  4. 2 3 2
  5. 3 1 3
  6. 4 4
  7. 1 2 2
  8. 2 3 2
  9. 3 4 2
  10. 4 1 2

Sample Output

  1. 3
  2. Not Unique!
  3.  
  4. 题解:
    次小生成树,维护一个两点间的最小距离,最后再向上加
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cstring>
  5. #include <iostream>
  6. using namespace std;
  7. #define line cout<<"------------------"<<endl;
  8. const int MAXN=1e4+10;
  9. const int INF=0x3f3f3f3f;
  10. int n,m;
  11. struct node{
  12. int x,y;
  13. int v;
  14. bool vis;
  15. }Edge[MAXN];
  16. bool cmp(node a,node b)
  17. {
  18. return a.v<b.v;
  19. }
  20. int pre[MAXN];
  21. int Find(int a)
  22. {
  23. if(pre[a]==a)
  24. return a;
  25. return Find(pre[a]);
  26. }
  27. vector<int >G[110];
  28.  
  29. int maxd[110][110];//并查集划到一个树上后,树上任意两点之间的距离
  30.  
  31. void init()
  32. {
  33. for (int i = 1; i <=n; ++i) {
  34. G[i].clear();
  35. pre[i] = i;
  36. G[i].push_back(i);
  37. }
  38.  
  39. }
  40. int main()
  41. {
  42. int _;
  43. scanf("%d",&_);
  44. while(_--)
  45. {
  46. scanf("%d%d",&n,&m);
  47. init();
  48. for(int i=1;i<=m;i++)
  49. {
  50. scanf("%d%d%d",&Edge[i].x,&Edge[i].y,&Edge[i].v);
  51. Edge[i].vis=false;
  52. }
  53. sort(Edge+1,Edge+1+m,cmp);
  54. int sum=0;
  55. for (int i = 1; i <=m ; ++i) {
  56. int x=Find(Edge[i].x);
  57. int y=Find(Edge[i].y);
  58. if(x!=y)
  59. {
  60. pre[x]=y;
  61. sum+=Edge[i].v;
  62. int len1=G[x].size();
  63. int len2=G[y].size();
  64. for (int j = 0; j <len1 ; ++j) {
  65. for (int k = 0; k <len2 ; ++k) {
  66. maxd[G[x][j]][G[y][k]]=maxd[G[y][k]][G[x][j]]=Edge[i].v;//构建两点间最小距离
  67. }
  68. }
  69. int tem[110];
  70. for (int j = 0; j <len2 ; ++j) {
  71. tem[j]=G[y][j];
  72. }
  73. for (int j = 0; j <len1 ; ++j) {
  74. G[y].push_back(G[x][j]);
  75. }
  76. for (int j = 0; j <len2 ; ++j) {
  77. G[x].push_back(tem[j]);
  78. }
  79. Edge[i].vis=true;
  80. }
  81. }
  82. int cis=INF;
  83. for (int i = 1; i <=m ; ++i) {//从不是最小生成树上的边,遍历向上加。找到次小生成树
  84. if(!Edge[i].vis)
  85. cis=min(cis,sum+Edge[i].v-maxd[Edge[i].x][Edge[i].y]);
  86. }
  87. if(cis>sum)
  88. printf("%d\n",sum);
  89. else
  90. printf("Not Unique!\n");
  91. }
  92.  
  93. return 0;
  94. }
  95. //poj1679

  

POJ1679(次小生成树)的更多相关文章

  1. POJ1679(次小生成树)

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

  2. poj1679次小生成树入门题

    次小生成树求法:例如求最小生成树用到了 1.2.4这三条边,总共5条边,那循环3次的时候,每次分别不用1.2.4求得最小生成树的MST,最小的MST即为次小生成树 如下代码maxx即求最小生成树时求得 ...

  3. poj1679 次小生成树

    prim方法:先求过一遍prim,同时标记使用过得边.然后同时记录任意2点间的最大值. 每次加入一条新的边,会产生环,删去环中的最大值即可. #include<stdio.h> #incl ...

  4. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  5. 次小生成树(POJ1679/CDOJ1959)

    POJ1679 首先求出最小生成树,记录权值之和为MinST.然后枚举添加边(u,v),加上后必形成一个环,找到环上非(u,v)边的权值最大的边,把它删除,计算当前生成树的权值之和,取所有枚举加边后生 ...

  6. POJ1679 The Unique MST【次小生成树】

    题意: 判断最小生成树是否唯一. 思路: 首先求出最小生成树,记录现在这个最小生成树上所有的边,然后通过取消其中一条边,找到这两点上其他的边形成一棵新的生成树,求其权值,通过枚举所有可能,通过这些权值 ...

  7. POJ1679 The Unique MST 【次小生成树】

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

  8. 次小生成树(poj1679)

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

  9. POJ1679 The Unique MST —— 次小生成树

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

随机推荐

  1. SAP Fiori里两种锁机制(lock)的实现

    方法1: ETAG机制 SAP CRM Fiori采用了这种机制. 看一个具体的例子来理解.假设我用用户名Jerry选中了这个ID为3456的Opportunity,点击Edit按钮之后: 会触发一个 ...

  2. 高效实时的网络会议数据传输库—UDT

    在视频会议系统的研发当中,我们的音.视频数据必须要有相应的可靠性作为保障,因为视频会议系统是一个实时性非常强的系统,如果其数据在网络不太好的情况下,有可能会出现丢包.数据延迟.数据堵塞等现象,出现这些 ...

  3. hdu-2136 Largest prime factor---巧用素数筛法

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2136 题目大意: 每个素数在素数表中都有一个序号,设1的序号为0,则2的序号为1,3的序号为2,5的 ...

  4. 【洛谷1494】[国家集训队] 小Z的袜子(莫队)

    点此看题面 大致题意: 有\(N\)只从\(1\sim N\)编号的袜子,告诉你每只袜子的颜色,\(M\)组询问,每组询问给你一个区间\([L\sim R]\),让你求出小Z随机抽出\(2\)只袜子时 ...

  5. [USACO09FEB] Revamping Trails 【分层图+Dijkstra】

    任意门:https://www.luogu.org/problemnew/show/P2939 Revamping Trails 题目描述 Farmer John dutifully checks o ...

  6. 有趣的npx

    在更新 npm 5.2.0 的时候发现会买一送一,自动安装了 npx. npx 会帮你执行依赖包里的二进制文件,也就是说 npx 会自动查找当前依赖包中的可执行文件, 如果找不到,就会去 PATH 里 ...

  7. normal 普通身份 sysdba 系统管理员身份 sysoper 系统操作员身份 dba和sysdba

    as sysdba 就是以sysdba登录,oracle登录身份有三种:normal 普通身份sysdba 系统管理员身份sysoper 系统操作员身份每种身份对应不同的权限 sysdba权限:●启动 ...

  8. Unity让带有Rigidbody组件的游戏对象停止运动

    Rigidbody rigidbody = transform.GetComponent<Rigidbody>(); rigidbody.velocity = Vector3.zero; ...

  9. Asset Store 下载的package存在什么地方?

    发现从Asset store下载的packages都不知道放在了什么地方 Windows 7,C:\Users\<username>\AppData\Roaming\Unity\Asset ...

  10. Oracle书写格式

    字符串和日期要包含在单引号中 字符大小写敏感,日期格式敏感 默认日期格式:dd - MON - RR select * from emp where last_name = 'King' where ...