题目链接:http://poj.org/problem?id=1679

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29408   Accepted: 10520

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

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique! 题目大意: 给定一个连通无向网,判定它的最小生成树是否唯一。 解题思路: http://www.cnblogs.com/yoke/p/6527300.html AC代码:
 #include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; struct point
{
int u,v,w;
int equal; // 标记,1表示存在其他边权值跟该边一样,0表示不存在
int used; // 在第一次求得的MST中,是否包含该边,1包含,0不包含
int del; // 边是否删除 0不删除 1删除
}p[]; // 存边的数组
int n,m;
int first; // 表示第一次求MST的标记变量
int parent[];
bool cmp(point a, point b)
{
return a.w < b.w;
}
int find (int x)
{
int s,tmp;
for (s = x; parent[s] >= ; s = parent[s]);
while (s != x)
{
tmp = parent[x];
parent[x] = s;
x = tmp;
}
return s;
}
void Union (int A, int B)
{
int a = find(A), b = find(B);
int tmp = parent[a]+parent[b];
if (parent[a] > parent[b])
{
parent[a] = b;
parent[b] = tmp;
}
else
{
parent[b] = a;
parent[a] = tmp;
}
}
int kruskal()
{
int sum = ,num = ;
memset(parent,-,sizeof(parent));
for (int i = ; i < m; i ++)
{
if (p[i].del) continue; // 忽略去掉的边
int u = p[i].u, v = p[i].v;
if (find(u) != find(v))
{
if (first) p[i].used = ;
sum += p[i].w;
Union(u,v);
num ++;
}
if (num == n-) break;
}
return sum;
}
int main ()
{
int t,i,j,u,v,w;
scanf("%d",&t);
while (t --)
{
scanf("%d%d",&n,&m);
for (i = ; i < m; i ++)
{
scanf("%d%d%d",&u,&v,&w);
p[i].u = u; p[i].v = v; p[i].w = w;
p[i].equal = ; p[i].used = ; p[i].del = ;
}
for (i = ; i < m; i ++) // 标记相同权值的边
for (j = i+; j < m; j ++)
if (p[i].w == p[j].w)
p[i].equal = ;
first = ;
sort(p,p+m,cmp);
int sum = kruskal(), sum1; // 第一次求MST
first = ;
for (i = ; i < m; i ++)
{
if (p[i].equal && p[i].used) // 依次去掉原MST中相同权值的边
{
p[i].del = ;
sum1 = kruskal();
if (sum == sum1)
{
printf("Not Unique!\n");
break;
}
p[i].del = ;
}
}
if (i == m)
printf("%d\n",sum);
}
return ;
}

最新文章

  1. Spring中常见的bean创建异常
  2. Java中的内部类(回调)
  3. Hcm data loader for cancel work relationship
  4. Linux下监听或绑定(bind)843端口失败
  5. Kafka组件监控
  6. atitit. web 在线文件管理器最佳实践(1)--- elFinder 的使用流程解决之道 。打开浏览服务器文件夹java .net php
  7. .NET常用类库知识总结
  8. JS获取客户端IP地址、MAC和主机名七种方法
  9. Play-With-Docker在chrome上的插件
  10. 【深度学习系列】关于PaddlePaddle的一些避“坑”技巧
  11. 洛谷P3796 - 【模板】AC自动机(加强版)
  12. [转载非常好的文章]JLink+GDBServer调试S3C6410裸板的初始化代码 For OK6410开发板
  13. 边缘触发(Edge Trigger)和条件触发(Level Trigger)
  14. python第三十三天----静态方法、类方法、属性方法
  15. MongoDB中MapReduce介绍与使用
  16. Shell 比较两个数的大小
  17. vue webpack 打包后css背景图路径问题
  18. 洛谷 P1993 小K的农场 解题报告
  19. Jquery 中的CheckBox、 RadioButton、 DropDownList的取值赋值
  20. 添加mysamba

热门文章

  1. stl容器学习——queue,stack,list与string
  2. php图像处理函数image_type_to_extension、image_type_to_mime_type 的区别
  3. 网络基础 01_OSI网际互联
  4. Shiro源码解析-Session篇
  5. C/C++中创建(带头结点、不带头结点的)单链表
  6. display:inline-block会出现的问题
  7. linux install oracle jdk
  8. javascript的跨域调用
  9. 关于80286——《x86汇编语言:从实模式到保护模式》读书笔记15
  10. Firebird Internal Function