POJ 3522 Slim Span 暴力枚举 + 并查集
http://poj.org/problem?id=3522
一开始做这个题的时候,以为复杂度最多是O(m)左右,然后一直不会。最后居然用了一个近似O(m^2)的62ms过了。
一开始想到排序,然后扫一个长度n - 1区间,要快速判定这个区间能否构成MST,一直都想不到优秀的算法,然后干脆暴力了。
两种方法,1、dfs,删边容易,标记一下就好,但是这是不行的,删边确实容易,但是dfs的时候多次访问无用的边,所以TLE了。
2、并查集,这个复杂度是O(n)的,能AC,但是我的思路还是有一点bug,就是它不一定是在连续的n - 1个的区间上的。
7 7
1 2 1
2 3 2
3 4 3
4 5 4
5 6 5
4 6 5
5 7 6
0 0
所以,我只枚举起点,然后在后面找一颗MST算了,复杂度好想是O(m^2)啊,。。。。。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
int n, m;
const int maxn = + ;
struct Data {
int u, v, w, id;
bool operator < (const struct Data & rhs) const {
return w < rhs.w;
}
}a[ * ];
int fa[maxn];
int tofind(int u) {
if (u == fa[u]) return u;
else return fa[u] = tofind(fa[u]);
}
bool tomerge(int x, int y) {
x = tofind(x);
y = tofind(y);
if (x == y) return false;
fa[y] = x;
return true;
}
void init() {
for (int i = ; i <= n; ++i) fa[i] = i;
}
bool check() {
int has = ;
for (int i = ; i <= n; ++i) {
has += tofind(i) == i;
if (has == ) return false;
}
return true;
}
void work() {
for (int i = ; i <= m; ++i) {
scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
a[i].id = i;
}
if (m < n - ) {
printf("-1\n");
return;
}
sort(a + , a + + m);
int ans = inf;
for (int i = ; i <= m; ++i) {
int has = ;
if (i + (n - ) - > m) break;
init();
bool flag = true;
int mx;
for (int j = i; j <= m && has != n - ; ++j) {
mx = a[j].w;
if (a[j].w - a[i].w > ans) {
flag = false;
break;
}
if (tomerge(a[j].u, a[j].v)) {
has++;
}
}
if (flag && check()) {
ans = min(ans, mx - a[i].w);
}
}
if (ans == inf) ans = -;
printf("%d\n", ans);
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d%d", &n, &m) > && n + m) work();
return ;
}
POJ 3522 Slim Span 暴力枚举 + 并查集的更多相关文章
- POJ 3522 Slim Span (Kruskal枚举最小边)
题意: 求出最小生成树中最大边与最小边差距的最小值. 分析: 排序,枚举最小边, 用最小边构造最小生成树, 没法构造了就退出 #include <stdio.h> #include < ...
- poj 3522 Slim Span (最小生成树kruskal)
http://poj.org/problem?id=3522 Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions ...
- POJ 3522 Slim Span 最小生成树,暴力 难度:0
kruskal思想,排序后暴力枚举从任意边开始能够组成的最小生成树 #include <cstdio> #include <algorithm> using namespace ...
- POJ 3522 Slim Span 最小差值生成树
Slim Span Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3522 Description Gi ...
- POJ 3522 - Slim Span - [kruskal求MST]
题目链接:http://poj.org/problem?id=3522 Time Limit: 5000MS Memory Limit: 65536K Description Given an und ...
- POJ 3522 ——Slim Span——————【最小生成树、最大边与最小边最小】
Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7102 Accepted: 3761 Descrip ...
- POJ 3522 Slim Span
题目链接http://poj.org/problem?id=3522 kruskal+并查集,注意特殊情况比如1,0 .0,1.1,1 #include<cstdio> #include& ...
- POJ 3522 Slim Span(极差最小生成树)
Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9546 Accepted: 5076 Descrip ...
- SGU 128. Snake --- 暴力枚举+并查集+贪心+计算几何
<传送门> 128. Snake time limit per test: 0.25 sec. memory limit per test: 4096 KB There are N poi ...
随机推荐
- iOS优秀博文合集
一.优化篇 Xcode7中你一定要知道的炸裂调试神技 iOS使用Instrument-Time Profiler工具分析和优化性能问题 dSYM 文件分析工具 二.功能篇 3分钟实现iOS语言本地化/ ...
- java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistrationBean
java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistrationBean 把 ...
- Hive两种访问方式:HiveServer2 和 Hive Client
老版HiveClient: 要求比较多,需要Hive和Hadoop的jar包,各配置环境. HiveServer2: 使得与YARN和HDFS的连接从Client中独立出来, ...
- react native camera
最近在尝试用react native camera iOS版本很方便就调试通过了,react的试用非常方便 android版本要单独试用fork的 屏蔽了lint的报错后也可以调试通过 参考这篇文章填 ...
- bzoj3134: [Baltic2013]numbers
稍微用脑子想一想,要是一个回文数,要么s[i]==s[i+1]要么s[i]==s[i+2]就可以实锤了 所以多开两维表示最近两位选的是什么数就完了 注意前导0 #include<cstdio&g ...
- POJ1087 A Plug for UNIX —— 最大流
题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K T ...
- 织梦DEDEcms首页调用文档整篇内容
DedeCMS在首页调用内容页中的整篇文章 织梦程序是一个很强大的程序,也有很多朋友使用织梦程序来做博客,想要像是在博客中一样能在首页中调用一整篇文章,其实在织梦程序中页可以很完美的实现这个功能. 在 ...
- js程序开发-1
<h1>数组的常用操作</h1> push() 方法可向数组的末尾添加一个或多个元素,并返回新数组的长度. unshift() 方法可向数组的开头添加一个或更多元素,并返回新数 ...
- 对象数组、集合、链表(java基础知识十五)
1.对象数组的概述和使用 * 需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息. Student[] arr = new Student[5]; //存储学生 ...
- 让 SyntaxHighlighter 3.x 支持 Lua 语法着色
1. [代码]shBrushLua.js /** * SyntaxHighlighter * http://alexgorbatchev.com/SyntaxHighlighter * * Synta ...