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 <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
#include <queue>
#include <map>
#include <iomanip>
using namespace std;
vector<int>region;
int total;
void dfs(int nodo,vector<vector<int> >&g,vector<bool>&vi,int b,int x,int a){
vi[nodo]=true;
if(region[nodo]==b)total+=x;
if(region[nodo]==a)x++;
for(int c:g[nodo]){
if(!vi[c]){
dfs(c,g,vi,b,x,a);
}
}
}
int main(){
int N,R,Q;
cin>>N>>R>>Q;
vector<vector<int> >g(N);
g.clear();
region.resize(N);
int a,b;
cin>>a;
a--;
region[0]=a;
for(int i=1;i<N;i++){
cin>>a>>b;
a--;
b--;
region[i]=b;
g[a].push_back(i);
}
for(int i=0;i<Q;i++){
cin>>a>>b;
a--;
b--;
total=0;
vector<bool>vi(N,false);
dfs(0,g,vi,b,0,a);
cout<<total<<"\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |