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 "deliveries.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 100000 + 10;
const int INF = 1e9;
int n;
int w[MAXN];
llong sz[MAXN], sum;
std::vector <std::pair <int,int>> g[MAXN];
void init(int N, std::vector<int> U, std::vector<int> V, std::vector<int> T, std::vector<int> W)
{
n = N;
for (int i = 0 ; i < n - 1 ; ++i)
{
g[U[i]].push_back({V[i], T[i]});
g[V[i]].push_back({U[i], T[i]});
}
for (int i = 0 ; i < n ; ++i)
{
w[i] = W[i];
sum += w[i];
}
w[0]++;
sum++;
}
void fixDFS(int node, int par)
{
sz[node] = w[node];
for (const auto &[u, d] : g[node])
{
if (u == par)
{
continue;
}
fixDFS(u, node);
sz[node] += sz[u];
}
}
int findDFS(int node, int par)
{
for (const auto &[u, d] : g[node])
{
if (u == par)
{
continue;
}
if (sz[u] > sum / 2)
{
return findDFS(u, node);
}
}
return node;
}
llong calcDFS(int node, int par, llong currD)
{
llong res = currD * w[node];
for (const auto &[u, d] : g[node])
{
if (u == par)
{
continue;
}
res += calcDFS(u, node, currD + d);
}
return res;
}
long long max_time(int S, int X)
{
sum -= w[S];
w[S] = X;
if (S == 0) w[S]++;
sum += w[S];
fixDFS(0, -1);
return 2 * calcDFS(findDFS(0, -1), -1, 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |