Given a Directed Tree consisting of N nodes, the task is to check if there exists a node in the given tree such that all other nodes are reachable by removing any directed edge from the Tree and adding another directed edge between any pair of nodes in the Tree at most floor(N/2) times. If there exists any such node, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 3

Output: Yes
Explanation:
Remove the edge 2 -> 3 and insert an edge 1 -> 3.

Therefore, both the remaining nodes (2, 3) are now accessible from the node 1.
Count of operations required is 1, which is <= floor(3/2) (= 1).
Input: N = 5

Output: No
Approach: The idea to solve this problem is based on the following observations:
- Every node should have at least one parent node, i.e. every node should have at least 1 indegree to make the tree accessible from the required node.
- It can be concluded that if every node has at least 1 indegree, then all other nodes can be accessed.
- Therefore, the task is reduced to finding the number of nodes having 0 in-degrees and checking if it is at most N / 2 or not.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void findNode(map< int , int > mp, int n)
{
int a[n];
for ( int i = 0; i < n; i++)
{
a[i] = mp[i + 1];
}
int count0 = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] == 0)
{
count0++;
}
}
count0 -= 1;
if (count0 <= floor ((( double )n) /
(( double )2)))
{
cout << "Yes" ;
}
else
cout << "No" ;
}
int main()
{
int N = 3;
map< int , int > mp;
mp[1] = 0;
mp[2] = 2;
mp[3] = 0;
findNode(mp, N);
}
|
Java
import java.io.*;
import java.util.HashMap;
class GFG {
public static void
findNode(HashMap<Integer, Integer> map,
int n)
{
int [] a = new int [n];
for ( int i = 0 ; i < n; i++) {
a[i] = map.getOrDefault(i + 1 , 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 ))) {
System.out.println( "Yes" );
}
else
System.out.println( "No " );
}
public static void main(String[] args)
{
int N = 3 ;
HashMap<Integer, Integer> map
= new HashMap<>();
map.put( 1 , 0 );
map.put( 2 , 2 );
map.put( 3 , 0 );
findNode(map, N);
}
}
|
Python3
def findNode(mp, n):
a = [ 0 ] * n
for i in range (n):
a[i] = mp[i + 1 ]
count0 = 0
for i in range (n):
if (a[i] = = 0 ):
count0 + = 1
count0 - = 1
if (count0 < = (n) /
( 2 )):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
N = 3
mp = {}
mp[ 1 ] = 0
mp[ 2 ] = 2
mp[ 3 ] = 0
findNode(mp, N)
|
C#
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);
}
}
|
Javascript
<script>
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( "Yes" );
}
else
document.write( "No" );
}
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);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)