#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
#include<cstdio>
#define MAXN 200005
using namespace std;
int F[MAXN];
int num[MAXN];
ll s[MAXN];
struct Edge{
int a,b,cost;
}e[MAXN];
int cmp(Edge &a,Edge &b){
return a.cost>b.cost;
}
int find(int x){
if(F[x]==-) return x;
return F[x]=find(F[x]);
}
int main(){
int n;
while(scanf("%d",&n)==){
for(int i=;i<=n;i++){
F[i]=-;
num[i]=;
s[i]=;
}
for(int i=;i<n-;i++)
scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].cost);
sort(e,e+n-,cmp); //手动合并
int t1,t2;
for(int i=;i<n-;i++){
t1=find(e[i].a);
t2=find(e[i].b);
if(t1!=t2){
s[t2]=max((ll)s[t1]+(ll)e[i].cost*num[t2],(ll)s[t2]+(ll)e[i].cost*num[t1]);
num[t2]+=num[t1];
F[t1]=t2;
}
}
printf("%lld\n",s[t2]);
}
return ;
}

zoj3659的更多相关文章

  1. zoj3659(经典并查集)

    这种思想很经典. 从最小的边选择,那么可以知道的是,在除去这条边的另外两个联通块,选其中一块中的点做为源点到另一块所得到的费用和. 如果你已经知道了这两个联通块内部选一个点时的最大费用和.那么这题就可 ...

  2. ZOJ3659 Conquer a New Region 并查集

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

  3. zoj 3659 并检查集合

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...

随机推荐

  1. 【mmall】学习Spring要善用Spring的Github

    官网:https://projects.spring.io/spring-framework 宠物医院项目(非常经典的Spring项目):https://github.com/spring-proje ...

  2. Selenium: Trying to log in with cookies and get the errorMessage - “Can only set cookies for current domain” or "Unable to set Cookie"

    from selenium import webdriver driver = webdriver.PhantomJS(executable_path='G:/OpenSources/phantomj ...

  3. Linux系统特点

    自由软件 真正的多用户.多任务操作系统 极强的平台可伸缩性 强大的管理功能 强大的网络功能

  4. uniGUI -- Web 应用框架 Delphi + Ext JS

    uniGUI --  Web 应用框架 ,基于 Delphi + Ext JS  技术.

  5. 【HAOI2008】硬币购物

    既然没人写扩欧,那我就来一发吧. 扩欧也还好,就是跑的有点慢,然后写的时候还有点烦,不过还是卡过去了. 考场上看到这道题又蒙了...怎么回事第一题又要爆零了? 然后我打了个暴力测了一下极限数据根本过不 ...

  6. P1262 间谍网络 (tarjan缩点 水过去)

    题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手中掌握的全部情报 ...

  7. JS:判断是否是移动端

    通过User-Agent判断 代码: if(navigator.userAgent.match(/mobile/i)) { //业务层代码 $('body').removeClass("si ...

  8. List<T>常用操作

    1.List<T>类型强制转换: List<UIData> datalist=null;datalist.ConvertAll<object>(input => ...

  9. 最新手机号码验证正则表达式(PHP版本)

    1 前言 手机号码是否合规,则需要校验,可以使用正则表达式. 2 代码 function checkPhoneNumberValidate($phone_number){ //@2017-11-25 ...

  10. CXF使用

    一.服务端: 1.web.xml配置 <servlet> <servlet-name>cxf</servlet-name> <servlet-class> ...