POJ2395 Out of Hay

寻找最小生成树中最大的边权。

使用 Kruskal 求解,即求选取的第 \(n-1\) 条合法边。

时间复杂度为 \(O(e\log e)\) 。

#include<stdio.h>
#include<string.h>
#include<algorithm> using namespace std; const int maxn = 10005;
int n, m, tot, f[2005];
struct edge{
int from, to, w;
bool operator < (const edge & _edge) const {
return w < _edge.w;
}
}e[maxn]; void init(){
for(int i = 1; i <= n; i++) f[i] = i;
}
int father(int x){
if(f[x] != x){
f[x] = father(f[x]);
}
return f[x];
}
void _union(int a, int b){
int fa = father(a), fb = father(b);
f[fa] = f[fb];
}
int ok(int a, int b){
int fa = father(a), fb = father(b);
return fa == fb ? 1 : 0;
}
int main()
{
scanf("%d%d", &n, &m);
init();
for(int i = 1; i <= m; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
e[++tot].from = a; e[tot].to = b; e[tot].w = c;
}
sort(e + 1, e + 1 + m);
int cnt = 0;
for(int i = 1; i <= m; i++){
if(!ok(e[i].from, e[i].to)){
_union(e[i].from, e[i].to);
cnt++;
if(cnt == n - 1){
printf("%d\n", e[i].w); break;
}
}
}
return 0;
}

POJ2395 Out of Hay(求最小生成树中最大的边权,Kruskal)的更多相关文章

  1. POJ 2485 Highways (求最小生成树中最大的边)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  2. poj 2485 求最小生成树中 最长的一条边

    Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim # include <iostr ...

  3. POJ 2395 Out of Hay( 最小生成树 )

    链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...

  4. POJ 2395 Out of Hay(最小生成树中的最大长度)

    POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时, ...

  5. POJ 2395 Out of Hay(求最小生成树的最长边+kruskal)

    Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18472   Accepted: 7318 Descr ...

  6. cogs—— 310. [POJ2395] Out of Hay

    310. [POJ2395] Out of Hay ★☆   输入文件:outofhay.in   输出文件:outofhay.out   简单对比 时间限制:1 s   内存限制:128 MB De ...

  7. HDU-1233 还是畅通工程 (prim 算法求最小生成树)

    prim 算法求最小生成树 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. 求最小生成树(Prim算法)(1075)

    Description 求出给定无向带权图的最小生成树.图的定点为字符型,权值为不超过100的整形.在提示中已经给出了部分代码,你只需要完善Prim算法即可. Input 第一行为图的顶点个数n    ...

  9. prime算法求最小生成树(畅通工程再续)

    连着做了四道畅通工程的题,其实都是一个套路,转化为可以求最小生成树的形式求最小生成树即可 这道题需要注意: 1:因为满足路的长度在10到1000之间才能建路,所以不满足条件的路径长度可以初始化为无穷 ...

随机推荐

  1. SVN与Git的优点差异比较

    今天自己还是很有进步的,但是 下午的进度很慢,学习还是得回去,不能在工位进行 在网上看到一篇有关于SVN与Git的区别 复制下来了,以后可以经常看看 一. 集中式vs分布式 1. Subversion ...

  2. 小白如何入门 Python 爬虫?

    本文针对初学者,我会用最简单的案例告诉你如何入门python爬虫! 想要入门Python 爬虫首先需要解决四个问题 熟悉python编程 了解HTML 了解网络爬虫的基本原理 学习使用python爬虫 ...

  3. 常用css相关笔记

    最后一个css不加样式 .nav-sort li:not(:last-child) { border-bottom:#3e3e3e 1px solid; } 垂直居中 vertical-align: ...

  4. ResultEntity

    就是封装的一个map集合  省时省力好用 package com.ujy.utils; import java.util.HashMap; import java.util.Map; public c ...

  5. python引用库异常总结

    一.导入import pandas.io.data as web 时报了"The pandas.io.data module is moved to a separate package & ...

  6. Fokker–Planck equation

    Fokker–Planck equation:https://en.wikipedia.org/wiki/Fokker%E2%80%93Planck_equation 随机微分方程:https://e ...

  7. muduo

    https://blog.csdn.net/zxm342698145/article/details/80689016 https://blog.csdn.net/u010087886/article ...

  8. 以太坊智能合约开发工具 Truffle 入门1

    Truffle是以太坊(Ethereum)智能合约开发的瑞士军刀,小巧好用,上手简单. 本篇文章主要展示如何用Truffle 开发第一个Ethereum智能合约. 1.准备工作:(本人针对window ...

  9. springboot+HttpInvoke 实现RPC调用

    开始用springboot2+hession4实现RPC服务时,发现第一个服务可以调用成功,但第二个就一直报 '<' is an unknown code.第一个服务还是可以调用的.参考网上的方 ...

  10. CSS3边框 圆角效果 border-radius

    border-radius是向元素添加圆角边框 使用方法: border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border-radius: 5px 5px 5px ...