이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int _ = 2E5 + 500;
int r[_], N, R, Q, vis[_];
vector<int> child[_], officer[_];
void dfs(int u){
vis[u] = 1;
for(auto & v : child[u])
dfs(v);
}
i64 work(int x, int y){
i64 ans = 0;
for(auto person : officer[x]){
dfs(person);
for(int i = 1; i <= N; i++){
if(vis[i] && r[i] == y)
ans++;
vis[i] = 0;
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> N >> R >> Q;
cin >> r[1];
officer[r[1]].push_back(1);
for(int i = 2; i <= N; i++){
int p; cin >> p >> r[i];
child[p].push_back(i);
officer[r[i]].push_back(i);
}
while(Q--){
int r1, r2; cin >> r1 >> r2;
i64 ans = work(r1, r2);
cout << ans << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |