LCA - Lowest Common Ancestor

no tags 

A tree is an undirected graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. - Wikipedia 

The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with N nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself). - Wikipedia

Your task in this problem is to find the LCA of any two given nodes v and w in a given tree T.

For example the LCA of nodes 9 and 12 in this tree is the node number 3.

Input

The first line of input will be the number of test cases. Each test case will start with a number N the number of nodes in the tree, 1 <= N <= 1,000. Nodes are numbered from 1 to N. The next N lines each one will start with a number M the number of child nodes of the Nth node, 0 <= M <= 999 followed by M numbers the child nodes of the Nth node. The next line will be a number Q the number of queries you have to answer for the given tree T, 1 <= Q <= 1000. The next Q lines each one will have two number v and w in which you have to find the LCA of v and w in T, 1 <= v, w <= 1,000.

Input will guarantee that there is only one root and no cycles.

Output

For each test case print Q + 1 lines, The first line will have “Case C:” without quotes where C is the case number starting with 1. The next Q lines should be the LCA of the given v and w respectively.

Example

Input:
1
7
3 2 3 4
0
3 5 6 7
0
0
0
0
2
5 7
2 7

Output:
Case 1:
3
1

hide comments
jpfr12: 2024-10-31 22:23:54

Spoj's online judge accepts the naive solution that runs in O(n) time for each query. However, this is a good exercise for practicing fast methods, such as Binary Lifting, Tarjan's offline LCA algorithm, and Euler Tour + RMQ.

dvelagap: 2024-04-06 05:29:20

Used HLD , getting TLE. logic to find LCA is jumping chains using parent[head] of the chain, until both heads are same.

luuphucvinh: 2023-09-13 06:28:48

use sqrt

baodepzai: 2023-09-13 05:18:07

easy

vuduoc: 2023-09-13 04:28:07

wow u re so good

metalavocado99: 2023-05-23 09:39:00

AC in one Go , I ued HLD , the best method , search ahegao face on youtube and self pleasure for the rest of the day

anujjadhav0215: 2023-04-18 16:12:29

It is giving wrong ans, for my accepted code on hackerrank

sky_0509: 2023-04-08 09:38:10

my code ac in cses and hear give wr and

prateek7248: 2022-10-09 19:30:55

<snip>
[Simes]: Read the footer - Don't post source code here

Last edit: 2022-10-09 22:16:31
ninhdn2006: 2022-10-03 11:44:42

I need help ! My code is time limit excced . Can you help me to fix it
<snip>
[Simes]: Read the footer - use the forum.

Last edit: 2022-10-03 18:34:56

Added by:hossamyosef
Date:2013-05-13
Time limit:0.600s-1.113s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:FCIS/ASU Local Contest 2013