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 LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define int long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=200005;
const int INF=1e18;
const int mod=998244353;
int n;
vector<pair<int,int>> adj[N];
vector dp(2,vector(N,(int)0));
void dfs(int k,int pa,int cost){
vector<int> t;
int cnt=0;
for(auto [j,c]:adj[k]){
if(j==pa)continue;
dfs(j,k,c);
t.push_back(dp[1][j]-dp[0][j]);
cnt+=dp[0][j];
}
sort(all(t),greater<>());
int sz=t.size();
dp[0][k]=cnt;
dp[1][k]=cnt+cost;
if(sz>=1){
int tmp=cnt+t[0];
dp[0][k]=max(dp[0][k],tmp+cost);
}
if(sz>=2){
int tmp=cnt+t[0]+t[1];
dp[0][k]=max(dp[0][k],tmp);
dp[1][k]=max(dp[1][k],tmp+cost);
}
}
int32_t main() {
LCBorz;
int n;cin>>n;
for(int i=1;i<n;i++){
int a,b,c;cin>>a>>b>>c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
dfs(1,1,-INF);
cout<<dp[0][1]<<endl;
return 0;
}
/*
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
# | 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... |