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>
using namespace std;
using edges_t = vector<vector<int>>;
void city_sum(int from, int to, int index,
edges_t &edges, edges_t &sums, vector<int> &weights)
{
int sum = 0;
for (int i = 0; i < (int)edges[to].size(); i++)
{
int &next = edges[to][i];
if (next == from)
{
continue;
}
if (sums[to][i] == -1)
{
city_sum(to, next, i, edges, sums, weights);
}
sum += sums[to][i];
}
sums[from][index] = sum + weights[to];
}
int max_load(int city, edges_t &edges, edges_t &sums, vector<int> &weights)
{
int res = 0;
for (int i = 0; i < (int)edges[city].size(); i++)
{
city_sum(city, edges[city][i], i, edges, sums, weights);
res = max(res, sums[city][i]);
}
return res;
}
int LocateCentre(int N, int P[], int S[], int D[])
{
edges_t edges(N);
vector<int> weights(N);
for (int i = 0; i < N; i++)
{
weights[i] = P[i];
}
for (int i = 0; i < N - 1; i++)
{
int a, b;
a = S[i];
b = D[i];
edges[a].push_back(b);
edges[b].push_back(a);
}
edges_t sums(N);
for (int i = 0; i < (int)sums.size(); i++)
{
sums[i] = vector<int>(edges[i].size(), -1);
}
int m_load = numeric_limits<int>::max();
int vertex = -1;
for (int i = 0; i < N; i++)
{
int load = max_load(i, edges, sums, weights);
if (load < m_load)
{
m_load = load;
vertex = i;
}
}
return vertex;
}
#ifdef _AAAAAAAAA
static int N, P[1000000], S[1000000], D[1000000];
int main()
{
freopen("eismas.in", "r", stdin);
int i;
scanf("%d", &N);
for (i = 0; i < N; i++)
scanf("%d", &P[i]);
for (i = 0; i < N - 1; i++)
scanf("%d%d", &S[i], &D[i]);
int r = LocateCentre(N, P, S, D);
printf("%d\n", r);
return 0;
}
#endif
# | 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... |