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. php使用Socket实现聊天室功能(书中的代码)

    这只是一种技术 <?php $host = "127.0.0.1"; // 指定监听的端口,注意该端口不能与现有应用的端口冲突 $port = '9505'; $null = ...

  2. 行人重识别(ReID) ——基于Person_reID_baseline_pytorch修改业务流程

    下载Person_reID_baseline_pytorch地址:https://github.com/layumi/Person_reID_baseline_pytorch/tree/master/ ...

  3. python特殊的类属性

    类C的特殊属性: C.__name__ 类C的名字 C.__doc__ 类C文档字符串 C.__bases__ 类C所有父类的元组 C.__dict__ 类C的属性 C.__module__ 类C所在 ...

  4. [php代码审计] apache 后缀名解析“漏洞”

    不能说是漏洞,只是 apache 特性而已. 下面是apache  httpd.conf中截取的一段: <IfModule mime_module> # # TypesConfig poi ...

  5. 牛客ACM赛 C 区区区间间间

    链接 C 区区区间间间 给定长度为\(n\)序列,求\[\sum_{i=1}^{n} \sum_{j=i}^{n} max-min\] 其中\(max\),\(min\)为区间最大,最小值,\(n\l ...

  6. 四种会话跟踪技术以及jstl介绍

    四种会话跟踪技术 page:代表与一个页面相关的对象和属性.一个页面由一个编译好的 Java servlet 类(可以带有任何的 include 指令,但是没有 include 动作)表示.这既包括 ...

  7. git & gerrit & shell

    g公司使用Gerrit改善评审流程. 比较麻烦.gerrit提交后会触发vertifyCI, 实施代码扫描. 这一堆过程, 打印出一堆信息, 都在log中, 所以处理log就需要自己写shell了. ...

  8. RMQ Direct

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11792398.html RMQ Direct Project Directory Maven Depe ...

  9. 对Kubernetes的研究

    服务发现和负载平衡 自动包装 存储编排 自愈 自动部署和回滚 秘密和配置管理 批量执行 水平缩放 是一个对docer进行管理的平台.

  10. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 I. Reversion Count (java大数)

    Description: There is a positive integer X, X's reversion count is Y. For example, X=123, Y=321; X=1 ...