Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U | To store the time ; Function to perform DFS to store times , distance and parent of each node ; Update the distance of node u ; Update parent of node u ; Increment time timeT ; Discovery time of node u ; Traverse the adjacency list of current node and recursively call DFS for each vertex ; If current node Adj [ u ] [ i ] is unvisited ; Update the finishing time ; Function to add edges between nodes u and v ; Function to find the node U such that path from root to U contains nodes in V [ ] ; Initialise vis , dis , parent , preTime , and postTime ; Store Adjacency List ; Create adjacency List ; Perform DFS Traversal ; Stores the distance of deepest vertex 'u ; Update the deepest node by traversing the qu [ ] ; Find deepest vertex ; Replace each vertex with it 's  corresponding parent except  the root vertex ; Checks if the ancestor with respect to deepest vertex u ; Update ans ; Prthe result ; Driver Code ; Total vertices ; Given set of vertices ; Given edges ; Function Call
timeT = 0 ; NEW_LINE def dfs ( u , p , dis , vis , distance , parent , preTime , postTime , Adj ) : NEW_LINE INDENT global timeT NEW_LINE distance [ u ] = dis ; NEW_LINE parent [ u ] = p ; NEW_LINE vis [ u ] = 1 ; NEW_LINE timeT += 1 NEW_LINE preTime [ u ] = timeT ; NEW_LINE for i in range ( len ( Adj [ u ] ) ) : NEW_LINE INDENT if ( vis [ Adj [ u ] [ i ] ] == 0 ) : NEW_LINE INDENT dfs ( Adj [ u ] [ i ] , u , dis + 1 , vis , distance , parent , preTime , postTime , Adj ) ; NEW_LINE DEDENT DEDENT timeT += 1 NEW_LINE postTime [ u ] = timeT ; NEW_LINE DEDENT def addEdge ( Adj , u , v ) : NEW_LINE INDENT Adj [ u ] . append ( v ) ; NEW_LINE Adj [ v ] . append ( u ) ; NEW_LINE DEDENT def findNodeU ( N , V , Vertices , Edges ) : NEW_LINE INDENT vis = [ 0 for i in range ( N + 1 ) ] NEW_LINE distance = [ 0 for i in range ( N + 1 ) ] NEW_LINE parent = [ 0 for i in range ( N + 1 ) ] NEW_LINE preTime = [ 0 for i in range ( N + 1 ) ] NEW_LINE postTime = [ 0 for i in range ( N + 1 ) ] NEW_LINE Adj = [ [ ] for i in range ( N + 1 ) ] NEW_LINE u = 0 NEW_LINE v = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT addEdge ( Adj , Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; NEW_LINE DEDENT dfs ( 1 , 0 , 0 , vis , distance , parent , preTime , postTime , Adj ) ; NEW_LINE maximumDistance = 0 ; NEW_LINE DEDENT ' NEW_LINE INDENT maximumDistance = 0 ; NEW_LINE for k in range ( V ) : NEW_LINE INDENT if ( maximumDistance < distance [ Vertices [ k ] ] ) : NEW_LINE INDENT maximumDistance = distance [ Vertices [ k ] ] ; NEW_LINE u = Vertices [ k ] ; NEW_LINE DEDENT if ( parent [ Vertices [ k ] ] != 0 ) : NEW_LINE INDENT Vertices [ k ] = parent [ Vertices [ k ] ] ; NEW_LINE DEDENT DEDENT ans = True ; NEW_LINE flag = False NEW_LINE for k in range ( V ) : NEW_LINE INDENT if ( preTime [ Vertices [ k ] ] <= preTime [ u ] and postTime [ Vertices [ k ] ] >= postTime [ u ] ) : NEW_LINE INDENT flag = True ; NEW_LINE DEDENT else : NEW_LINE INDENT flag = False ; NEW_LINE DEDENT ans = ans & flag ; NEW_LINE DEDENT if ( ans ) : NEW_LINE INDENT print ( u ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 ; NEW_LINE V = 5 ; NEW_LINE Vertices = [ 4 , 3 , 8 , 9 , 10 ] ; NEW_LINE Edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 2 , 5 ] , [ 2 , 6 ] , [ 3 , 7 ] , [ 7 , 8 ] , [ 7 , 9 ] , [ 9 , 10 ] ] ; NEW_LINE findNodeU ( N , V , Vertices , Edges ) ; NEW_LINE DEDENT