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){
dp[now][0] = 0;
dp[now][1] = -1e9;
vector<int> v;
for(auto [nxt,w]:tree[now]){
if(nxt == par)continue;
dfs(nxt,now);
dp[now][0] += max(dp[nxt][0],dp[nxt][1]+w);
v.push_back(dp[nxt][0]+w-max(dp[nxt][0],dp[nxt][1]+w));
}
sort(v.rbegin(),v.rend());
if(!v.empty())dp[now][1] = dp[now][0]+v[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));
}
for(int i = 1;i<=N;i++){
dfs(i,i);
ans = max(ans,dp[i][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... |