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>
#include "traffic.h"
#define ll long long
#define pii pair<int, int>
#define st first
#define nd second
#define pb push_back
using namespace std;
const int maxN = 1e6 + 10;
vector<int> adj[maxN];
int prt[maxN], dis[maxN];
int dfs(int i, int p, int P[]) {
for (auto j : adj[i]) {
if (j == p) continue;
dis[i] += dfs(j, i, P);
prt[j] = i;
}
return dis[i] + P[i];
}
int LocateCentre(int N, int P[], int S[], int D[]) {
for (int i = 0; i < N - 1; ++i) {
adj[S[i]].pb(D[i]);
adj[D[i]].pb(S[i]);
}
dfs(0, -1, P);
int ans = 2e9, c;
for (int i = 0; i < N; ++i) {
int tmp = 0;
for (auto j : adj[i]) {
if (j == prt[i]) tmp = max(tmp, dis[0] - dis[i]);
else tmp = max(tmp, dis[j]);
}
if (tmp < ans) c = i, ans = tmp;
}
// for (int i = 0; i < N; ++i) cout << prt[i] << " " << dis[i] << '\n';
return c;
}
//int main() {
// int n; cin >> n;
// int p[n], s[n], d[n];
// for (int i = 0; i < n; ++i) cin >> p[i];
// for (int i = 0; i < n - 1; ++i) cin >> s[i];
// for (int i = 0; i < n - 1; ++i) cin >> d[i];
// cout << LocateCentre(n, p, s, d) << '\n';
//}
Compilation message (stderr)
traffic.cpp: In function 'int LocateCentre(int, int*, int*, int*)':
traffic.cpp:45:12: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
45 | return c;
| ^
# | 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... |