이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
const int maxn = 200001;
vector<int> adj[maxn];
int home[maxn];
map<int, int> mp[maxn];
map<pair<int, int>, int> ans;
void dfs(int s, int p){
mp[s][home[s]] = 1;
for(auto u: adj[s]){
if(u == p) continue;
dfs(u, s);
if(sz(mp[s]) < sz(mp[u])) swap(mp[u], mp[s]);
for(auto &[r, m]: mp[u]) mp[s][r] += m;
}
for(auto &[r, m]: mp[s]){
if(r == home[s]){
ans[{r, r}] += m-1;
}
else{
ans[{home[s], r}] += m;
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int N, R, Q; cin >> N >> R >> Q;
int h; cin >> h; home[1] = h;
for(int i=2; i<=N; i++){
int p, g; cin >> p >> g;
adj[i].pb(p); adj[p].pb(i);
home[i] = g;
}
dfs(1, 0);
while(Q--){
int r1, r2; cin >> r1 >> r2;
cout << ans[{r1, r2}] << "\n";
cout.flush();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |