...最小生成树裸题,9月最后一天刷水刷水。

 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXM=+;
const int MAXN=+;
struct Rec
{
int ori,des,len;
bool operator < (const Rec &x) const
{
return len<x.len;
}
}edge[MAXM];
int par[MAXN],height[MAXN];
int n,m;
int ans; void initset()
{
for (int i=;i<=n;i++)
{
par[i]=i;
height[i]=;
}
} int find(int x)
{
int r=x,temp;
while (par[r]!=r) r=par[r];
while (x!=r)
{
temp=par[x];
par[x]=r;
x=temp;
}
return (r);
} void unionset(int fa,int fb)
{
if (height[fa]>=height[fb])
{
par[fb]=fa;
if (height[fa]==height[fb]) height[fa]++;
}
else
par[fa]=fb;
} void init()
{
scanf("%d%d",&n,&m);
for (int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
edge[i].ori=u;
edge[i].des=v;
edge[i].len=w;
}
} void solve()
{
sort(edge,edge+m);
initset();
ans=;
for (int i=;i<m;i++)
{
int fa=find(edge[i].ori);
int fb=find(edge[i].des);
if (fa!=fb)
{
unionset(fa,fb);
ans=max(ans,edge[i].len);
}
}
printf("%d",ans);
} int main()
{
init();
solve();
return ;
}

【最小生成树】BZOJ1682[Usaco2005 Mar]-Out of Hay 干草危机的更多相关文章

  1. bzoj1682[Usaco2005 Mar]Out of Hay 干草危机*

    bzoj1682[Usaco2005 Mar]Out of Hay 干草危机 题意: 给个图,每个节点都和1联通,奶牛要从1到每个节点(可以走回头路),希望经过的最长边最短. 题解: 求最小生成树即可 ...

  2. 1682: [Usaco2005 Mar]Out of Hay 干草危机

    1682: [Usaco2005 Mar]Out of Hay 干草危机 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 391  Solved: 258[ ...

  3. 【BZOJ】1682: [Usaco2005 Mar]Out of Hay 干草危机(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1682 最小生成树裸题.. #include <cstdio> #include < ...

  4. [Usaco2005 Mar]Out of Hay 干草危机

    题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,000)条双向道路,所有 ...

  5. BZOJ 1682: [Usaco2005 Mar]Out of Hay 干草危机

    Description 牛们干草要用完了!贝茜打算去勘查灾情. 有N(2≤N≤2000)个农场,M(≤M≤10000)条双向道路连接着它们,长度不超过10^9.每一个农场均与农场1连通.贝茜要走遍每一 ...

  6. bzoj 1682: [Usaco2005 Mar]Out of Hay 干草危机【并查集+二分】

    二分答案,把边权小于mid的边的两端点都并起来,看最后是否只剩一个联通块 #include<iostream> #include<cstdio> using namespace ...

  7. BZOJ 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机

    题目 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机 Time Limit: 5 Sec  Memory Limit: 64 MB Desc ...

  8. 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机

    1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit:  ...

  9. BZOJ1680: [Usaco2005 Mar]Yogurt factory

    1680: [Usaco2005 Mar]Yogurt factory Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 106  Solved: 74[Su ...

随机推荐

  1. 设计模式之Composite

    设计模式总共有23种模式这仅仅是为了一个目的:解耦+解耦+解耦...(高内聚低耦合满足开闭原则) Composite定义? 将对象以树形结构组织起来,以达成“部分-整体” 的层次结构. 想到Compo ...

  2. spring boot 自定义属性覆盖application文件属性

    参考 Spring boot源码分析-ApplicationListener应用环境: https://blog.csdn.net/jamet/article/details/78042486 加载a ...

  3. 初识PDO数据库抽象层

    目录: 00x1 php中的pdo是什么? 00x2 pdo创建一个PDO对象 00x1 php中的pdo是什么? 就是操作数据库的方法,pdo就是把操作数据库的函数封装成一个pdo类,其间做了安全验 ...

  4. 宋牧春: Linux设备树文件结构与解析深度分析(1) 【转】

    转自:https://mp.weixin.qq.com/s/OX-aXd5MYlE_YoZ3p32qWA 作者简介 宋牧春,linux内核爱好者,喜欢阅读各种开源代码(uboot.linux.ucos ...

  5. [写出来才有价值系列:node.js]node.js 02-,learnyounode

    安装learnyounode: npm install g learnyounode 官方说直接 但是我发现不行,很慢几乎就是死在那里了 还好有淘宝的东西给我们用https://npm.taobao. ...

  6. Linux删除除了今天以外的文件

    [背景] 开发到日志记录功能时,每天都会产生当天的一个日志,久而久之就会产生累积,想要查看的时候,tab键无法自动补全,还要自己额外输入. 比较麻烦. [命令] 经过查找和实验,找到了以下的方法: 1 ...

  7. IP负载均衡技术

    参考链接:http://www.360doc.com/content/12/1117/19/820209_248442094.shtml

  8. 设计模式(一)工厂模式Factory(创建型)(转)

    原文链接:http://blog.csdn.net/hguisu/article/details/7505909 设计模式一 工厂模式Factory 在面向对象编程中, 最通常的方法是一个new操作符 ...

  9. Codefroces 919D Substring(拓扑排序+DP)

    题目链接:http://codeforces.com/problemset/problem/919/D 题目大意:给你一张有向图,给你每个顶点上的字母和一些边,让你找出一条路径,路径上的相同字母数最多 ...

  10. csu 1598(KMP)

    1598: 最长公共前缀 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 109  Solved: 92[Submit][Status][Web Boar ...