← All problemsSign in

ANCESTRAL QUERIES

You a given a tree consisting of N nodes, and a set of Q queries. Each query consists of a pair of nodes (u, v). For each query, find the number of nodes r, such that when you root the tree at r, either u is an ancestor of v, or v is an ancestor of u. Input - The first line consists of two integers:N and Q. - The next N-1 lines consist of 2 integers each: u and v, each describing an edge. - Th

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start