POJ 2395 Out of Hay

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

判断ans==n-1时,即找到了最大边。

  

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
int par[];
int ran[];
struct edge{
int from,to,cost;
};
edge es[];
bool cmp(const edge& x,const edge& y){
return x.cost<y.cost;
}
void init(int n){
for(int i=;i<n;i++){
par[i]=i;
ran[i]=;
}
}
int find(int x){
if(par[x]==x) return x;
return par[x]=find(par[x]);
}
void unite(int x,int y){
x=find(x);
y=find(y);
if(x==y) return ;
if(ran[x]<ran[y]) par[x]=y;
else{
par[y]=x;
if(ran[x]==ran[y])
ran[x]++;
}
}
bool same(int x,int y){
return find(x)==find(y);
}
int main()
{
int n,m,k,res;
scanf("%d%d",&n,&m);
int a,b,c;
k=;
while(m--){
scanf("%d%d%d",&a,&b,&c);
a--;b--;
es[k].from=a;
es[k].to=b;
es[k++].cost=c;
es[k].from=b;
es[k].to=a;
es[k++].cost=c;
}
sort(es,es+k,cmp);
init(n);//注意并查集的初始化
int ans=;
for(int i=;i<k;i++){
edge e=es[i];
if(!same(e.to,e.from)){
unite(e.to,e.from);
ans++;//单独标记已用过的边数
if(ans==n-){
res=e.cost;
break;
}
}
}
printf("%d\n",res);
return ;
}

POJ 2395 Out of Hay(最小生成树中的最大长度)的更多相关文章

  1. Poj 2395 Out of Hay( 最小生成树 )

    题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价 ...

  2. poj 2395 Out of Hay(最小生成树,水)

    Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...

  3. 瓶颈生成树与最小生成树 POJ 2395 Out of Hay

    百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...

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

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

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

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

  6. poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)

    http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...

  7. POJ 2395 Out of Hay(MST)

    [题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...

  8. POJ 2395 Out of Hay (prim)

    题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...

  9. POJ 2395 Out of Hay

    这个问题等价于求最小生成树中权值最大的边. #include<cstdio> #include<cstring> #include<cmath> #include& ...

随机推荐

  1. c# 集合ArrayList;特殊集合Stack、Queue

    一)  ArrayList 1.foreach遍历数组中各个元素,执行内部语句 2.  3. 4.  myarry.Clear();//将集合清空 bool b = myarry.Contains(3 ...

  2. php pack、unpack、ord 函数使用方法

    string pack ( string $format [, mixed $args [, mixed $... ]] ) Pack given arguments into a binary st ...

  3. phpMyAdmin 完整路径泄露漏洞

    漏洞名称: phpMyAdmin 完整路径泄露漏洞 CNNVD编号: CNNVD-201307-650 发布时间: 2013-08-09 更新时间: 2013-08-09 危害等级: 中危   漏洞类 ...

  4. WPF之插件开发

    一:解决方案管理器截图 效果图: 二:简单功能说明 IMsg定义了一个接口,MYPlugin1实现接口功能,”插件式开发“实现程序运行时再调用非本身引用的dll文件,调用该dll的方法实现功能 三:I ...

  5. 使用DateDiff方法获取日期时间的间隔数

    一:用DateDiff方法获取日期时间的间隔数,截图 二:代码 using System; using System.Windows.Forms; using Microsoft.VisualBasi ...

  6. Remove Duplicates from Sorted List II ——LeetCode

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  7. Combination Sum III —— LeetCode

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. [Locked] Missing Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  9. Mobile Computing-天平难题-Uva1354(回溯枚举二叉树)

    原题:https://uva.onlinejudge.org/external/13/1354.pdf 有s块石头,每块都被一根绳子吊着,如果有两个及以上的石头,需要平衡的天平把所有的石头挂起来. 房 ...

  10. SRM 401(1-250pt, 1-500pt)

    DIV1 250pt 题意:给一个整数f,则这样的正整数整数数列称为好数列:数列元素a0 >= a1 >= a2...,且a0<= f, a1 <= f-1, a2 <= ...