Submission #376924

#TimeUsernameProblemLanguageResultExecution timeMemory
37692454skyxenonTraffic (IOI10_traffic)C++11
0 / 100
17 ms23788 KiB
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

#define ll long long

const int maxN = 1e6;

vector<int> adj[maxN];
int people[maxN];
int children[maxN];

int total;

// use DP on trees to calculate the max congestion starting from src => people[src]
// count number of children in postorder DFS
void DFS(int P[], int src, int par) {
    for (int x : adj[src]) {
        if (x != par) {
            DFS(P, x, src);
            children[src] += children[x];
            people[src] = max(people[src], children[x]);
        }
    }

    children[src] += P[src];
    people[src] = max(people[src], total - children[src]);
}

int LocateCentre(int N, int P[], int S[], int D[]) {
    ll minCongestion = 2e9;
    int ans = -1;

    total = accumulate(P, P + N, 0);

    for (int i = 0; i < N - 1; i++) {
       adj[S[i]].push_back(D[i]);
       adj[D[i]].push_back(S[i]);
    }

    DFS(P, 0, -1);

    for (int i = 0; i < N; i++) {
        if (people[i] < minCongestion) {
            minCongestion = people[i];
            ans = i;
        }
    }

    cout << "people: ";
    for (int i = 0; i < N; i++) {
        cout << people[i] << " ";
    }
    cout << endl;

    cout << "children: ";
    for (int i = 0; i < N; i++) {
        cout << children[i] << " ";
    }
    cout << endl;

    return ans;
}

// int main() {
//     int a1[5] = {10, 10, 10, 20, 20};
//     int a2[4] = {0, 1, 2, 3};
//     int a3[4] = {2, 2, 3, 4};
//     cout << LocateCentre(5, a1, a2, a3) << endl;
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...