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 ...
随机推荐
- Mac OS安装Scrapy
个人觉得掌握简单的爬虫知识非常有用,特别是想要从一些特定网站自动地下载一些资源或者统计一些数据,非常的有用.对于产品经理来说,如果要了解竞争产品.替代产品的价格,可以写一个爬虫脚本从各大电商网站爬取相 ...
- 使用jsoncpp解析生成json
在此站点下载jsoncpp(https://sourceforge.net/projects/jsoncpp/这个站点的版本较旧) 在电脑上安装Python,运行amalgamate.py,生成的di ...
- 通俗易懂EJB
摘自:http://blog.csdn.net/jojo52013145/article/details/5783677 1. 我们不禁要问,什么是"服务集群"?什么是" ...
- Servlet session的理解
servlet参见http://blog.csdn.net/bryanliu1982/article/details/5214899 session参见http://lavasoft.blog.51c ...
- YTU 2425: C语言习题 输出月份
2425: C语言习题 输出月份 时间限制: 1 Sec 内存限制: 128 MB 提交: 476 解决: 287 题目描述 编写一程序,输入月份号,输出该月的英文月名.例如,输入3,则输出Mar ...
- vue中使用axios post上传头像/图片并实时显示到页面
在前端开发中,为了更好的用户体验,在头像上传时会先将图片显示到页面然后点击保存按钮 完成图片的上传成功 代码部分有参考他人的写法. html代码: <div id="myPhoto ...
- codeforces 673D D. Bear and Two Paths(构造)
题目链接: D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input ...
- MyBatis学习 之 五、MyBatis配置文件
在定义sqlSessionFactory时需要指定MyBatis主配置文件: <bean id="sqlSessionFactory" class="org.myb ...
- 【POJ 1655】 Balancing Act
[题目链接] 点击打开链接 [算法] 树形DP求树的重心 [代码] #include <algorithm> #include <bitset> #include <cc ...
- Git如何删除自己创建的项目
版本管理器第二行最右边,找到倒三角,下面的Edit Project,拖动鼠标到最下面,Remove project ,弹出框Confirmation required里面输入项目名字,如项目名字为“w ...