What is wrong in this code?

tatic class Graph{

  private class Vertex{
	 HashMap<String , Integer> neighbour = new HashMap<>();
  }
	 
  HashMap<String , Vertex> vertices ;
 
  public Graph(){
	vertices = new HashMap<>();
  }

  public void addVertex(String add) {
	Vertex vtx = new Vertex();
	vertices.put(add , vtx);
  }

  public void addEdge(String vname1 , String vname2 , int length) {
	Vertex vtx1 = vertices.get(vname1);
	Vertex vtx2 = vertices.get(vname2);
	
	if(vtx1==null || vtx2==null || vtx1.neighbour.containsKey(vname2))
		return;
	
	vtx1.neighbour.put(vname2 , length);
	vtx2.neighbour.put(vname1 , length);
  }

  static int ans;

  public void beautiful(String parent , HashMap<String , Boolean> processed){
	  
	  int pchild = vertices.get(parent).neighbour.size();

	  processed.put(parent , true);

	  ArrayList<String> keys = new ArrayList<>( vertices.get(parent).neighbour.keySet() ) ;
	  for(String key : keys){
		  if(vertices.get(key).neighbour.size()>pchild)
		  {
			 ans++;
		  }		
	  }

	  for(String key : keys){
		  if(processed.get(key)!=null && processed.get(key)==false)
		  	  beautiful(key , processed);
	  }

  }

}

public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	int n=sc.nextInt();
	int m=sc.nextInt();

	Graph graph = new Graph();
	int master=0;
	for(int i=1 ; i<=m ; i++){
		int x=sc.nextInt();
		int y=sc.nextInt();
		
		if(i==1)
			master=x;

		if(graph.vertices.get(x+"")==null)
			graph.addVertex(x+"");

		if(graph.vertices.get(y+"")==null)
			graph.addVertex(y+"");

		graph.addEdge(x+"" , y+"" , 0);
	}

	graph.beautiful(master+"" , new HashMap<String, Boolean>());
	System.out.println(graph.ans);
}

Hey @Himanshu-Jhawar-2273952536067590
The problem is simple implementation of DFS while maintaining a count of children of the vertices visited. Let u be a vertex and v be its child. Then, check if children[v]>children[u].This way, check for each vertex while traversing the graph in DFS. One important thing to note that actually there are many trees in the input(a graph with no cycles and self loops is a tree). That means , you have to apply DFS for each of these trees.


input :
13 9
1 2
2 4
2 5
2 6
5 8
1 3
3 7
7 11
7 13
output :
2