http://poj.org/problem?id=1679

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17726   Accepted: 6150

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!

Source

 
【题解】:
  用kruskal,求出最小生成树,然后枚举删除最小生成树上的一条边,再求最小生成树,比较前后两次求解的大小
  【注意】:如果题目给的数据不能构成一个树需要输出0
 
【code】:
 

  1. /**
  2. Judge Status:Accepted Memory:748K
  3. Time:32MS Language:G++
  4. Code Lenght:1814B Author:cj
  5. */
  6. #include<iostream>
  7. #include<stdio.h>
  8. #include<string.h>
  9. #include<algorithm>
  10. #include<vector>
  11.  
  12. #define N 101
  13. #define M 6000
  14. using namespace std;
  15.  
  16. struct Nod
  17. {
  18. int x,y,w;
  19. }node[M];
  20.  
  21. int parent[N];
  22. int n,m;
  23. vector<int> vct;
  24.  
  25. bool cmp(Nod a,Nod b)
  26. {
  27. return a.w<b.w;
  28. }
  29.  
  30. int findp(int a)
  31. {
  32. while(a!=parent[a])
  33. {
  34. a=parent[a];
  35. }
  36. return a;
  37. }
  38.  
  39. int merge(Nod nd) //合并
  40. {
  41. int x = findp(nd.x);
  42. int y = findp(nd.y);
  43. if(x>y)
  44. {
  45. parent[y]=x;
  46. return nd.w;
  47. }
  48. else if(x<y)
  49. {
  50. parent[x]=y;
  51. return nd.w;
  52. }
  53. return -;
  54. }
  55.  
  56. int kruskal(int id)
  57. {
  58. int i,sum=,cnt=;
  59. for(i=;i<=n;i++) parent[i]=i;
  60. for(i=;i<m;i++)
  61. {
  62. if(i!=id)
  63. {
  64. int temp = merge(node[i]);
  65. if(temp!=-)
  66. {
  67. sum+=temp;
  68. cnt++; //剪枝
  69. if(id==-) vct.push_back(i); //保存第一次最小生成树的各个节点
  70. }
  71. if(cnt>=n-) //找到n-1条边即可以跳出了
  72. break;
  73. }
  74. }
  75. cnt = ;
  76. for(i=;i<=n;i++) //判断是不是构成一棵树
  77. if(parent[i]==i)
  78. cnt++;
  79. if(cnt==) //是
  80. return sum;
  81. if(id==-) //否
  82. return ;
  83. return -;
  84. }
  85.  
  86. int main()
  87. {
  88. int t;
  89. scanf("%d",&t);
  90. while(t--)
  91. {
  92. scanf("%d%d",&n,&m);
  93. int i;
  94. for(i=;i<m;i++)
  95. {
  96. scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].w);
  97. }
  98. vct.clear();
  99. sort(node,node+m,cmp);
  100. int mins = kruskal(-); //找到第一颗最小生成树
  101. int temp=-;
  102. for(i=;i<vct.size();i++)
  103. {
  104. temp = kruskal(vct[i]); //每次去掉一个节点 再判断是否可以组成最小生成树
  105. if(mins==temp)
  106. break;
  107. }
  108. if(temp==mins) puts("Not Unique!");
  109. else printf("%d\n",mins);
  110. }
  111. return ;
  112. }

poj 1679 The Unique MST(唯一的最小生成树)的更多相关文章

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

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

  2. POJ 1679 The Unique MST 【判断最小生成树是否唯一】

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique.  Defini ...

  3. POJ 1679 The Unique MST(推断最小生成树_Kruskal)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique.  Defini ...

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

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

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

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

  6. poj 1679 The Unique MST

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

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

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

  8. POJ 1679 The Unique MST 推断最小生成树是否唯一

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

  9. POJ 1679 The Unique MST (最小生成树)

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

随机推荐

  1. AngularJS学习手册

    看书和视频结合是学习的最高效方式,看了这本书之后对angularjs才算是有一定的理解了.这本书以搭建一个博客为线索讲解了angularjs的知识点和实际项目开发流程.非常适合初学者!下面是我的读书笔 ...

  2. CSS3—三角形

    话不多说看效果:演示效果,runjs 1.加了宽高和border,边用不同颜色显示,每条边都是一个梯形 2.去掉宽高,每条边都是三角形 3.只显示其中一条边就是不同的三角形了,是不是很简单,改变bor ...

  3. AngularJS 整理资料

    AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足,其通过使用指令(directives)结构来扩展HTML词汇,使开发者可以使用HTML来 ...

  4. Linux 命令 - head: 打印文件的开头部分

    命令格式 head [OPTION]... [FILE]... 命令参数 -c, --bytes=[-]K 显示每个文件的前 K 字节内容. -n, --lines=[-]K 显示每个文件的前 K 行 ...

  5. sql中的case when语句

    1.在where子句中: CREATE TABLE `hello`.`sometbl` ( `id` INT NOT NULL AUTO_INCREMENT , `a` VARCHAR(45) NUL ...

  6. 和阿文一起学H5——H5工具、素材

    字体: 一般的H5工具都会提供一部分字体,如果提供的字体中没有自己想用的字体,可以在PPT或PS中进行加工,然后另存为图片,再导入到H5工具中去. 字全生成器: 1.http://www.diyizi ...

  7. JSP之request对象

    在请求转发时,我们需要把一些数据传递到转发后的页面进行处理.这时就需要使用request对象的setAttribute()方法将数据保存到request范围内的变量中. 示例:创建index.jsp文 ...

  8. 北大ACM(POJ1002-487-3279)

    Question:http://poj.org/problem?id=1002问题点:字符映射.选重复项及排序. Memory: 1136K Time: 813MS Language: C++ Res ...

  9. lstm-思想

    RNN(Recurrent Neural Network) 今天我这里讲到的RNN主要是上图这种结构的,即是Hidden Layer会有连向下一时间Hidden Layer的边,还有一种结构是Bidi ...

  10. SQLite的简单应用

    安装部署 1)进入 SQL 下载页面:http://www.sqlite.org/download.html 2)下载预编译二进制文件包. Windows 环境的如下: 下载完之后,就算部署完成.(P ...