제출 #527928

#제출 시각아이디문제언어결과실행 시간메모리
527928MohamedAliSaidaneRegions (IOI09_regions)C++14
30 / 100
1140 ms131076 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pld> vpd;

#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(v) (v).begin(),(v).end()
#define ff first
#define ss second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
int nx[4] = {1,-1,0,0}, ny[4] = {0,0,1,-1};
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a, ll b){return (a / gcd(a, b)) * b;}


const int MAX_R = 504;
const int MAX_N =2e5 + 4;
ll sup[MAX_R][MAX_R];
int reg[MAX_N];
vi adj[MAX_N];
int n, r, q;
int p[MAX_N];
vi last;
void dfs(int x)
{
    vi cur;
    cur.assign(r,0);
    for(auto e: adj[x])
    {
        dfs(e);
        for(int h = 1; h <= r;h ++)
        {
            cur[h-1] += last[h-1] + (reg[e] == h);
        }
    }
    for(int h = 1; h <= r;h ++)
        sup[reg[x]][h] += cur[h-1];
    swap(cur,last);
    return;
}

void solve()
{
    cin >> n >> r >> q;
    cin >> reg[1];
    map<int,vi> m;
    m[reg[1]].pb(1);
    for(int i = 2; i <= n; i ++)
    {
        cin >> p[i] >> reg[i];
        adj[p[i]].pb(i);
        m[reg[i]].pb(i);
    }
    dfs(1);
    while(q--)
    {
        int u, v;
        cin >> u >> v;
        cout << sup[u][v] << endl;
    }

}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int tt = 1;
    while(tt--)
        solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...