Not getting accepted!

Tried many test cases available on the internet, worked for them, but not working for submission.
What am i Doing wrong in this?

public class Main
{
public static void main(String [] args)
{
Scanner scn = new Scanner(System.in);

    int n = scn.nextInt();
    int m = scn.nextInt();
    
    
    //graph
    HashMap <Integer, HashSet <Integer> > graph = new HashMap <> ();
    
    
    for(int i = 1; i<=m; i++)
    {
        int x = scn.nextInt();
        int y = scn.nextInt();
        
        if(!graph.containsKey(x))
            graph.put(x, new HashSet <> ());
            
        if(!graph.containsKey(y))
            graph.put(y, new HashSet <> ());
            
        if(x < y)
            graph.get(x).add(y);
            
        else
            graph.get(y).add(x);
    }
    
    
    System.out.println(beautifulVertices(graph));
}






public static int beautifulVertices(HashMap <Integer, HashSet<Integer>>graph)
{
    
    int count = 0;
    
    HashMap <Integer,Boolean> visited = new HashMap <> ();
    
    
    for(int key : graph.keySet())
    {
        if(visited.containsKey(key))
            continue;
        
        Queue <Integer> q = new LinkedList <> ();
        
        q.add(key);
        
        while(!q.isEmpty())
        {
            int rv = q.remove();
                
            if(visited.containsKey(rv))
                continue;
                            
            visited.put(rv,true);
            
            for(int nbr : graph.get(rv))
            {
                if(visited.containsKey(nbr))
                    continue;
                    
                if(graph.get(nbr).size() > graph.get(key).size())
                        count++;
                
                q.add(nbr);
            }
        }
    }
    
    return count;
}

}

The problem is a 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.

You can refer to the following code for better understanding:
https://ide.codingblocks.com/s/204947

Hope, this would help.
Give a like if you are satisfied.

Hey? I understood your code. But why my code is not working?