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! 思路:找次小生成树,如果权值相等则不唯一,用kruskal实现次小生成树
const int maxm = ;
const int maxn = ; struct edge {
int u, v, w;
edge(int _u=-, int _v=-, int _w=):u(_u), v(_v), w(_w){}
bool operator<(const edge &a) const {
return w < a.w;
}
};
vector<edge> Edge; int fa[maxm], T, N, M, tree[maxn], k; void init() {
Edge.clear();
for(int i = ; i <= N; ++i)
fa[i] = i;
k = ;
} int Find(int x) {
if(fa[x] == x)
return x;
return fa[x] = Find(fa[x]);
} void Union(int x, int y) {
x = Find(x), y = Find(y);
if(x != y) fa[x] = y;
} int main() {
scanf("%d", &T);
while(T--) {
int t1, t2, t3, u, v;
scanf("%d%d", &N, &M);
init();
int sum = ;
for(int i = ; i < M; ++i) {
scanf("%d%d%d", &t1, &t2, &t3);
Edge.push_back(edge(t1, t2, t3));
}
sort(Edge.begin(), Edge.end());
bool flag = true;
for(int i = ; i < M; ++i) {
u = Edge[i].u, v = Edge[i].v;
u = Find(u), v = Find(v);
if(u != v) {
sum += Edge[i].w;
Union(u,v);
tree[k++] = i;
}
}
for(int i = ; i < k; ++i) {
int cnt = , edgenum = ;
for(int t = ; t <= N; ++t)
fa[t] = t;
for(int j = ; j < M; ++j) {
if(j == tree[i]) continue;
u = Edge[j].u, v = Edge[j].v;
u = Find(u), v = Find(v);
if(u != v) {
cnt += Edge[j].w;
edgenum++;
Union(u,v);
}
}
if(cnt == sum && edgenum == N - ) {
flag = false;
break;
}
}
if(flag)
printf("%d\n", sum);
else printf("Not Unique!\n");
}
return ;
}

次小生成树博客:https://www.cnblogs.com/bianjunting/p/10829212.html

https://blog.csdn.net/niushuai666/article/details/6925258

注:这里的Max数组是记录从i到j节点中边权最大值(不是和),从其父节点与新连接的边中比较

												

Day5 - G - The Unique MST POJ - 1679的更多相关文章

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

    链接: http://poj.org/problem?id=1679 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  2. The Unique MST POJ - 1679 (次小生成树)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  3. K - The Unique MST - poj 1679

    题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...

  4. The Unique MST POJ - 1679 次小生成树prim

    求次小生成树思路: 先把最小生成树求出来  用一个Max[i][j] 数组把  i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过   把没有使用过的一条边加 ...

  5. The Unique MST POJ - 1679 最小生成树判重

    题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...

  6. poj 1679 The Unique MST

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

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

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

  8. POJ 1679 The Unique MST(判断最小生成树是否唯一)

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

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

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

随机推荐

  1. Vulnhub_DC8 记录

    目录 DC8 经验 & 总结 步骤流水 信息搜集 80端口 获取Shell 提权 DC8 经验 & 总结 对页面的功能和对应的url要敏感. 所有的功能都要测试,要雨露均沾. 提示说的 ...

  2. (踩过的坑)使用Github Page搭建个人博客

    最近需要搭建一个网站,作为导航网址,但是自己的域名备案还要等上几天,就想着有没有别的办法来搭建一个公网可以访问的网站. Github Page的话是一个github个人主页,完全适合用来搭建普通网站. ...

  3. Java Juc学习笔记

    Java JUC 简介 在 Java 5.0 提供了 java.util.concurrent (简称JUC )包,在此包中增加了在并发编程中很常用的实用工具类,用于定义类似于线程的自定义子系统,包括 ...

  4. Java基础 -5

    方法的定义与使用 方法(method)的基本定义 本次方法定义在主类之中并且由主方法直接调用,所以方法的定义语法形式如下: public static 返回值类型 方法名称([参数类型 变量, ... ...

  5. AWS-DDNS

    1. DDNS 2. 在 Linux 实例上设置动态 DNS 2.1 Ubuntu 2.2 Amazon Linux 2 2.3 Arch Linux 2.4 其他Linux系统 3. 更多相关 1. ...

  6. Java垃圾回收与内存

    好久没看关于java的书了, 最近, 看了James Gosling的<<Java程序设计语言>>, 做了一些读书笔记. 这部分是关于垃圾回收的. 1.垃圾回收 对象是使用ne ...

  7. Servlet对用户输入的数据进行读取

    逻辑代码: package com.zyb.test; import java.io.IOException; import java.util.Enumeration; import javax.s ...

  8. :before 与 :after

    http://justcoding.iteye.com/blog/2032627  网址

  9. dequeueReusableCellWithIdentifier 与 dequeueReusableCellWithIdentifier:forIndexPath 区别

    参考:http://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequ ...

  10. NSIndexPath等结构体的比较

    1.NSIndexPath的比较方式,需要将结构体内部的属性一一对比.比如, if ((indexPath.section == self.selectIndexPath.section) & ...