// 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大
// 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. catci监控

    snmp安装:yum install net-snmp* 配置/etc/snmp/snmpd.conf:com2sec notConfigUser 192.168.79.129    publicac ...

  2. PHP之SQL防注入代码(360提供)

    <?php class sqlsafe { private $getfilter = "'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\ ...

  3. oracle 字符集导入、导出 、转换

    导入导出及转换 导入导出是我们常用的一个数据迁移及转化工具,因其导出文件具有平台无关性,所以在跨平台迁移中,最为常用. 在导出操作时,非常重要的是客户端的字符集设置,也就是客户端的NLS_LANG设置 ...

  4. 【leetcode】Find Peak Element ☆

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  5. PowerDesigner修改设计图中文字的字体大小等样式

    设计图中默认的字体是对英文比较合适的,中文就看不清楚了,特别不美观.但是可以通过修改“Display Preferences”适应我们的汉字. 我使用的PowerDesigner版本是15.1(测试版 ...

  6. 360 chrome不能登录Google账户

    用过Google chrome,枫树,360 chrome国际版,今天还是回到Google chrome了. 因为除了Chrome自己,其他的都不能正常登录Google账户(偶尔偶尔一次可以,直到今天 ...

  7. *[topcoder]IncrementingSequence

    http://community.topcoder.com/stat?c=problem_statement&pm=12107 此题想了半天,当时瞥到了Greedy,所以就想着贪心,最后的方法 ...

  8. React gulp、Browserify、Webpack实例

    一.gulp var gulp = require('gulp'); var react = require('gulp-react'); gulp.task('jsx', function() { ...

  9. 应用程序加载外部字体文件(使用AddFontResource API函数指定字体)

    /* MSDN: Any application that adds or removes fonts from the system font table should notify other w ...

  10. App应用与思考

    我为什么没有加入苹果的iOS APP移动大军?http://blog.csdn.net/Code_GodFather/article/details/7956858 ----------------- ...