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 <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <string.h>
#include <iterator>
#include <list>
#include <cmath>
#include <math.h>
#include <unordered_map>
#include <numeric>
using namespace std;
typedef pair<int,int> pi;
vector<int> adj[1000001]; // change
int people[1000001]; // change
int sums[1000001]; // change
int subtree[1000001]; // change
void dfsSubtree(int node, int parent, int parentSum) {
// sums[node].insert(parentSum); // record the cumulative sum from root
subtree[node] += people[node];
for (auto u: adj[node]) {
if (u != parent) { // traverse to the next node, adding on the parentSum
// the subtree sum of the next node must be returned and added
dfsSubtree(u, node, parentSum + people[node]);
subtree[node] += subtree[u]; // subtree u is the sum for the child
// sums[node].insert(subtree[u]); // REPLACE
sums[node] = max(sums[node], subtree[u]);
}
}
}
void dfsParents(int node, int parent, int parentSum) {
// sums[node].insert(parentSum); // REPLACE
sums[node] = max(sums[node], parentSum);
for (auto u : adj[node]) {
if (u != parent) {
int res = subtree[0]-subtree[u];
// cout << "going from: "<< node << " to " << u << ", parentsum: " << res << endl;
// must send parentSum = entire tree sum - dest node subtree
dfsParents(u, node, res);
// subtree[node] += subtree[u]; // subtree u is the sum for the child
// sums[node].insert(subtree[u]);
}
}
}
int LocateCentre(int N,int P[],int S[], int D[]) {
fill(sums, sums+1000001, 0);
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 ++) {
people[i] = P[i];
}
dfsSubtree(0, -1, 0);
dfsParents(0, -1, 0);
int minRoad = 1E12;
int city = -1;
for (int i = 0; i < N; i++) {
if (sums[i] < minRoad) {
minRoad = sums[i];
city = i;
}
}
return city;
}
// int main() {
// int P[5] = {10,10,10,20,20};
// int S[4] = {1, 2, 2, 3};
// int D[4] = {2, 0, 3, 4};
// cout << LocateCentre(5, P, S, D);
// }
Compilation message (stderr)
traffic.cpp: In function 'int LocateCentre(int, int*, int*, int*)':
traffic.cpp:69:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+12' to '2147483647' [-Woverflow]
69 | int minRoad = 1E12;
| ^~~~
# | 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... |