This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define pill pair<ll, ll>
using namespace std;
const ll N = 1e4 + 10;
const ll big = 1e9;
ll n;
vector<pair<int,int>> g[N];
int dp2[N][3];
int ans;
void dfs(int v, int p, int z) {
dp2[v][0] = 0, dp2[v][1] = -big, dp2[v][2] = -big;
for(auto u : g[v]) {
if(u.f != p) {
dfs(u.f, v, u.s);
int z = max(dp2[u.f][0], dp2[u.f][1] + u.s);
dp2[v][2] = max(dp2[v][2] + z, dp2[v][1] + dp2[u.f][0] + u.s);
dp2[v][1] = max(dp2[v][1] + z, dp2[v][0] + dp2[u.f][0] + u.s);
dp2[v][0] = dp2[v][0] + z;
}
}
ans = max(ans, dp2[v][2]);
}
int main() {
speed;
cin >> n;
for(int i = 1, a, b, c; i < n; i++)
cin >> a >> b >> c, g[a].pb(mp(b, c)), g[b].pb(mp(a, c));
for(int i = 1; i <= n; i++)
dfs(i, 0, 0);
cout << ans;
}
# | 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... |