AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 商店
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

原文

作者: 作者的头像   LittleLily ,  2022-07-24 17:02:39 ,  所有人可见 ,  阅读 5


0


984 #include <cstdio> #include <iostream> #include <cmath> using namespace std; typedef long long ll; const int maxn=10000010; ll n,cnt,ans; ll sl[maxn],head[maxn]; struct node{ int w,to,nt; } e[maxn*2]; void add(int x,int y,int z){ cnt++; e[cnt].to = y; e[cnt].nt = head[x]; e[cnt].w = z; head[x] = cnt; } void dfs(int x,int fa){ sl[x]=1; for(int i=head[x];i;i=e[i].nt){ int to=e[i].to; if(fa==to) continue; dfs(to,x); sl[x] += sl[to]; ans += e[i].w*abs(2*sl[to]-n); } } int main(){ cin>>n; for(int i=1;i<n;i++){ int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } dfs(1,0); cout<<ans<<endl; return 0; }

0 评论

你确定删除吗?
1024
x

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息