Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations | C # program for the above approach ; 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 ; Driver code ; Given number of nodes ; Given Directed Tree
using System ; using System . Collections . Generic ; public class GFG { public static void findNode ( Dictionary < int , int > map , int n ) { int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( i + 1 ) ) a [ i ] = map [ i + 1 ] ; else a [ i ] = 0 ; } int count0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count0 ++ ; } } count0 -= 1 ; if ( count0 <= Math . Floor ( ( ( double ) n ) / ( ( double ) 2 ) ) ) { Console . WriteLine ( " Yes " ) ; } else Console . WriteLine ( " No ▁ " ) ; } static public void Main ( ) { int N = 3 ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; map [ 1 ] = 0 ; map [ 2 ] = 2 ; map [ 3 ] = 0 ; findNode ( map , N ) ; } }