This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 2e5+10;
vector<pii> tree[mxn];
int dp[mxn][2];
int N;
int ans = 0;
void dfs(int now,int par,int up){
dp[now][0] = dp[now][1] = 0;
vector<int> v;
for(auto [nxt,w]:tree[now]){
if(nxt == par)continue;
dfs(nxt,now,w);
dp[now][0] += dp[nxt][1];
dp[now][1] += dp[nxt][1];
v.push_back(dp[nxt][0]-dp[nxt][1]+w);
}
sort(v.rbegin(),v.rend());
if(v.empty())dp[now][0] = dp[now][1] = 0;
else if(v.size() == 1)dp[now][0] = 0,dp[now][1] += v[0]+up;
else dp[now][0] += v[0]+v[1],dp[now][1] += v[0]+up;
if(up)ans = max({ans,dp[now][0],dp[now][1]});
else ans = max(ans,dp[now][0]);
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<N;i++){
int a,b,c;
cin>>a>>b>>c;
tree[a].push_back(pii(b,c));
tree[b].push_back(pii(a,c));
}
dfs(1,1,0);
cout<<ans<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |