#include <iostream>
#include <vector>
#include <map>
using namespace std;
int ans;
map<int,int> dfs(int a,vector<vector<int>>& g,vector<int>& reg,vector<map<int,int>>& ans){
for(int u:g[a]){
map<int,int> x=dfs(u,g,reg,ans);
if(x.size()>ans[a].size())swap(x,ans[a]);
for(auto y:x)ans[a][y.first]+=y.second;
ans[a][reg[u]]++;
}
return ans[a];
}
int main(){
int n,r,q,x,y;
cin>>n>>r>>q;
vector<int> reg(n);
vector<vector<int>> g(n,vector<int>());
vector<map<int,int>> ans(n,map<int,int>());
vector<vector<int>> sol(r+1,vector<int>(r+1));
cin>>reg[0];
for(int i=1;i<n;i++){
cin>>x>>reg[i];
x--;
g[x].push_back(i);
}
dfs(0,g,reg,ans);
for(int i=0;i<n;i++){
for(auto mp:ans[i]){
//cout<<mp.first<<' ';
sol[reg[i]][mp.first]+=mp.second;
}
}
while(q--){
cin>>x>>y;
cout<<sol[x][y]<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
4 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
720 KB |
Output is correct |
6 |
Correct |
34 ms |
7248 KB |
Output is correct |
7 |
Correct |
34 ms |
1744 KB |
Output is correct |
8 |
Correct |
35 ms |
4008 KB |
Output is correct |
9 |
Correct |
161 ms |
68380 KB |
Output is correct |
10 |
Correct |
83 ms |
12260 KB |
Output is correct |
11 |
Correct |
174 ms |
33616 KB |
Output is correct |
12 |
Runtime error |
137 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Correct |
176 ms |
8776 KB |
Output is correct |
14 |
Correct |
229 ms |
26640 KB |
Output is correct |
15 |
Runtime error |
140 ms |
131072 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
163 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Correct |
985 ms |
114116 KB |
Output is correct |
3 |
Runtime error |
176 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Runtime error |
113 ms |
131072 KB |
Execution killed with signal 9 |
5 |
Runtime error |
86 ms |
131072 KB |
Execution killed with signal 9 |
6 |
Runtime error |
56 ms |
131072 KB |
Execution killed with signal 9 |
7 |
Runtime error |
58 ms |
131072 KB |
Execution killed with signal 9 |
8 |
Runtime error |
61 ms |
131072 KB |
Execution killed with signal 9 |
9 |
Runtime error |
64 ms |
131072 KB |
Execution killed with signal 9 |
10 |
Runtime error |
53 ms |
131072 KB |
Execution killed with signal 9 |
11 |
Runtime error |
55 ms |
131072 KB |
Execution killed with signal 9 |
12 |
Runtime error |
54 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Runtime error |
56 ms |
131072 KB |
Execution killed with signal 9 |
14 |
Runtime error |
64 ms |
131072 KB |
Execution killed with signal 9 |
15 |
Runtime error |
58 ms |
131072 KB |
Execution killed with signal 9 |
16 |
Runtime error |
51 ms |
131072 KB |
Execution killed with signal 9 |
17 |
Runtime error |
56 ms |
131072 KB |
Execution killed with signal 9 |