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<bits/stdc++.h>
using namespace std;
int n,r,q,ly[200005],h[200005],dp[2505][2505];
vector<int>v[200005],c[200005];
void dfs(int x)
{
for(auto i:v[x])
{
if(ly[x]>ly[i])continue;
dfs(i);
if(h[x]!=h[i])dp[h[x]][h[i]]++;
for(int y=1;y<=r;y++)
{
if(h[i]==y||h[x]==y)continue;
dp[h[x]][y]+=dp[h[i]][y];
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>r>>q;
int hm,sp;
cin>>hm;
h[1]=hm;
c[hm].push_back(1);
for(int x=2;x<=n;x++)
{
cin>>sp>>hm;
v[sp].push_back(x);
v[x].push_back(sp);
h[x]=hm;
ly[x]=ly[sp]+1;
c[hm].push_back(x);
}
dfs(1);
while(q--)
{
int l,r;
cin>>l>>r;
cout<<dp[l][r]<<'\n'<<flush;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |