vjudge

智商掉线...

可以发现一条边能贡献其他点当且仅当两点路径上这个边权值最小,所以如果按照边权从大到小加边,每加一条边就会合并两个联通块,那么一个联通块内的点到另一个联通块的点的权值就都是那条边的边权,所以可以给两个联通块内的点答案分别加上边权\(*\)另一个联通块点数.然后这个可以用类似重构树的方法维护,具体是每次给两个联通块根节点打标记,然后查询某个点就是根到这个点路径上的标记和

#include<bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define db double using namespace std;
const int N=2e5+10;
int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
struct node{int x;LL y;};
struct edge
{
int x,y,z;
bool operator < (const edge &bb) const {return z>bb.z;}
}e[N];
int n,pt,ff[N<<1],sz[N<<1],ch[N<<1][2];
LL tg[N<<1],ans;
int findf(int x){return ff[x]==x?x:ff[x]=findf(ff[x]);}
queue<node> q; int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;++i)
ff[i]=i,sz[i]=1;
for(int i=1;i<=n+n;++i) tg[i]=ch[i][0]=ch[i][1]=0;
for(int i=1;i<n;++i)
{
int x=rd(),y=rd(),z=rd();
e[i]=(edge){x,y,z};
}
sort(e+1,e+n);
pt=n;
for(int i=1;i<n;++i)
{
int x=findf(e[i].x),y=findf(e[i].y),z=e[i].z;
tg[x]+=1ll*sz[y]*z,tg[y]+=1ll*sz[x]*z;
++pt,ff[x]=ff[y]=ff[pt]=pt,sz[pt]=sz[x]+sz[y],ch[pt][0]=x,ch[pt][1]=y;
}
ans=0;
q.push((node){pt,0});
while(!q.empty())
{
int x=q.front().x;
LL di=q.front().y;
q.pop();
ans=max(ans,di);
if(!x) continue;
q.push((node){ch[x][0],di+tg[ch[x][0]]});
q.push((node){ch[x][1],di+tg[ch[x][1]]});
}
printf("%lld\n",ans);
}
return 0;
}

最新文章

  1. 要想提高PHP的编程效率,你必须知道的要点
  2. 一步一步学ROP之linux_x64篇
  3. springmvc js/css路径问题
  4. hdu4976 A simple greedy problem. (贪心+DP)
  5. phoenix与spark整合
  6. information_schema系列二(列,列权限,事件,存储引擎)
  7. 某些版本的IIS可能有SessionID混淆的Bug
  8. VC 对话框背景颜色、控件颜色(三种方法)
  9. ASP.NET- 播放视频代码
  10. python标准库 difflib-比较序列
  11. Minecraft
  12. Java-ServletContextAttributeListener
  13. SQL Server - case when...then...else...end
  14. 解决linux用户切换失败 su:execute /usr/bin 没有权限
  15. sed 随笔
  16. knockoutjs关于ko.bindingHandlers的updata订阅
  17. NOIP2018题解
  18. JAVA框架 Spring AOP注解
  19. python 元类(metaclass)
  20. Tornado源码分析之http服务器篇

热门文章

  1. 一致性Hash 分析和实现
  2. Vue源码阅读一:说说vue.nextTick实现
  3. CountDownLatch和CyclicBarrier的比较
  4. [Java]算术表达式求值之一(中序表达式转后序表达式方案)
  5. mysql replaceinto VS insertinto
  6. leetcode -1 count the path
  7. hibernate本地验证
  8. leetcode 118. Pascal&#39;s Triangle 、119. Pascal&#39;s Triangle II 、120. Triangle
  9. 【转】JS正则验证邮手机、箱等格式
  10. 利用开源SlidingMenu框架实现左右侧滑菜单的功能