Submission #1323363

#TimeUsernameProblemLanguageResultExecution timeMemory
1323363hectormedranoRegions (IOI09_regions)C++20
30 / 100
435 ms196608 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<vector<ll>> v, ans;
vector<ll> H, t, ch;
vector<map<ll, ll>> m;

void DFS1(ll x, ll y){
    ll mx = 0;
    for(ll z : v[y]){
        if(z==x){continue;}
        DFS1(y, z);
        t[y] += t[z];
        if(mx < t[z]){
            ch[y] = z;
            mx = t[z];
        }
    }
}

void DFS2(ll x, ll y){
    for(ll z : v[y]){
        if(z==x){continue;}
        DFS2(y, z);
    }
    swap(m[y], m[ch[y]]);
    for(ll z : v[y]){
        if(z==x){continue;}
        m[y][H[z]]++;
        if(z==ch[y]){continue;}
        for(pair<ll, ll> p : m[z]){
            m[y][p.first] += p.second;
        }
    }
    for(pair<ll, ll> p : m[y]){
        ans[H[y]][p.first] += p.second;
    }
}

int main() {
	cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    ll N, R, Q, s, r1, r2;
    cin>>N>>R>>Q;
    ans.resize(R, vector<ll>(R, 0));
    v.resize(N);
    H.resize(N);
    t.resize(N, 1);
    ch.resize(N);
    m.resize(N);
    cin>>H[0];
    H[0]--;
    for(ll i=1;i<N;i++){
        cin>>s>>H[i]; 
        s--; H[i]--;
        v[s].push_back(i);
    }
    DFS1(-1, 0);
    DFS2(-1, 0);
    while(Q--){
        cin>>r1>>r2;
        r1--; r2--;
        cout<<ans[r1][r2]<<endl;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...