The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39561   Accepted: 14444

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!

Source

 
本题大意:给定一个无向图,让你判断这个图的最小生成树是否唯一。
本题思路:我们可以求出这个图的次小生成树,判断如果Second_MST > MST则说明这个图G只存在一颗最小生成树,相等则说明存在不止一颗最小生成树。
参考代码:
Prim解法:
 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + , maxe = * / + , INF = 0x3f3f3f3f;
int n, m, lowc[maxn], pre[maxn], cost[maxn][maxn], Max[maxn][maxn];
bool vis[maxn], used[maxn][maxn]; int Prim(int source) {
int ans = ;
memset(vis, false, sizeof vis);
memset(Max, , sizeof Max);
memset(used, false, sizeof used);
for(int i = ; i <= n; i ++) {
lowc[i] = cost[source][i];
pre[i] = source;
}
pre[source] = -;
lowc[source] = ;
vis[source] = true;
for(int i = ; i <= n; i ++) {
int MIN = INF, k = -;
for(int j = ; j <= n; j ++)
if(!vis[j] && MIN > lowc[j]) {
MIN = lowc[j];
k = j;
}
if(MIN == INF) return -;
vis[k] = true;
ans += MIN;
used[pre[k]][k] = used[k][pre[k]] = true;//这里记得要把现在访问的边进行标记
for(int j = ; j <= n; j ++) {
if(vis[j] && j != k)
Max[k][j] = Max[j][k] = max(Max[pre[k]][j], lowc[k]);//每次加入一个顶点,就将这个顶点到达其他顶点路径上的最大边权进行更新
//为什么要这样更新呢?我们知道一个顶点在还没有加入最小生成树时它距离MST中各边顶点的最小值可以由它的父亲结点到j结点和它本身到MST结点的最小值的最大值来表示
if(!vis[j] && lowc[j] > cost[k][j]) {
lowc[j] = cost[k][j];
pre[j] = k;
}
}
}
return ans;
} int Second_Prim(int MST) {
int ans = INF;
for(int i = ; i <= n; i ++)
for(int j = + i; j <= n; j ++)
if(!used[i][j] && cost[i][j] != INF)
ans = min(ans, MST - Max[i][j] + cost[i][j]);
return ans;
} int main () {
int t, u, v, w;
scanf("%d", &t);
while(t --) {
scanf("%d %d", &n, &m);
memset(cost, INF, sizeof cost);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &u, &v, &w);
cost[u][v] = cost[v][u] = w;
}
int MST = Prim();
int Second_MST = Second_Prim(MST);
if(Second_MST > MST)
printf("%d\n", MST);
else printf("Not Unique!\n");
}
return ;
}

POJ-1679.The Unique MST.(Prim求次小生成树)的更多相关文章

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

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

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

    题意:求解最小生成树的权值是否唯一,即要我们求次小生成树的权值两种方法求最小生成树,一种用prim算法, 一种用kruskal算法 一:用prim算法 对于给定的图,我们可以证明,次小生成树可以由最小 ...

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

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

  9. poj 1679 The Unique MST 【次小生成树+100的小数据量】

    题目地址:http://poj.org/problem?id=1679 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 Outpu ...

随机推荐

  1. Vue实现点击li变色

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. idea 创建java web项目ssm-gradle

    环境准备:jdk1.8+tomcat8+idea+gradle 1.创建项目SSM 使用gradle创建项目,按照提示如下   image.png 输入项目名称,组名   image.png   im ...

  3. arm算力

    arm 算力运算 MIPS: Million Instructions executed Per SecondDMIPS: Dhrystone Million Instructions execute ...

  4. postgresql相关sql集锦

    1.类似于oracle的listagg->string_agg SELECT area_county,)total FROM project_info GROUP BY area_county ...

  5. windows下laravel 快速安装

    1. 安装composer  https://getcomposer.org/ 2. 安装git windows 客户端工具 https://git-scm.com/downloads 3. 更改co ...

  6. 24.mongodb可视化工具部署——2019年12月19日

    2019年10月09日17:05:54 教程链接:https://blog.csdn.net/qq_32340877/article/details/79142129 项目名:adminMongo g ...

  7. Linux命令行工具之pidstat命令

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484624.html pidstat命令就可以帮助我们监测到具体线程的上下文切换 通过pidstat ...

  8. javaSE之运行时异常和编译时异常

    运行时异常继承自RuntimeException; package foundationEnhance; public class Person { private int age; public P ...

  9. python中*args和**kwargs学习

    *args 和 **kwargs 经常看到,但是一脸懵逼 ,今天终于有收获了 """ python 函数的入参经常能看到这样一种情况 *args 或者是 **kwargs ...

  10. string 、char* 、 char []的转换

    1.string->char* (1)data string s = "goodbye"; const char* p=str.data(); (2)c_str() stri ...