Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations |  ; Store the indegree of every node ; Store the nodes having indegree equal to 0 ; Traverse the array ; If the indegree of i - th node is 0 ; Increment count0 by 1 ; If the number of operations needed is at most floor ( n / 2 ) ; Otherwise ; Driver Code ; Given number of nodes ; Given Directed Tree
/ * Function to check if there is a NEW_LINE INDENT node in tree from where all other NEW_LINE nodes are accessible or not * / NEW_LINE DEDENT def findNode ( mp , n ) : NEW_LINE INDENT a = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = mp [ i + 1 ] NEW_LINE DEDENT count0 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT DEDENT count0 -= 1 NEW_LINE if ( count0 <= ( n ) / ( 2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE mp = { } NEW_LINE mp [ 1 ] = 0 NEW_LINE mp [ 2 ] = 2 NEW_LINE mp [ 3 ] = 0 NEW_LINE findNode ( mp , N ) NEW_LINE DEDENT