Store the tree as a directed graph whose edges go from a parent to a child. For each vertex it is enough to store the list of its children, so the graph contains exactly n - 1 edges.
Run a depth-first search from the root and for each vertex remember two moments of time:
— the entry time (when DFS visits the vertex for the first time).
— the exit time (when DFS finishes processing the vertex).
The time counter is incremented at every entry into a vertex and at every exit from it, so all labels are different.
Nesting lemma. While the depth-first search processes vertex , it visits all vertices of its subtree and only them. Therefore, for any descendant of vertex , the entry into happens after the entry into , and the exit from — before the exit from :
If neither of the vertices and is an ancestor of the other, then the segments [d[a]; f[a]] and [d[b]; f[b]] do not intersect: the depth-first search completely finishes processing one subtree before entering another. Thus, any two segments are either nested one inside the other or disjoint — like correctly placed brackets.
Criterion. Vertex is an ancestor of vertex if and only if the segment [d[b]; f[b]] lies inside the segment [d[a]; f[a]]. Since the inequality d[b] < f[b] always holds, it is enough to check two conditions:
By the problem statement a \ne b, so all inequalities are strict: the labels of different vertices are always different.
Complexity. The depth-first search is performed once in , each query is answered in . The total complexity is O(n + m).
Recursion depth. If the tree is a chain, the recursion depth of the function dfs reaches 10^5. Usually the stack is large enough; otherwise the traversal is rewritten with an explicit stack.
Example
The tree from the example with the labels d[v] / f[v]. Time starts from , the children of each vertex are visited in input order.
Let us draw the segments [d[v]; f[v]] on the time axis. The segment of each vertex lies inside the segment of its parent, and the segments of vertices from different subtrees do not intersect.
Let us process the queries from the example.
Query : d[4] = 3 > d[1] = 1, vertex is not an ancestor of vertex . Answer .
Query : d[1] = 1 < d[4] = 3 and f[4] = 4 < f[1] = 12. Answer .
Query : d[3] = 6 < d[6] = 9 and f[6] = 10 < f[3] = 11. Answer .
Query : d[2] = 2 < d[6] = 9, but f[6] = 10 > f[2] = 5 — the segments and do not intersect. Answer .
Query : d[6] = 9 > d[5] = 7, vertices and are siblings. Answer .
Algorithm Implementation
Store the tree as adjacency lists . The array marks visited vertices, the arrays and store the entry time and the exit time of each vertex.
vector<vector<int> > g;
vector<int> used, d, f;The function dfs performs a depth-first search from vertex . On entering the vertex, remember the entry time , then recursively visit all unvisited children, and on leaving remember the exit time . The global counter is incremented after each label.
void dfs(int v)
{
used[v] = 1;
d[v] = time++;
for (int to : g[v])
if (!used[to]) dfs(to);
f[v] = time++;
}The function is_Parent returns if vertex is an ancestor of vertex , that is, if the segment [d[b]; f[b]] is nested in the segment [d[a]; f[a]]. Two inequalities are enough for this:
int is_Parent(int a, int b)
{
return (d[a] < d[b]) && (f[b] < f[a]);
}The main part of the program. Read the number of vertices .
scanf("%d",&n);Allocate memory for the adjacency lists and the label arrays. Vertices are numbered from , so the size of the arrays is n + 1.
g.resize(n+1); used.resize(n+1);
d.resize(n+1); f.resize(n+1);Read the parents of the vertices. If the parent of vertex is a \ne 0, add the directed edge a \to i. The vertex with a zero parent is the root of the tree; its number is stored in the variable .
for(i = 1; i <= n; i++)
{
scanf("%d",&a);
if(!a) root = i; else g[a].push_back(i);
}Run the depth-first search from the root .
dfs(root);Read the queries and answer each of them in .
scanf("%d",&m);
for(i = 0; i < m; i++)
{
scanf("%d %d",&a,&b);
printf("%d\n",is_Parent(a,b));
}