The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20421   Accepted: 7183

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

这题做得好开心,一次AC~

题意:给定一个联通图,推断最小生成树是否唯一。

题解:求出最小生成树后再求次小生成树。若次小生成树的长度与最小生成树相等就说明不唯一,否则唯一。

这是我的第一道次小生成树题,在这里总结下这个算法:

利用一个矩阵max【】【】表示最小生成树中随意两点路径上的最长边权值(关键!!),在求最小生成树时将已经选上的边标记为已用,求完后。遍历剩下未用的边,这条边若加入到最小树中必然构成回路。所以此时须要去掉原来树中那条回路中的最大值。也就是max矩阵存储的值。所以问题转换成找到一条未用的边,使得它跟相应于max矩阵里的边差值最小。遍历之后,次小生成树的值即为原最小生成树的值加上这个最小的差值。

#include <stdio.h>
#include <string.h>
#include <limits.h>
#define maxn 102
#define maxm (maxn * maxn) >> 1 int head[maxn], max[maxn][maxn];
struct Node{
int u, v, cost, next;
bool vis;
} E[maxm];
bool vis[maxn]; int mini(int a, int b){
return a < b ? a : b;
} int prim(int n, int m)
{
int u, i, tmp, j, len = 0, count = 0;
memset(max, 0x7f, sizeof(max));
memset(vis, 0, sizeof(vis));
vis[1] = 1;
while(count < n - 1){
for(i = 1, tmp = INT_MAX; i <= n; ++i){
if(!vis[i]) continue;
for(j = head[i]; j != -1; j = E[j].next){
if(vis[E[j].v]) continue;
if(E[j].cost < tmp){
tmp = E[j].cost; u = j;
}
}
}
++count; len += tmp;
for(i = 1; i <= n; ++i){
if(!vis[i]) continue;
max[i][E[u].v] = max[E[u].v][i] = E[u].cost;
}
vis[E[u].v] = 1; E[u].vis = 1;
}
return len;
} int getSecLen(int n, int m)
{
int min = INT_MAX, u, v, w;
for(int i = 0; i < m; ++i){
if(E[i].vis) continue;
u = E[i].u; v = E[i].v;
w = E[i].cost;
min = mini(min, w - max[u][v]);
if(min == 0) return 0;
}
return min;
} int main()
{
int t, n, m, i, minLen, secLen;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
for(i = 0; i < m; ++i){
scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost);
E[i].vis = 0; E[i].next = head[E[i].u];
head[E[i].u] = i;
}
minLen = prim(n, m);
secLen = getSecLen(n, m);
if(secLen == 0) printf("Not Unique!\n");
else printf("%d\n", minLen);
}
return 0;
}

POJ1679 The Unique MST 【次小生成树】的更多相关文章

  1. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  2. POJ1679 The Unique MST —— 次小生成树

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

  3. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

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

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

  5. POJ_1679_The Unique MST(次小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  6. POJ_1679_The Unique MST(次小生成树模板)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23942   Accepted: 8492 D ...

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

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

  8. poj1679The Unique MST(次小生成树模板)

    次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...

  9. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  10. poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35999   Accepted: 13145 ...

随机推荐

  1. Windows Apache Django 配置

    所有刚刚学习Django 都会在这里配置的时候遇到问题.这里我简单秒速一个傻瓜步骤,使得大家更容易上手. 一.环境准备 1.windows 32系统 至少是 XPSP3 版本,注意:apache2.X ...

  2. GCC 4.9.0 公布,提升 C++11 和 C++14 特性

    from :http://www.oschina.net/news/51084/gcc-4-9-0 GCC 4.9.0 公布,此版本号是个主要版本号更新,包含了 GCC 4.8.x 系列和之前的 GC ...

  3. Quartz实现动态定时任务

    一. 说明 由于最近工作要实现定时任务的执行,而且要求定时周期是不固定的,所以就用到了quartz来实现这个功能: spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持q ...

  4. wrote a programming language

    https://medium.freecodecamp.org/the-programming-language-pipeline-91d3f449c919

  5. CSDN积分规则具体解释--【叶子】

    前记:在CSDN的社区支持板块,常常看到有人提问,为什么有积分却不能下载,此类问题层出不穷,而论坛的各种积分制度说明又非常分散,不便于寻找,为了方便新注冊用户高速了解论坛的积分规则,也为了降低社区支持 ...

  6. Tomcat 下 Memcached 集群与 Terracotta 集群比较

    总结:Terracotta 集群配置要比Memcached 集群简单,但Terracotta 集群启动的速度要比Memcached 集群慢,性能Terracotta 集群要比Memcached 集群好 ...

  7. 文件权限控制篇access alphasort chdir chmod chown chroot closedir fchdir fchmod fchown fstat ftruncate getcwd

    access(判断是否具有存取文件的权限) 相关函数 stat,open,chmod,chown,setuid,setgid 表头文件 #include<unistd.h> 定义函数 in ...

  8. C# 编程指南

    此部分详细介绍了 C# 语言主要功能,以及通过 .NET Framework 可以在 C# 中使用的功能. 阅读此部分的大部分内容的前提是,你已对 C# 和一般编程概念有一定的了解. 如果完全没有接触 ...

  9. 深入理解Java中的组合和继承

    Java是一个面向对象的语言.每一个学习过Java的人都知道,封装.继承.多态是面向对象的三个特征.每个人在刚刚学习继承的时候都会或多或少的有这样一个印象:继承可以帮助我实现类的复用.所以,很多开发人 ...

  10. MySQL命令行查询结果中文显示乱码

    数据库编码格式为utf8,表和字段也都是utf8,存进去的格式是utf-8 但是用命令行工具查询命令select * from 表名; 查询出来的中文是乱码 原因:MySQL客户端根本就不能以utf8 ...