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!

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. int father[111],n,m,first;
  7.  
  8. struct node
  9. {
  10. int u,v,w;
  11. int used;
  12. int equal;
  13. int del;
  14. } a[11111];
  15.  
  16. bool cmp(node x,node y)
  17. {
  18. if(x.w<y.w) return true;
  19. return false;
  20. }
  21.  
  22. int find(int x)
  23. {
  24. int r=x;
  25. while(father[r]!=r) r=father[r];
  26. int i=x,j;
  27. while(i!=r) {
  28. j=father[i];
  29. father[i]=r;
  30. i=j;
  31. }
  32. return r;
  33. }
  34.  
  35. int prime()
  36. {
  37. int i,j,k,sum,num;
  38. sum=0;num=0;
  39. for(i=1;i<=n;i++) father[i]=i;
  40. for(i=1;i<=m;i++) {
  41. if(a[i].del) continue;
  42. int fx=find(a[i].u);
  43. int fy=find(a[i].v);
  44. if(fx!=fy) {
  45. num++;
  46. father[fx]=fy;
  47. sum+=a[i].w;
  48. if(first) a[i].used=1;
  49. }
  50. if(num==n-1) break;
  51. }
  52. return sum;
  53. }
  54.  
  55. int main()
  56. {
  57. int i,j,k,u,v,w,sum1,sum2;
  58. int t;
  59. scanf("%d",&t);
  60. while(t--) {
  61. sum1=sum2=0;
  62. memset(a,0,sizeof(a));
  63. scanf("%d%d",&n,&m);
  64. for(i=1;i<=m;i++) {
  65. scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
  66. }
  67. for(i=1;i<=m;i++) {
  68. for(j=i+1;j<=m;j++) {
  69. if(a[i].w==a[j].w) a[i].equal=1;
  70. }
  71. }
  72. sort(a+1,a+1+m,cmp);
  73. first=1;
  74. sum1=prime();
  75. first=0;
  76. for(i=1;i<=m;i++) {
  77. if(a[i].used && a[i].equal) {
  78. a[i].del=1;
  79. sum2=prime();
  80. if(sum1==sum2) {
  81. printf("Not Unique!\n");
  82. break;
  83. }
  84. }
  85. }
  86. if(i==m+1) printf("%d\n",sum1);
  87. }
  88. }

POJ 1679 The Unique MST(推断最小生成树_Kruskal)的更多相关文章

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

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

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

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

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

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  4. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

  5. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...

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

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

  7. poj 1679 The Unique MST 判断最小生成树是否唯一(图论)

    借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...

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

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

  9. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  10. poj 1679 The Unique MST

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

随机推荐

  1. OpenVX

    OpenVX openvx  1. 编译 尝试编译openvx_sample,下载相关代码. 下载的sample code直接使用make可以生成libopenvx.so. 使用python Buil ...

  2. php数据库增删改查

    首先建立一个数据库db_0808,将db_0808中表格student导入网页. CURD.php <!DOCTYPE html> <html lang="en" ...

  3. Deutsch lernen (03)

    1. das Gewerbe, - 行业 Was ist Ihr Gewerbe? Welches Gewerbe treibt er? treiben - trieb - getrieben  从事 ...

  4. spring中的prop、set、list、map

    props.set.list.map这些事spring配置文件中很常见的标签,下面说下各自的适用场合. props:用于键值对,建和值都为string类型. <property name=&qu ...

  5. HDU_1729_sg函数(dfs)

    Stone Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  6. Java继承实现接口的抽象类

    1.TestIntace.java package com.chase.abstrac; /** * 接口 * @author Chase * * @date 2013-10-21 下午02:29:1 ...

  7. [POI2012]OKR-A Horrible Poem

    正解:对于一个区间l,r,它的循环节长度一定是它的因数. 然后如果循环节是这个长度,那么[l+len,r]一定等于[l,r-len]. 然后每次询问的时候就把它的长度的最小质因子提出来. BZOJ上都 ...

  8. 最小化安装CentOS-7-x86_64-Minimal-1511图文教程

    说明: 虚拟机产品:VMware® Workstation 12 Pro,版本:12.5.0 build-4352439 系统镜像:CentOS-7-x86_64-Minimal-1511.iso 操 ...

  9. 邓_ Phpcms·二次开发

    PHPCMS V9产品介绍 PHPCMS V9(简称V9)采用PHP5+MYSQL做为技术基础进行开发.V9采用OOP(面向对象)方式进行基础运行框架搭建.模块化开发方式做为功能开发形式.框架易于功能 ...

  10. (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】

    文件上传主要分以下几个步骤: (1)新建maven java project: (2)在pom.xml加入相应依赖: (3)新建一个表单页面(这里使用thymleaf); (4)编写controlle ...