# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
48296 | octopuses | Beads and wires (APIO14_beads) | C++17 | 7 ms | 4992 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>
#define ll long long
#define fr first
#define sc second
#define M (ll)(3e17)
using namespace std;
const int N = 200020;
vector < pair < int, int > > G[N];
int n;
int A[N], B[N];
void go(int v, int p = 0)
{
int mx1 = -100, mx2 = -100, child = 0, tot = 0, par = -2e9;
for(int i = 0; i < G[v].size(); ++ i)
{
int to = G[v][i].first;
if(to == p)
{
par = G[v][i].second;
continue;
}
go(to, v);
tot += A[to];
int now = B[to] - A[to] + G[v][i].second;
if(now > mx1)
{
mx2 = mx1;
mx1 = now;
}
else
if(now > mx2)
mx2 = now;
child ++;
}
A[v] = B[v] = tot;
if(child > 1)
B[v] = max(tot + mx1 + mx2, B[v]);
A[v] = max(B[v], A[v]);
for(int i = 0; i < G[v].size(); ++ i)
{
int to = G[v][i].first;
if(to == p)
continue;
A[v] = max(A[v], tot - A[to] + B[to] + par + G[v][i].second);
}
}
int main()
{
scanf("%d", &n);
for(int i = 1; i < n; ++ i)
{
int x, y, val;
scanf("%d %d %d", &x, &y, &val);
G[x].push_back({y, val});
G[y].push_back({x, val});
}
go(1);
printf("%d\n", A[1]);
}
Compilation message (stderr)
# | 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... |