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 INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int n;
int c[200010];
vector<pii> g[200010];
int dp[200010][2];
int par[200010];
bool v[200010];
void dfs(int s){
v[s]=true;
int tot=0;
for(auto pr:g[s]){
int u=pr.ft, c=pr.sc;
if(!v[u]){
par[u]=c;
dfs(u);
tot+=max(dp[u][0], dp[u][1]);
}
}
vector<int> vec;
for(auto pr:g[s]){
int u=pr.ft, c=pr.sc;
if(!v[u]){
vec.pb(dp[u][0]+c-max(dp[u][0], dp[u][1]) );
}
}
sort(vec.begin(), vec.end());
if(vec.size()>=1){
dp[s][1]=tot+vec.back()+par[s];
}
if(vec.size()>=2){
dp[s][0]=tot+vec[vec.size()-1]+vec[vec.size()-2];
}
v[s]=false;
}
int32_t main(){
INIT
cin>>n;
for(int i=1; i<n; i++){
int a, b;
cin>>a>>b>>c[i];
g[a].pb({b, c[i]});
g[b].pb({a, c[i]});
}
dfs(1);
cout<<dp[1][0];
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... |