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. 从后台看python--为什么说python是慢的

    python越来越作为一种科学技术研究的语言越来越流行,可是我们经常听到一个问题,python是慢的.那么我们从后台分析一下,为什么python是慢的. python是一种动态类型,解释型语言,它的值 ...

  2. ES6——generator

    generator 生成器函数 普通函数,一路到底 generator函数,中间可以停,到哪停呢,用 yield 配合,交出执行权 yield 有 放弃.退让.退位的意思 需要调用next()方法启动 ...

  3. 前端开发HTML&css入门——CSS的文本格式化

    长度单位 像素 px 百分比 % em - 像素是我们在网页中使用的最多的一个单位, * 一个像素就相当于我们屏幕中的一个小点, * 我们的屏幕实际上就是由这些像素点构成的 * 但是这些像素点,是不能 ...

  4. Window Server 2008 R2 FTP服务用户隔离

    Window Server 2008 R2 FTP服务用户隔离 原题:安装FTP服务,新建一个FTP站点,主目录为C:\ftproot,通过适当技术实现用户soft1 与soft2通过匿名方式登录FT ...

  5. ln 硬链接与软链接

    1. 命令功能 ln 可以看做是link的简写,功能是创建链接文件,链接文件包括硬链接(hard link)和软链接(符号链接,symbolic link) 2. 语法格式 ln  [option]  ...

  6. Java面向对象(二) 接口、多态和泛型

    一.接口 二.多态 多态是同一个行为具有多个不同表现形式或形态的能力. 2.1 类型转换 转换方式 隐式 向上转型 对于基本数据类型,存储容量低的可自动向存储容量高的类型转换 对于引用变量,子类可被转 ...

  7. [CSP-S模拟测试]:建设城市(city)(组合数学+容斥)

    题目传送门(内部题8) 输入格式 一行三个整数$n,m,k$. 输出格式 一行一个整数表示答案.对$998244353$取模. 样例 样例输入 3 7 3 样例输出 数据范围与提示 对于10%的数据, ...

  8. json和list转换

    1.json转list List<TenantMember> tm= (List<TenantMember>)JSONArray.toCollection(JSONArray. ...

  9. 问候 UEditor 的大爷

    记录该日志的时间是2015年2月1日. 先给出 UEditor 项目的首页,它是一款由百度开发的开源富文本编辑器,关于它的介绍,大家可以查看百度百科. UEditor - 首页http://uedit ...

  10. Cisco Packet Tracer基本操作

    IOS主要模式模式 描述 提示符用户执行模式 路由器受限检查,远程访问 Router>特权执行模式 路由器的详细检查:调试和测试,文件处理,远程访问 Router#全局配置模式 全局配置命令 R ...