#include <bits/stdc++.h>
#define ll int64_t
using namespace std;
vector<ll > sum;
vector<ll > ans;
vector<int> g[1000006];
ll dfs1(int u, int p, int pp[]) {
sum[u] += pp[u];
for(int v: g[u]) {
if(v != p) {
sum[u] += dfs1(v, u, pp);
}
}
return sum[u];
}
void dfs2(int u, int p, ll psum, int pp[]) {
ans[u] = 0;
ll csum = pp[u];
if(p != -1) {
ans[u] = max(ans[u], psum);
csum += psum;
}
for(int v: g[u]) {
if(v != p) {
csum += sum[v];
ans[u] = max(ans[u], sum[v]);
}
}
for(int v: g[u]) {
if(v != p) {
dfs2(v, u, csum - sum[v], pp);
}
}
}
int LocateCentre(int N, int pp[], int S[], int D[]) {
sum.resize(N);
ans.resize(N);
for(int i=0;i<N-1;i++) {
g[S[i]].pb(D[i]);
g[D[i]].pb(S[i]);
}
dfs1(0, -1, pp);
dfs2(0, -1, 0, pp);
int id = 0, mn = ans[0];
for(int i=1;i<N;i++) {
if(mn > ans[i]) {
mn = ans[i];
id = i;
}
}
return id;
}
Compilation message
traffic.cpp: In function 'int LocateCentre(int, int*, int*, int*)':
traffic.cpp:47:14: error: 'class std::vector<int>' has no member named 'pb'
47 | g[S[i]].pb(D[i]);
| ^~
traffic.cpp:48:14: error: 'class std::vector<int>' has no member named 'pb'
48 | g[D[i]].pb(S[i]);
| ^~