forked from MountAim/Graph-Coding-Minutes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
63-Count Descendants.cpp
47 lines (39 loc) · 1.06 KB
/
63-Count Descendants.cpp
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
44
45
46
47
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>g,go;
vector<int>tin,tout,dep;
int timer;
void dfs(int node,int par)
{
tin[node] = timer++;
if(par == -1) dep[node] = 0;
else dep[node] = dep[par] + 1;
go[dep[node]].push_back(tin[node]);
for(auto x : g[node])
if(x != par) dfs(x,node);
tout[node] = timer - 1;
}
int cnt(int x,int l,int r)
{
return upper_bound(go[x].begin(),go[x].end(),r) - upper_bound(go[x].begin(),go[x].end(),l-1);
}
vector<int> countDescendants (int n, vector<vector<int>> edges, vector<vector<int>>queries)
{
timer=0;
g = go = vector<vector<int>>(n+1);
tin = tout = dep =vector<int>(n+1);
int x,y;
for (int i =0; i<edges.size() ; i++) {
x=edges[i][0],y=edges[i][1];
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,-1);
vector<int>ans;
for(int i=0;i<queries.size();i++)
{
x=queries[i][0],y=queries[i][1];
ans.push_back(cnt(y,tin[x],tout[x]));
}
return ans;
}