hdu 4424 Conquer a New Region (并查集)
///题意:给出一棵树。树的边上都有边权值,求从一点出发的权值和最大,权值为从一点出去路径上边权的最小值
# include <stdio.h>
# include <algorithm>
# include <iostream>
# include <string.h>
using namespace std;
# define MAX 200010
struct node
{
int u,v;
int w;
};
struct node a[MAX];
__int64 dis[MAX];///存以i为根结点的边权和
int father[MAX],rank[MAX];///存以i为根结点的数的节点数
void init()///初始化
{
for(int i=0; i<=MAX; i++)
{
father[i]=i;
rank[i]=1;
dis[i]=0;
}
}
int cmp(node a1,node a2)///边权从大到小
{
return a1.w>a2.w;
}
int find(int x)
{
if(x==father[x])
return x;
return father[x]=find(father[x]);
}
void Union(int x,int y,__int64 v)
{
father[x]=y;
rank[y]+=rank[x];
dis[y]=v;
}
int main()
{
int i,n;
__int64 s1,s2;
while(~scanf("%d",&n))
{
for(i=1; i<n; i++)
scanf("%d%d%d",&a[i].v,&a[i].u,&a[i].w);
init();
sort(a+1,a+n,cmp);///
for(i=1; i<n; i++)
{
int fa=find(a[i].v);
int fb=find(a[i].u);
if(fa!=fb)///树中不会出现fa,fb相等的情况。。。。 {
s1=dis[fa]+a[i].w*rank[fb];///在fa集合中选点
s2=dis[fb]+a[i].w*rank[fa];///在fb集合中选点
}
if(s1>s2)
Union(fb,fa,s1);
else
Union(fa,fb,s2); }
printf("%I64d\n",dis[find(1)]);
}
return 0;
}
hdu 4424 Conquer a New Region (并查集)的更多相关文章
- ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)
这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4424 Conquer a New Region
http://acm.hdu.edu.cn/showproblem.php?pid=4424 [题目大意] 给你N个点和N-1条边的连通图,也就是说任意两点间的路径是唯一的.每条边有个权值,从一点到另 ...
- ZOJ3659 Conquer a New Region 并查集
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- HDU 4424 Conquer a New Region 最大生成树
给你一颗树 每条边有一个权值 选择一个点为中心 定义S值为中心到其它n-1个点的路径上的最小边权 求全部点S值的和 从大到小排序 每次合并2棵树 设为A集合 B集合 设A集合的最大S值的和为sumA ...
- hdu4424 Conquer a New Region 并查集/类似最小生成树
The wheel of the history rolling forward, our king conquered a new region in a distant continent.The ...
- zoj 3659 Conquer a New Region(并查集)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
随机推荐
- ARM平台的内核模块编写与安装
Linux 系统一直在不断地发展,而相应地她的代码量也在不断的增大,直接导致的结果就是她的可执行镜像就变得越来越庞大.那么问题来了,如果将所有的镜像文件一次性地复制到内存中,那么所需的空间就非常 ...
- js实现placeholder效果
<form name="testForm" action="" method=""> <input type=" ...
- A Script Pro nginx URL重写规则无法播放MP4解决方法
I am using nginx and I have already add the line location /file/ { rewrite ^/-]+)/([-]+)/([^/]*)/([- ...
- IOS 企业版证书($299)In-House方式发布指南
一.明确几个概念 1.企业版IDP:即iOS Development Enterprise Program.注意是$299/Year那种,并不是$99/Year的那种. 2.In House:是只企业 ...
- SharePoint 2013 弹窗效果之URL打开方式(一)
在SharePoint中想做一个弹出效果其实很简单,仅仅在js中使用SharePoint Modal Dialog, 以下做一个简单的例子:很多情况下我们会通过linkButton弹出一个详细页面,那 ...
- WebForm
ASP开发方式 格式 <% %> C#代码可以写在里面 <%= %> 往外输出一个值,可以放一个变量,一个方法(这个方法是有返回值的直接打印到界面上去) <%@ %& ...
- Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四)
Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四) 题记:本来计划的SolrCloud的Recovery策略的文章是3篇的,但是没想到Recovery的内容蛮多的,前面 ...
- Comparing randomized search and grid search for hyperparameter estimation
Comparing randomized search and grid search for hyperparameter estimation Compare randomized search ...
- hdu 5139 Formula
http://acm.hdu.edu.cn/showproblem.php?pid=5139 思路:这道题要先找规律,f(n)=n!*(n-1)!*(n-2)!.....1!; 不能直接打表,而是离 ...
- n数码问题, 全排列哈希
转载了一篇关于全排列的哈希函数,Poj1077就是应用了全排列的哈希: 我们经常使用的数的进制为“常数进制”,即始终逢p进1.例如,p进制数K可表示为 K = a0*p^0 + a1*p^1 + ...