이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
vector<int> adj[200100];
int region[200100];
int answers[501][501];
vector<int> regions[25100];
map<int, int> vals[200100];
void merge(map<int, int> &a, map<int, int> b){
if(a.size()<b.size())
swap(a, b);
for(auto i: b)
a[i.first]+=i.second;
}
void dfs(int n){
regions[region[n]].push_back(n);
vals[n][region[n]]=1;
for(auto i: adj[n]){
dfs(i);
merge(vals[n], vals[i]);
}
}
int main(){
int n, r, q;
cin >> n >> r >> q;
cin >> region[0];
for(int i = 1; i < n; i++){
int p;
cin >> p >> region[i];
adj[p-1].push_back(i);
}
dfs(0);
if(r<501){
for(int i = 1; i <= r; i++){
for(auto j : regions[i]){
for(auto k: vals[j]){
answers[i][k.first]+=k.second;
}
}
}
while(q--){
int a, b;
cin >> a >> b;
cout << answers[a][b] << endl;
}
} else {
while(q--){
int a, b, ans = 0;
cin >> a >> b;
for(auto i: regions[a])
if(vals[i].count(b))
ans+=vals[i][b];
cout << ans << endl;
}
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |