题目链接: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 ;
}

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

  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 判断最小生成树是否唯一(图论)

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

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

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

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

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

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

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

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

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

  7. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

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

  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 ...

随机推荐

  1. 编写高质量代码:Web前端开发修炼之道(三)

    第五章:高质量的Javascript 这章的内容我看的最久,这是跟我js基础没打好有着莫大的关系,但是还是耐着性子看完了, 不懂的东西都是百度上搜索,理解后再继续.下面是记录下来的笔记. 1)如何避免 ...

  2. C#集合之位数组

    如果需要处理的数字有许多位,就可以使用BitArray类和BitVector32结构.BitArray类位于System.Collection,BitVector32结构位于System.Collec ...

  3. annotation-config和component-scan

    以前学到<context:annotation-config></context:annotation-config>和<context:component-scan b ...

  4. 移动APP的用户体验测试

    经常被问到用户体验测试,什么是用户体验测试,用户体验测试要关注的都有哪些呢,现在为大家来罗列一下: 1.横竖屏测试 在移动设备上做用户体验测试,最容易想到的就是对APP做横竖屏的测试来观察APP的显示 ...

  5. U盘拷贝大文件提示文件过大无法拷贝解决方案

    工具: 计算机 windows操作系统 U盘 原因:由于U盘的格式问题导致的,当期的磁盘格式是FAT32类型的,无拷贝过大的文件 方法:接下来修改U盘类型,且不格式化U盘 1.键盘win+R快捷键弹出 ...

  6. 关于 vertical-align 的一些小知识

    引子 在日常开发过程中,我们经常会遇到如下的场景,一行中既有图片也有文字,而且图片还要和文字对齐.效果如下: 通常代码如下: <!DOCTYPE html> <html> &l ...

  7. python中函数参数传递的几种方法

    转自  http://www.douban.com/note/13413855/ Python中函数参数的传递是通过“赋值”来传递的.但这条规则只回答了函数参数传递的“战略问题”,并没有回答“战术问题 ...

  8. jQuery插件的开发(一)

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  9. WEBPACK & BABEL 打包项目

    本文首发于 BriFuture's Blog. 最近在用 Vue 重写之前的一个项目 Compass,这个项目以前是用 QML + JavaScript 在 Qt 平台上搭建的.这是我本科毕设时做的一 ...

  10. C# 连接Oracle,进行查询,插入操作

    注:OracleConnection和OracleCommand已被标注为[弃用的],可以使用System.Data.OleDb.OleDbConnection代替OracleCOnnection,使 ...