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>
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
using namespace std;
const int N = (int) 2e5 + 7;
const int M = (int) 2e6 + 7;
const ll LINF = (ll) 1e16;
const int INF = (int) 1e9 + 7;
const double EPS = (double) 1e-9;
int n;
struct edge {
int a, b, c;
edge() {}
};
edge a[N];
ll dp[N][3];
vector<int> v[N];
bool ok(edge x, edge y) {
if (x.a == y.a || x.a == y.b) return true;
if (x.b == y.a || x.b == y.b) return true;
return false;
}
void dfs(int x, int p = -1) {
for (int mod = 0; mod < 3; mod++) {
dp[x][mod] = -LINF;
}
dp[x][1] = 0;
for (auto it : v[x]) {
int go = a[it].a ^ a[it].b ^ x;
if (go == p)
continue;
dfs(go, x);
vector<ll> tmp(3);
for (int mod = 0; mod < 3; mod++) {
tmp[mod] = dp[x][mod];
}
for (int mod = 0; mod < 3; mod++) {
for (int t = 0; t < 3; t++) {
tmp[(mod + t) % 3] = max(tmp[(mod + t) % 3], dp[x][mod] + dp[go][t] + a[it].c);
}
}
for (int mod = 0; mod < 3; mod++) {
dp[x][mod] = tmp[mod];
}
}
}
int main() {
#define fn "balls"
#ifdef witch
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen(fn".in", "r", stdin);
// freopen(fn".out", "w", stdout);
#endif
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> a[i].a >> a[i].b >> a[i].c;
v[a[i].a].pb(i);
v[a[i].b].pb(i);
}
dfs(1);
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[i][0]);
}
cout << ans;
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... |