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. HashMap底层为什么一定用数组

    HashMap源码数据结构: Entry[] table = new Entry[capacity]; 其中,Entry就是一个链表节点.如果将数组替换成LinkedList是否可行?如下: List ...

  2. 异步Promise及Async/Await可能最完整入门攻略

    此文只介绍Async/Await与Promise基础知识与实际用到注意的问题,将通过很多代码实例进行说明,两个实例代码是setDelay和setDelaySecond. tips:本文系原创转自我的博 ...

  3. [七月挑选]windows上面的发音

    title: windows上面的发音 开始 love.vbs: CreateObject("SAPI.SpVoice").Speak "I love YOU" ...

  4. 算法学习之选择排序算法的python实现

    ——参考自<算法图解> def findSmallest(arr): # 假设第一个元素最小 smallest = arr[0] smallest_index = 0 for i in r ...

  5. [NOI2018]归程(kruscal重构树)

    [NOI2018]归程 题面太长辣,戳这里 模拟赛上写了一个spfa (关于spfa,它已经死了),然后一个st表水完暴力跑路.考后说是Kruscal重构树或者可持久化并查集???这都是些什么东西.不 ...

  6. element-ui 表格标题换行

     render-header: 列标题 Label 区域渲染使用的 Function <template> <el-table :data="dataList"& ...

  7. update_all_fun(send recv)

    '''Send messages through all edges >>> update all nodes.DGLGraph.update_all(message_func='d ...

  8. bzoj4165 矩阵 堆维护多路归并

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4165 题解 大概多路归并是最很重要的知识点了吧,近几年考察也挺多的(虽然都是作为签到题的). ...

  9. 借助tcpdump统计http请求

    借助tcpdump统计http请求     这里所说的统计http请求,是指统计QPS(每秒请求数),统计前十条被访问最多的url.一般做这样的统计时,我们经常会使用网站访问日志来统计.当我们来到一个 ...

  10. 与Swing的相识

    参考自http://c.biancheng.net/swing/ Swing是一个用于Java GUI编程(图形界面设计)的工具包(类库):换句话说,java可以用来开发带界面的PC软件,使用到的工具 ...