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!'.
 
题目大意:判断最小生成树是否唯一(或者说判断次小生成树与最小生成树是否具有同样的权值)
思路:用Kruskal加边的时候,每次判断是否有其他边和当前边具有同样的功能(同样的边权,连接的集合相同),有则输出Not Unique!
 
 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXE = ;
const int MAXN = ; struct Edge {
int from, to, val;
bool operator < (const Edge &rhs) const {
return val < rhs.val;
}
} edge[MAXE]; int fa[MAXN], deg[MAXN];
int n, ecnt; void init() {
ecnt = ;
for(int i = ; i <= n; ++i) {
fa[i] = i;
deg[i] = ;
}
} void add_edge(int u, int v, int c) {
edge[ecnt].from = u;
edge[ecnt].to = v;
edge[ecnt++].val = c;
} int getfather(int x) {
return fa[x] == x ? x : getfather(fa[x]);
} void union_set(int x, int y) {
int a = getfather(x);
int b = getfather(y);
if(a == b) return ;
if(deg[a] <= deg[b]) swap(a, b);
++deg[a]; fa[b] = a;
} int kruskal() {
int sum = ;
int xa, ya;
sort(edge, edge + ecnt);
for(int i = ; i < ecnt; ++i) {
xa = getfather(edge[i].from);
ya = getfather(edge[i].to);
if(xa == ya) continue;
for(int j = i + ; j < ecnt; ++j) {
if(edge[j].val != edge[i].val) break;
if(xa == getfather(edge[j].from) && ya == getfather(edge[j].to)) {
return -;
break;
}
}
union_set(edge[i].from, edge[i].to);
sum += edge[i].val;
}
return sum;
} int main() {
int T, m, a, b, c;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
init();
for(int i = ; i < m; ++i) {
scanf("%d%d%d", &a, &b, &c);
if(a > b) add_edge(b, a, c);
else add_edge(a, b, c);
}
int ans = kruskal();
if(ans < ) printf("Not Unique!\n");
else printf("%d\n", ans);
}
}

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

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

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

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

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

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

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

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

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

  5. poj 1679 The Unique MST

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

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

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

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

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

  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: 24034   Accepted: 8535 D ...

  10. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

随机推荐

  1. 在IDEA中关于项目java版本问题

    在IDEA中关于项目java版本问题 当出现错误如:java无效的源发行版11或IDEA Error:java:Compliation failed:internal java complier er ...

  2. MySQL索引的使用及注意事项

    索引是存储引擎用于快速找到记录的一种数据结构.索引优化应该是对查询性能优化最有效的手段了.索引能够轻易将查询性能提高几个数量级,"最优"的索引有时比一个"好的" ...

  3. vue的$emit 与$on父子组件与兄弟组件的之间通信

    本文主要对vue 用$emit 与 $on 来进行组件之间的数据传输. 主要的传输方式有三种: 1.父组件到子组件通信 2.子组件到父组件的通信 3.兄弟组件之间的通信 一.父组件传值给子组件 父组件 ...

  4. 03JavaScript 输出

    JavaScript 输出 JavaScript 没有任何打印或者输出的函数. 先来一点DOM的小知识点: DOM 解释: 您会经常看到 document.getElementById("i ...

  5. margin中的bug解决方法

    margin bug问题 : 当做子元素中使用margin-top: 50px;父子元素都会跑出50px, 解决方法: 在父元素中使用下面三种任意一种都可以. 方法一:给父元素加边框 border: ...

  6. C语言学习记录

    思路: 工具书: <c程序设计语言> R&K <linux C 编程一站式学习>

  7. xmind打开文件报错

    可以尝试使用导入 文件——导入 选择此方式打开即可.

  8. 北京Uber优步司机奖励政策(2月27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. sql server 对Geography 的增(insert)和查询(select)

    insert:    Location为    Geography类型                INSERT INTO [oss1].[dbo].[Order] ([Location]) VAL ...

  10. spl_autoload_register()函数

    一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.class.php <?php class PRINTI ...