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. python中模块的制作

    1.import 模块名 2.from 模块名 import 类名(或方法名或全局变量) 3.from 模块名 import *   导入模块名下的所有类名,方法,全局变量 4.from 模块名 im ...

  2. selenium webdriver 常用方法

    /** * 判断元素是否存在 * * @param driver * @param by * @return */ public static boolean isElementPresent(Web ...

  3. springboot 模板

    参考:https://blog.csdn.net/wangb_java/article/details/71775637

  4. python学习笔记(3) -- 字符与数字之间的转换函数

    转载:python中的字符数字之间的转换函数 int(x [,base ])         将x转换为一个整数     long(x [,base ])        将x转换为一个长整数     ...

  5. httpclient post 请求

    package com.thinkgem.jeesite.common.utils; import org.apache.http.HttpEntity; import org.apache.http ...

  6. redhat 7.6 密码破解(无光盘)

    开机,在下面界面按e 找到linux16  在最尾输入 rd.break 按 Ctrl+x 输入 mount -o remount,rw /sysroot 输入chroot   /sysroot sh ...

  7. MySQL必知必会(1-8)章

    1.数据库,表,列,行,模式,每一列有唯一的数据类型,模式是数据库和表的布局及特性 2.满足主键的两个条件:任意两行都不具有相同的主键值,每行都必须具有主键值 3.SQL(Structured Que ...

  8. 任意两点之间的最短路(floyed)

    F.Moving On Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each ...

  9. PaperReading20200221

    CanChen ggchen@mail.ustc.edu.cn Busy... Human-level concept learning through probabilistic program i ...

  10. Linux系统chmod命令的含义和权限详解

    许多喜欢使用chmod命令的用户,对chmod命令的含义和权限仍然不是很清楚,因此在使用的时候对它们造成了一定的麻烦.为了解决这些用户的迷惑,今天小编就和大家一起分享下chmod命令的含义和权限. 对 ...