当前位置:网站首页>Change the root
Change the root
2022-06-10 20:03:00 【2020100XWH】
Sign in — major IT Written interview preparation platform _ Cattle from
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5000;
struct edge{
ll to,next,w;
}e[N*2];
ll head[N],tot,flow[N],deg[N],sum,ans[N];
void addedge(ll u,ll v,ll z)
{
e[tot].to=v;
e[tot].w=z;
e[tot].next=head[u];
head[u]=tot++;
}
void dfs1(ll x,ll fa)
{
ll to;
for(int i=head[x];i!=-1;i=e[i].next)
{
to=e[i].to;
if(to==fa)
continue;
dfs1(to,x);
if(deg[to]==1)
flow[x]+=e[i].w;
else flow[x]+=min(e[i].w,flow[to]);
}
}
bool vis[N];
void dfs2(ll x)
{
ll to;
vis[x]=1;
for(ll i=head[x];i!=-1;i=e[i].next)
{
to=e[i].to;
if(vis[to])
continue;
if(deg[x]>1)
{
//flow[to]+=flow[x]-min(e[i].w,flow[to]);
ans[to]=flow[to]+min(ans[x]-min(flow[x],e[i].w),e[i].w);
}
else {
ans[to]=flow[to]+e[i].w;
}
dfs2(to);
}
}
int main ()
{
int t;
cin>>t;
ll x,y,z;
ll n;
while(t--)
{
tot=0;
memset(head,-1,sizeof(head));
memset(flow,0,sizeof(flow));
memset(deg,0,sizeof(deg));
cin>>n;
for(int i=1;i<n;++i)
{
vis[i]=0;
cin>>x>>y>>z;
addedge(x,y,z);
addedge(y,x,z);
deg[x]++;
deg[y]++;
}
dfs1(1,-1);
// for(int i=1;i<=n;++i)
// cout<<flow[i]<<" ";
//cout<<sum<<"\n";
ans[1]=flow[1];
dfs2(1);
sum=ans[1];
for(int i=2;i<=n;++i)
{
sum=max(sum,ans[i]);
}
cout<<sum<<endl;
}
return 0;
}New array record prevention dfs The disorderly , Define traffic, especially leaf nodes , When the leaf node is replaced, it is handled separately
边栏推荐
- During the college entrance examination this year, all examination sites were in good order, and no sensitive cases affecting safety occurred
- First batch! Sinomenine has passed CWPP capability assessment and inspection of Xintong Institute
- 网上开期货账户安全吗?如何避免被骗?
- This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
- [C language] have you mastered these classic questions? Learn these questions in one article
- Does the giraffe's neck grow longer not because it eats leaves from high places? Scientists have found the answer in fossils 17million years ago
- Rmarkdown 轻松录入数学公式
- After the college entrance examination, VR panoramic tour will show you the beautiful scenery of the scenic spot
- 2022.05.25 (lc_718_longest repeating subarray)
- 叮咚抢菜-派送时段监听及推送工具
猜你喜欢

专项测试之「 性能测试」总结

How to add independent hotspots in VR panoramic works?

During the college entrance examination this year, all examination sites were in good order, and no sensitive cases affecting safety occurred

Zabbix_原理架构-安装部署-自定义监控

VR全景作品中各式各样的嵌入功能是如何做到的?

HW blue team intermediate interview reply

2022.05.24 (lc_674_longest continuous increasing sequence)

基于改进SEIR模型分析上海疫情

刷脸认证如何实现人脸又快又准完成校验?

Performance test plan (plan) template
随机推荐
专项测试之「 性能测试」总结
C (pointer 02)
2022.05.27 (lc_647_palindrome substring)
2022.05.28 (lc_5_longest palindrome substring)
Trilogy to solve the problem of playing chess first and then
Before we learn about high-performance computing, let's take a look at its history
Integrate machine learning to make Chrome browser more "understand" you
First batch! Sinomenine has passed CWPP capability assessment and inspection of Xintong Institute
Micronet practice: image classification using micronet
TiDB - 快速入门,集群搭建
Bit operation topic
How to apply VR panorama in home decoration? Experience the real home decoration effect
大学生毕业季找房,VR全景看房帮你线上筛选
Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close registration | geek headlines
seata 还是不支持sqlserver吗?
4.35V锂电充电IC
2022 software test interview strategy for the strongest version of fresh students to help you get directly to the big factory
Spark ShuffleManager
Domain Driven Design (VI) - Architecture Design
[C language] have you mastered these classic questions? Learn these questions in one article