# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
719111 | irmuun | Beads and wires (APIO14_beads) | C++17 | 0 ms | 0 KiB |
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 pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
const ll N=10005;
ll dp[2][N];
vector<pair<ll,ll>>adj[N];
void dfs(ll v,ll par){
for(auto [u,w]:adj[u]){
if(u!=par){
dp[1][u]=w;
dfs(u,v);
dp[0][v]=max(dp[0][v]+dp[0][u],dp[1][v]+dp[1][u]);
dp[1][v]+=dp[0][u];
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n;
cin>>n;
for(ll i=1;i<n;i++){
ll u,v,w;
cin>>u>>v>>w;
dv[u].pb({v,w});
dv[v].pb({u,w});
}
ll ans=0;
for(ll i=1;i<=n;i++){
fill(dp[0],dp[0]+n+1,0);
fill(dp[1],dp[1]+n+1,0);
dfs(i,-1);
ans=max(ans,dp[1][i]);
}
cout<<ans;
}