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 ...
随机推荐
- vmware Esxi 回收Thin模式磁盘空间
本文来自:https://blog.csdn.net/happyfreeangel/article/details/85075179 第一部分 回收Thin模式磁盘空间背景:在使用Thin模式的磁盘, ...
- js 兼容各类手机 的写法 待续
//通过高度来判断是否是iPhone 4还是iPhone 5 isPhone4inches = (window.screen.height==480); isPhone5inches = (windo ...
- 优化cocos2d/x程序的内存使用和程序大小
本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2d/1043.html ☞ ...
- Python int操作
a = 1 # 1 print(a.bit_length()) #计算一个数字的二进制长度.没啥用
- 揭秘Keras推荐系统如何建立模型、获取用户爱好
你是否有过这样的经历?当你在亚马逊商城浏览一些书籍,或者购买过一些书籍后,你的偏好就会被系统学到,系统会基于一些假设为你推荐相关书目.为什么系统会知道,在这背后又藏着哪些秘密呢? 荐系统可以从百万甚至 ...
- 4.1 shell文本过滤、find、awk、grep
文本过滤涉及到以下知识: 正则表达式.find.grep.awk.sed.合并与分割(sort.uniq.join.cut.paste.split). 正则表达式: 基本元字符集及其含义: 匹配IP地 ...
- linux下如何删除十字符libudev.so病毒文件
服务器不停的向外发包,且CPU持续100%,远程登录后查看发现有一长度为10的随机字符串进程,kill掉,会重新生成另外长度为10的字符串进程.删除文件也会重复生成,非常痛苦.查阅crond相关日志, ...
- 《DSP using MATLAB》Problem 4.14
代码: %% ---------------------------------------------------------------------------- %% Output Info a ...
- 《DSP using MATLAB》Problem 3.10
用到了三角窗脉冲序列,各小题的DTFT就不写公式了,直接画图(这里只贴长度M=10的情况). 1. 代码: %% ------------------------------------------- ...
- 网络流--最小费用最大流MCMF模板
标准大白书式模板 #include<stdio.h> //大概这么多头文件昂 #include<string.h> #include<vector> #includ ...