Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations | Function to check if there is a node in tree from where all other nodes are accessible or not ; 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 ; Given number of nodes ; Given Directed Tree
function findNode ( mp , n ) { var a = new Array ( n ) ; var i ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = mp [ i + 1 ] ; } var count0 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count0 ++ ; } } count0 -= 1 ; if ( count0 <= parseInt ( n / 2 ) ) { document . write ( " " ) ; } else document . write ( " " ) ; } var N = 3 ; var mp = new Map ( ) ; mp . set ( 1 , 0 ) ; mp . set ( 2 , 2 ) ; mp . set ( 3 , 0 ) ; mp [ 1 ] = 0 ; mp [ 2 ] = 2 ; mp [ 3 ] = 0 ; findNode ( mp , N ) ;