forked from pankaj443/CODEFORCES
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path111.cpp
More file actions
43 lines (36 loc) · 732 Bytes
/
111.cpp
File metadata and controls
43 lines (36 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
vector <int> adj[200005];
bool vis[200005];
int cnt[200005], k=0;
map<int, int> mp, mp1;
void dfs(int s) {
vis[s] = 1;
mp1[s] = k;
mp[k] = s;
k++;
for(int i = 0;i < adj[s].size(); i++) {
if(!vis[adj[s][i]])
dfs(adj[s][i]);
cnt[s] += cnt[adj[s][i]];
}
}
int main()
{
int m, n, i, a, b, d, in, q;
cin >> n >>q;
cnt[1] = 1;
for(i = 2; i <= n; i++)
{
cin >>a;
cnt[i] = 1;
adj[a].push_back(i);
}
dfs(1);
while(q--)
{
cin >>a>>b;
cout<<cnt[a]<<endl;
}
return 0;
}