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;
const int nx=2e5+5;
#define ll long long
ll n, u, v, w, dp[2][nx];
vector<pair<ll, ll>> d[nx];
void dfs(int u, int p, int pw)
{
vector<int> df;
for (auto [v, w]:d[u]) if (v!=p) dfs(v, u, w), dp[0][u]+=dp[1][v], df.push_back(w+dp[0][v]-dp[1][v]);
sort(df.begin(), df.end());
reverse(df.begin(), df.end());
if (w!=-1&&df.size()>=1) dp[1][u]=max(dp[0][u], dp[0][u]+df[0]+pw);
if (df.size()>=2) dp[0][u]=max(dp[0][u], dp[0][u]+df[0]+df[1]);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<n; i++) cin>>u>>v>>w, d[u].push_back({v, w}), d[v].push_back({u, w});
dfs(1, 1, -1);
cout<<dp[0][1];
}
/*
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... |