| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 672773 | Hanksburger | Beads and wires (APIO14_beads) | C++17 | 12 ms | 23764 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
using namespace std;
ll f[200005][2], mx[200005], mx2[200005], ans;
vector<ll> g[200005][2], h[200005][2];
vector<pair<ll, ll> > child[200005];
pair<ll, ll> par[200005];
void dfs(ll u)
{
// cout << "dfs " << u << '\n';
mx[u]=mx2[u]=-1e18;
for (ll i=0; i<child[u].size(); i++)
{
ll v=child[u][i].first, w=child[u][i].second;
dfs(v);
f[u][0]+=max(f[v][0], f[v][1]+w);
ll z=f[v][0]+w-max(f[v][0], f[v][1]+w);
if (mx[u]<=z)
{
mx2[u]=mx[u];
mx[u]=z;
}
else if (mx2[u]<z)
mx2[u]=z;
}
f[u][1]=f[u][0]+mx[u];
// cout << "f[" << u << "][0] " << f[u][0] << '\n';
// cout << "f[" << u << "][1] " << f[u][1] << '\n';
for (ll i=0; i<child[u].size(); i++)
{
ll v=child[u][i].first, w=child[u][i].second;
g[u][0].push_back(f[u][0]-max(f[v][0], f[v][1]+w));
if (mx[u]==f[v][0]+w-max(f[v][0], f[v][1]+w))
g[u][1].push_back(g[u][0][i]+mx2[u]);
else
g[u][1].push_back(g[u][0][i]+mx[u]);
}
}
void dfs2(ll u)
{
// cout << "dfs2 " << u << '\n';
ll p=par[u].first, j=par[u].second;
ll x=(u==1?0:child[p][j].second);
for (ll i=0; i<child[u].size(); i++)
{
ll v=child[u][i].first, w=child[u][i].second;
if (u==1)
{
h[u][0].push_back(g[u][0][i]);
h[u][1].push_back(g[u][1][i]);
}
else
{
h[u][0].push_back(g[u][0][i]+max(h[p][0][j], h[p][1][j]+x));
h[u][1].push_back(max(g[u][0][i]+h[p][0][j]+x, g[u][1][i]+max(h[p][0][j], h[p][1][j]+x)));
}
ans=max(ans, f[v][0]+max(h[u][0][i], h[u][1][i]+w));
dfs2(v);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n;
cin >> n;
for (ll i=1; i<n; i++)
{
ll u, v, w;
cin >> u >> v >> w;
par[v]={u, child[u].size()};
child[u].push_back({v, w});
}
dfs(1);
dfs2(1);
cout << max(ans, f[1][0]);
}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... | ||||
