POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST
时间限制: 10 Sec 内存限制: 128 MB
提交: 25 解决: 10
[提交][状态][讨论版]
题目描述
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'.
输入
输出
样例输入
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
样例输出
3
Not Unique!
题意:给一个无向图,判断这个图的最小生成树MST是否是唯一的。如果是唯一的,输出最小生成树的值,如果不是唯一的,输出“Not Unique!”
思路:kruskal的应用。详细思路与prim版相似,而且时间空间复杂度都得到了一些优化。
kruskal算法:把所有的边都排个序,从大到小取出,若取出的这条边的两个端点已经连通(用并查集),则换下一条边,n的顶点用n-1条边就可以相连,循环直到n-1条边。
本题:在找最小生成树(mst)的同时,把选中的边(是第几条)都存下来。再进行多次kruskal算法,每次模拟删除一条边,寻找一条新的边,得到边权和为mst2,判断mst==mst2?即可。
#include <iostream>
#include<string>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int n, m;
int pre[];
int Rank[];
int mst_e[];
struct node
{
int u, v, w;
};
bool cmp(node x, node y)
{
return x.w < y.w;
}
void init()//初始化
{
int i;
for (i = ; i <= n; i++) pre[i] = i;
memset(Rank, , sizeof(Rank));
}
int find(int x)//找根
{
if (pre[x] == x) return x;
return pre[x] = find(pre[x]);
}
void unite(int x, int y)//压缩合并
{
if (Rank[x] < Rank[y]) pre[x] = y;
else
{
pre[y] = x;
if (Rank[x] == Rank[y]) Rank[x]++;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
init();
node a[];
int i;
for (i = ; i <= m; i++)
{
cin >> a[i].u >> a[i].v >> a[i].w;
}
sort(a + , a + + m, cmp);
int mst = ;
int k = ;
for (i = ; i <= m; i++)//第一次kruskal算法
{
int x = find(a[i].u);
int y = find(a[i].v);
if (x != y)
{
unite(x, y);
mst = mst + a[i].w;
mst_e[k++] = i;// 记录下MST的边。
}
}
int edge_num = k-;
bool uni = ;//记录是不是唯一
int mst2, num;
for (k =; k <=edge_num; k++)
{//遍历每一条MST里的边,一次次模拟删除
init();//每进行一次kruskal算法,就初始化一次
mst2 = ;
num = ;
for (i = ; i <= m; i++)
{
if (i == mst_e[k]) continue;//模拟删除
int x = find(a[i].u);
int y = find(a[i].v);
if (x != y)
{
unite(x, y);
mst2 = mst2 + a[i].w;
num++;
}
if (num != edge_num) continue;//边数没达到就继续0
if (mst2 == mst)
{
uni = ;
break;
}
}
if (uni == ) break;
}
if (uni) cout << mst << endl;
else cout << "Not Unique!" << endl;
}
return ;
}
POJ 1679 The Unique MST (次小生成树kruskal算法)的更多相关文章
- POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35999 Accepted: 13145 ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 The Unique MST (判定最小生成树是否唯一)
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
随机推荐
- 用UILabel实现文字滚动播放(跑马灯)效果
- (void)viewDidLoad { [super viewDidLoad]; //数据源 self.messageArray = [NSArray arrayWithObjects: &quo ...
- DevExpress WPF入门指南:DXWindow应用
[DevExpress v17.2 版本更新公开课]点击免费报名 DevExpress WPF Window control有一点非常棒,就是可以和其他视觉主题保持统一性.DXWindow class ...
- SOD范例
SOD申请台站波形数据范例: <?xml version="1.0"?> <sod> <eventArm> <fdsnEvent> ...
- RNN - LSTM - GRU
循环神经网络 (Recurrent Neural Network,RNN) 是一类具有短期记忆能力的神经网络,因而常用于序列建模.本篇先总结 RNN 的基本概念,以及其训练中时常遇到梯度爆炸和梯度消失 ...
- 《C语言中分配了动态内存后一定要释放吗?》
问:比如main函数里有一句 malloc(),后面没有free()1.那么当main结束后,动态分配的内存不会随之释放吗?2.如果程序结束能自动释放,那么还加上free(),是出于什么考虑? 答: ...
- 逆向路由器固件之解包 Part1
这个系列的文章以逆向路由器固件的方式来挖掘路由器中存在的漏洞. 本篇文章主要以介绍解包固件工具为主.文中演示用的固件可以在这里下载.由于针对设备的攻击越来越多,很多厂商把不提供固件下载作为一种安全策略 ...
- SWIFT模糊效果
首先创建一个模糊效果 let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) 接着创建一个承载模糊效果的视图let blurView ...
- [LeetCode&Python] Problem 872. Leaf-Similar Trees
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...
- 在 Ubuntu 18.0-10上安装 MySQL8
直接使用apt install mysql-server安装,那么恭喜你踩坑. sudo apt install mysql-server默认会安装MySQL 5.7,将会出现一些莫名的问题,例如:安 ...
- hdu2073-2078
hdu2073 数学 #include<stdio.h> #include<math.h> double len(double x){ )*(x+)); } int main( ...