The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28207   Accepted: 10073

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

 
求一个图是否有不同的最小生成树
大家都太暴力,枚举去那条边,暴力一遍判断重复
但其实可以更快
可以参考这篇论文(注意,文章中代码我认为有错,请自行思考)
http://www.docin.com/p-806495282.html
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<climits>
  6. #include<algorithm>
  7. #include<queue>
  8. #define LL long long
  9. using namespace std;
  10. typedef struct{
  11. int to,frm,dis;
  12. }edge;
  13. edge gra[];
  14. int num=,fa[];
  15. int n,m;
  16. int cmp(const edge &a,const edge &b){
  17. return a.dis<b.dis;
  18. }
  19. int fnd(int x){
  20. return x==fa[x]?x:fnd(fa[x]);
  21. }
  22. int uni(int x,int y){
  23. int fx=fnd(x);
  24. int fy=fnd(y);
  25. fa[fy]=fx;
  26. return ;
  27. }
  28. inline int read(){
  29. int sum=;char ch=getchar();
  30. while(ch>''||ch<'')ch=getchar();
  31. while(ch<=''&&ch>=''){
  32. sum=sum*+ch-'';
  33. ch=getchar();
  34. }
  35. return sum;
  36. }
  37. int kru(){
  38. int ans=;
  39. sort(gra+,gra+m+,cmp);
  40. for(int i=;i<=n;i++)fa[i]=i;
  41. for(int i=;i<=m;i++){
  42. int x=gra[i].frm;
  43. int y=gra[i].to;
  44. int fx=fnd(x);
  45. int fy=fnd(y);
  46. if(fx!=fy){
  47. int j=i+;
  48. while(j<=m&&gra[j].dis==gra[i].dis){
  49. int y1=gra[j].frm;
  50. int x1=gra[j].to;
  51. int fy1=fnd(y1);
  52. int fx1=fnd(x1);
  53. if((fx1==fx&&fy1==fy)||(fx1==fy&&fy1==fx))return -;
  54. j++;
  55. }
  56. ans+=gra[i].dis;
  57. uni(fx,fy);
  58. }
  59. }
  60. return ans;
  61. }
  62. int main(){
  63. int t;
  64. t=read();
  65. while(t--){
  66. memset(gra,,sizeof(gra));
  67. n=read(),m=read();
  68. num=;
  69. for(int i=;i<=m;i++){
  70. gra[i].frm=read();
  71. gra[i].to=read();
  72. gra[i].dis=read();
  73. }
  74.  
  75. int ans=kru();
  76. if(ans==-)printf("Not Unique!\n");
  77. else printf("%d\n",ans);
  78. }
  79. return ;
  80. }

[poj1679]The Unique MST(最小生成树)的更多相关文章

  1. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

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

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

    http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...

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

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

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

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

  5. POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29902   Accepted: 10697 ...

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

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

  7. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

  8. poj1679 The Unique MST(判定次小生成树)

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

  9. POJ-1679.The Unique MST.(Prim求次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39561   Accepted: 14444 ...

随机推荐

  1. rhel 7.0 配置centos yum源(2016/12/8),成功!

    1.首先查看redhat 7.0系统本身所安装的那些yum 软件包: rpm -qa | grep yum #列出所有已安装的yum包 2.删除这些包: rpm -e *.rpm --nodeps # ...

  2. PHP自带防SQL攻击函数区别

    为了防止SQL注入攻击,PHP自带一个功能可以对输入的字符串进行处理,可以在较底层对输入进行安全上的初步处理,也即Magic Quotes.(php.ini magic_quotes_gpc).如果m ...

  3. 全国城市三级联动 html+js

    全国城市三级联动,没有css,所以屏幕的自适应必须自己想办法,手机端慎用(最好不要用,因为有些我也说不出的展示问题). html页面 <!DOCTYPE html> <html> ...

  4. python 在最后一行追加

    2.文本文件的写入 import fileinput file = open("D:\\test.txt", encoding="utf-8",mode=&qu ...

  5. spring cloud 学习研究- spring-cloud-microservice-example

    spring cloud + docker 微服务架构 http://www.open-open.com/lib/view/open1437363835818.html 实例项目 https://gi ...

  6. 赶时髦过了一遍Swift 语言....

    Swift 语言 2014年6月3日发布,替代OBJECT-C Swift is a new programming language for creating iOS and OS X apps. ...

  7. Lucene热词显示并选择

    利用Jquery easyui里的autocomplete(1.10.0版本) 的异步请求(remot.html) 添加引用 <script src="~/Scripts/jquery ...

  8. javascript 使用方法名作为参数

    Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="u ...

  9. 怎么用AJAX来判断dedecms用户是否登录呢

    JS代码:Copy code<script language="javascript" src="{dede:global name='cfg_cmspath'/} ...

  10. animate动画jquery

    <script> $(".change").animate({height:"hide",width:"300px"},&quo ...