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 暴力枚举 + 并查集的更多相关文章

  1. POJ 3522 Slim Span (Kruskal枚举最小边)

    题意: 求出最小生成树中最大边与最小边差距的最小值. 分析: 排序,枚举最小边, 用最小边构造最小生成树, 没法构造了就退出 #include <stdio.h> #include < ...

  2. poj 3522 Slim Span (最小生成树kruskal)

    http://poj.org/problem?id=3522 Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions ...

  3. POJ 3522 Slim Span 最小生成树,暴力 难度:0

    kruskal思想,排序后暴力枚举从任意边开始能够组成的最小生成树 #include <cstdio> #include <algorithm> using namespace ...

  4. POJ 3522 Slim Span 最小差值生成树

    Slim Span Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3522 Description Gi ...

  5. POJ 3522 - Slim Span - [kruskal求MST]

    题目链接:http://poj.org/problem?id=3522 Time Limit: 5000MS Memory Limit: 65536K Description Given an und ...

  6. POJ 3522 ——Slim Span——————【最小生成树、最大边与最小边最小】

    Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7102   Accepted: 3761 Descrip ...

  7. POJ 3522 Slim Span

    题目链接http://poj.org/problem?id=3522 kruskal+并查集,注意特殊情况比如1,0 .0,1.1,1 #include<cstdio> #include& ...

  8. POJ 3522 Slim Span(极差最小生成树)

    Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9546   Accepted: 5076 Descrip ...

  9. SGU 128. Snake --- 暴力枚举+并查集+贪心+计算几何

    <传送门> 128. Snake time limit per test: 0.25 sec. memory limit per test: 4096 KB There are N poi ...

随机推荐

  1. 启用了不安全的HTTP方法解决办法 IBM APPSCAN

    启用了不安全的HTTP方法解决办法  IBM APPSCAN     安全风险:       可能会在Web 服务器上上载.修改或删除Web 页面.脚本和文件. 可能原因:       Web 服务器 ...

  2. jQuery 怎么获取对象

    1.JQuery的核心的一些方法 each(callback) '就像循环 $("Element").length; ‘元素的个数,是个属性 $("Element&quo ...

  3. Hibernate的一些使用技巧

    1.Hibernate是如今最流行的开源对象关系映射(ORM)持久化框架,SSH框架组合是很多JavaEE工程的首选,java持久化框架(JPA)的设计师是Hibernate的作者,因此对于Hiber ...

  4. Record is locked by another user

    Oracle修改表中记录时出现record is locked by another user的问题 在操作表时没有commit,导致表被锁,只要执行下面两行语句,就可以了将行锁解锁了. Select ...

  5. 逼近法(例 poj3208、poj1037)

    ​ 逼近法是一种很奇妙的算法,以为"逼近"这一种思想在很多的算法中都有体现.诸如:像我们的二分答案,不断地排除决策集合的一半以接近我们的最终答案:我们的树上倍增求 \(LCA\) ...

  6. install build tools 25.0.2 and sync the project

    install build tools 25.0.2 and sync the project in android studio bundle.gradle,将buildToolsVersion修改 ...

  7. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  8. LoadRunner检查点使用小结

    LR中检查点有两种:图片和文字. 常用检查点函数如下: 1)web_find()函数用于从 HTML 页中搜索指定的文本字符串: 2)web_reg_find()函数注册一个请求,以在下一个操作函数( ...

  9. python读取一个文件的每一行判断是否为素数,并把结果写到另一个文件中

    刚刚学习python的菜鸟,这道题包括:文件的读写,python的参数调用,异常的使用,函数的使用 创建一个文本文件inti_prime.txt 执行命令:python Prime.py init_p ...

  10. C#方法参数总结

    C#中方法的参数的四种类型 C#中方法的参数有四种类型:       1. 值参数类型  (不加任何修饰符,是默认的类型)       2. 引用型参数  (以ref 修饰符声明)       3. ...