# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
380432 | ruadhan | Traffic (IOI10_traffic) | C++14 | 0 ms | 0 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>
typedef long long ll;
using namespace std;
const ll INF = 2e12 + 2;
const int MAXN = 1e6 + 1;
ll congestion = 0;
vector<int> adj[MAXN];
vector<int> population;
vector<bool> visited;
void dfs(int node)
{
for (auto u : adj[node])
{
if (!visited[u])
{
congestion += population[u];
visited[u] = true;
dfs(u);
}
}
}
int LocateCentre(int N, vector<int> P, vector<int> S, vector<int> D)
{
ll ans = INF;
population = P;
for (int i = 0; i < N - 1; i++)
{
adj[S[i]].push_back(D[i]);
adj[D[i]].push_back(S[i]);
}
for (int i = 0; i < N; i++) // try city i
{
visited.assign(N, false);
visited[i] = true;
ll currentWorst = 0;
for (auto u : adj[i]) // all outgoing edges have different congestion
{
congestion = P[u];
visited[u] = true;
dfs(u);
currentWorst = max(currentWorst, congestion);
}
ans = min(ans, currentWorst);
}
return ans;
}
int main()
{
// int n = 5;
// vector<int> p = {10, 10, 10, 20, 20};
// vector<int> s = {0, 1, 2, 3}; // edges
// vector<int> d = {2, 2, 3, 4};
// cout << LocateCentre(n, p, s, d) << endl;
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> p(n), s(n - 1), d(n - 1);
for (auto &x : p)
cin >> x;
for (int i = 0; i < n - 1; i++)
cin >> s[i] >> d[i];
cout << LocateCentre(n, p, s, d) << endl;
return 0;
}