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 <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <string>
#include <fstream>
#include <list>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <iomanip>
#include <traffic.h>
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define srt(a) sort(all(a));
#define srtc(a,comp) sort(all(a),comp);
#define srtb(a) sort(a.rbegin(),a.rend());
#define boost ios::sync_with_stdio(false); cin.tie(0); cin.tie(0);
using ll = long long;
using namespace std;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef pair<int,int> ii;
typedef vector<ii> vpi;
const int dx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}}; //right left down up
void setIO(string name = "") { // name is nonempty for USACO file I/O
    ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
    // alternatively, cin.tie(0)->sync_with_stdio(0);
    freopen((name+".in").c_str(), "r", stdin); // see Input & Output
    freopen((name+".out").c_str(), "w", stdout);
}
vi adj[1000005];
ll a[1000005];
map<ii,ll> weights;
vi popu;
ll ans = 1e9;
ll k;
ll dfs1(int node, int parent){
    if (parent != node && adj[node].size() == 1){
        return popu[node];
    }
    ll w = 0;
    for (auto x: adj[node]){
        if (x != parent){
            weights[{node,x}] = dfs1(x,node);
            //cout << node << " " << x << " " << weights[{node,x}] << endl;
            w += weights[{node,x}];
        }
    }
    w+=popu[node];
    //cout << node << " " << w << endl;
    //cout << endl;
    return w;
}
void dfs2(int node, int parent){
    ll curr = 0;
    for (auto x: adj[node]){
        if (x != parent) curr = max(curr,weights[{node,x}]);
    }
    if (node == parent){
        //cout << node << " " << curr << endl;
        if (ans < curr){
            ans = curr;
            k = node;
        }
        a[node] = curr;
        for (auto x: adj[node]){
            if (x != parent) dfs2(x,node);
        }
        return;
    }
    ll newweight = popu[parent];
    for (auto x: adj[parent]){
        if (x != node){
            newweight+=weights[{parent,x}];
        }
    }
    curr = max(curr,newweight);
    weights[{node,parent}] = newweight;
    a[node] = curr;
    for (auto x: adj[node]){
        if (x != parent) dfs2(x,node);
    }
}
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]);
        popu.pb(P[i]);
    }
    popu.pb(P[N-1]);
    dfs1(0,0);
    dfs2(0,0);
    ans = 1e9;
    for (int i = 0; i <N; i++){
        //cout << a[i] << " ";
        if (a[i] < ans){
            ans = a[i];
            k = i;
        }
    }
   // cout << k << " " << ans << endl;
    return k;
}
Compilation message (stderr)
traffic.cpp: In function 'void setIO(std::string)':
traffic.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     freopen((name+".in").c_str(), "r", stdin); // see Input & Output
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
traffic.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen((name+".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |