// 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大
// i 到 j的最大权值为 i到j所经历的树边容量的最小值
// 第一感觉是树上的dp
// 后面发现不可以
// 看了题解说是并查集
// 然后发现这不就是在最小生成树那个模板上做其它操作吗、、
// 的确是好题
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <string.h>
using namespace std;
#define maxn 200010
#define LL long long
struct Eg{
int a,b,w;
bool operator<(const Eg &t)const{
return w>t.w;
}
}E[maxn<<];
int f[maxn];
LL cnt[maxn],sum[maxn];
int Find(int x){
if(x!=f[x])
f[x]=Find(f[x]);
return f[x];
}
int main(){
int n;
int i;
while(scanf("%d",&n)!=EOF){
for(i=;i<n-;i++)
scanf("%d %d %d",&E[i].a,&E[i].b,&E[i].w);
sort(E,E+n-);
for(i=;i<=n;i++){
f[i]=i;
cnt[i]=;
sum[i]=;
}
LL ans=;
int u,v;
LL su,sv;
for(i=;i<n-;i++){
u=Find(E[i].a);
v=Find(E[i].b);
su=cnt[v]*E[i].w+sum[u];
sv=cnt[u]*E[i].w+sum[v];
if(su>sv){
f[v]=u;
cnt[u]+=cnt[v];
sum[u]=su;
}else {
f[u]=v;
cnt[v]+=cnt[u];
sum[v]=sv;
}
ans=max(ans,max(su,sv));
}
printf("%lld\n",ans);
}
return ;
}

zoj 3659 Conquer a New Region的更多相关文章

  1. 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 ...

  2. zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

  3. zoj 3659 Conquer a New Region(并查集)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...

  4. HDOJ 4424 Conquer a New Region

    并检查集合 侧降序,每增加一个侧面应该推断,其中基本建设方..... Conquer a New Region Time Limit: 8000/4000 MS (Java/Others)    Me ...

  5. ZOJ3659 Conquer a New Region 并查集

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

  6. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  7. zoj 3659

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3659 #include<cstdio> #inclu ...

  8. hdu4424 Conquer a New Region 并查集/类似最小生成树

    The wheel of the history rolling forward, our king conquered a new region in a distant continent.The ...

  9. HDU 4424 Conquer a New Region

    http://acm.hdu.edu.cn/showproblem.php?pid=4424 [题目大意] 给你N个点和N-1条边的连通图,也就是说任意两点间的路径是唯一的.每条边有个权值,从一点到另 ...

随机推荐

  1. EBP与ESP寄存器的使用

    push ebp mov esp,ebp esp是堆栈指针 ebp是基址指针 这两条指令的意思是将栈顶指向ebp的地址 ---------------------------------------- ...

  2. 【WCF--初入江湖】03 配置服务

    03 配置服务 数据库 生成数据库脚本: CREATE DATABASE [EmployeeDb]; CREATE TABLE [dbo].[T_Employee]( [Id] [,) NOT NUL ...

  3. 01-05-01-1【Nhibernate (版本3.3.1.4000) 出入江湖】延迟加载及其class和集合(set、bag等)的Lazy属性配置组合对Get和Load方法的影响

    这篇文章 http://ayende.com/blog/3988/nhibernate-the-difference-between-get-load-and-querying-by-id One o ...

  4. oracle 快速删除大批量数据方法(全部删除,条件删除,删除大量重复记录)

    oracle 快速删除大批量数据方法(全部删除,条件删除,删除大量重复记录) 分类: ORACLE 数据库 2011-05-24 16:39 8427人阅读 评论(2) 收藏 举报 oracledel ...

  5. Dijsktra算法C++实现

    Dijsktra算法解决了有向图G=(V,E)上带权的单源最短路径问题.但要求所有边的权值非负. 思想:Dijkstra算法中设置了一顶点集合S,从源点s到集合中的顶点的最终最短路径的权值均已确定.算 ...

  6. who is in front of me 解题报告

    题目描述:N(1<=N<=50005)个学生站成一个纵队,每个人只能看到前面身高比他高(严格大于)的人 求所有人中能看到的最大人数 分析:对于某个人A,设前面第一个身高比他高的人是B.如果 ...

  7. 简单易懂的现代魔法——Play Framework攻略1

    哇哈哈,寒假结束啦,于是我又开新坑了....这次的主角可是大名鼎鼎的Play Framework!!那么闲话少说,开始攻略吧! 1.什么是Play Framework? 大名鼎鼎的play frame ...

  8. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

  9. python 解析 xml

    <taskList nextId="62292"> <task module="reliability" owner="vprovo ...

  10. java io流缓冲理解

    bufferedinputstream和bufferedoutputstream:这两个类是在inputstream和outputstream的基础上增加了一个buffer的缓冲区,从而使数据不直接写 ...