...最小生成树裸题,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. Django之ModelForm(一)

    要说ModelForm,那就先说Form吧! 先给出一个Form示例: models.py from django.db import models class UserType(models.Mod ...

  2. cookie、localstroage与sessionstroage的一些优缺点

    1.    Cookie 在前端开发中,尽量少用cooie,原因: (1)   cookie限制大小,约4k左右,不适合存储业务数据,尤其是数据量较大的值: (2)   cookie会每次随http请 ...

  3. 深入理解Spring系列之十二:@Transactional是如何工作的

    转载 https://mp.weixin.qq.com/s/ZwhkUQF1Nun9pNrFI-3a6w 首先从说起.配置了,就必定有对应的标签解析器类,查看NamespaceHandler接口的实现 ...

  4. ios 不支持iframe 解决方案

    在iframe外层在包一层,通过appendChild()把内容增加到容器中,完整代码如下: @section Css { <link href="@ViewHelper.Conten ...

  5. sklearn逻辑回归(Logistic Regression)类库总结

    class sklearn.linear_model.LogisticRegression(penalty=’l2’, dual=False, tol=0.0001, C=1.0, fit_inter ...

  6. jquery如何获取第一个或最后一个子元素

      jquery如何获取第一个或最后一个子元素? 通过children方法,children("input:first-child") $(this).children(" ...

  7. Linux创建ftp并设置权限以及忘记ftp帐号(密码)修改

    忘记ftp密码修改方法: 1.登录服务器 cd  /etc/vsftpdcat ftpusers找到对应的ftp用户名 (如果用户名也忘记了 那么 cd /etc 然后cat passwd 查看用户和 ...

  8. 让Linux应用更加得心应手的

    1.计算文件数和目录数  下面的语句可以帮你计算有多少个文件和多少个目录 # ls -l * |grep "^-"|wc -l ---- to count files # ls - ...

  9. webStorage,离线缓存

    一.webStorage 1.目标 1.了解cookie的不足之处,引入webstorage的概念    2.学习并且掌握webstorage有哪两种    3.学习并且掌握sessionStorag ...

  10. 洛谷 P2871 [USACO07DEC]手链Charm Bracelet 题解

    题目传送门 这道题明显就是个01背包.所以直接套模板就好啦. #include<bits/stdc++.h> #define MAXN 30000 using namespace std; ...