Problem Statement
In a normal tree, the lowest common ancestor (or LCA for short) of two vertices u and vis defined as the lowest vertex that is ancestor of both the vertices.
Given a tree of N vertices, you need to answer in the form "suv" which means if the root of the tree is at r then what is LCA of u and v.
Input:
The first line contains a single integer N. Each line in the next N - 1 lines contains a pair of integers u and v representing an edge between these two vertices.
The next line contains a single integer which is the number of the queries. Each line in the next Q lines contains three integers r, u, v representing a query.
Output:
For each query, write out the answer on a single line.
Example:
Input:
4
12
23
14
2
142
242
i
Type here to search
Chp
Answers
Answer:
The Cambr¨ıdge Monographs on Applied and Computational Mathematics reflects the
crucial role of mathematical and computational techniques in contemporary science. The
series publishes expositions on all aspects of applicable and numerical mathematics, with
an emphasis on new developments in this fast-moving area of research.
State-of-the-art methods and algorithms as well as modern mathematical descriptions
of physical and mechanical ideas are presented in a manner suited to graduate research
students and professionals alike. Sound pedagogical presentation is a prerequisite. It is
intended that books in the series will serve to inform a new generation of researchers.
Also in this series:
1. A Practical Guide to Pseudospectral Methods, Bengt Fornberg
2. Dynamical Systems and Numerical Analysis, A. M. Stuart and A. R. Humphries
3. Level Set Methods and Fast Marching Methods, J. A. Sethian
4. The Numerical Solution of Integral Equations of the Second Kind, Kendall E.
Atkinson
5. Orthogonal Rational Functions, Adhemar Bultheel, Pablo Gonzalez-Vera, Erik ´
Hendiksen, and Olav Njastad ˚
6. The Theory of Composites, Graeme W. Milton
7. Geometry and Topology for Mesh Generation, Herbert Edelsbrunner
8. Schwarz–Christoffel Mapping, Tofin A. Driscoll and Lloyd N. Trefethen
9. High-Order Methods for Incompressible Fluid Flow, M. O. Deville, P. F. Fischer,
and E. H. Mund
10. Practical Extrapolation Methods, Avram Sidi
11. Generalized Riemann Problems in Computational Fluid Dynamics, Matania
Ben-Artzi and Joseph Falcovitz
12. Radial Basis Functions: Theory and Implementations, Martin D. Buhmann
13. Iterative Krylov Methods for Large Linear Systems, Henk A. van der Vorst
14. Simulating Hamiltonian Dynamics, Ben Leimkuhler and Sebastian Reich
15. Collocation Methods for Volterra Integral and Related Functional Equations,