Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem Statement In a normal tree, the lowest common ancestor (or LCA for short) of two vertices u and v is defined as the lowest
Problem Statement In a normal tree, the lowest common ancestor (or LCA for short) of two vertices u and v is 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 " ruv " 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 N1 lines contains a pair of integers u and v representing an edge between these two vertices. The next line contains a single integer Q 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: 41223142142242 Output: 1 2 HTIP 1: start writing your answer from line 8 import sys \#this lines imports sys library input_from_question=input() \#Gets the Input def solution(input_from_question): \#\# WRITE YOUR ANSWER HERE \#\# \#TIP 2: use input() again to get input in different lines \#TIP 3: use input_from_question.split(' ') to get different values return print(solution(input_from_question)) \#printing the output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started