Beautiful Vertices

What is the error in my code?
import java.util.Scanner;
import java.util.ArrayList;

class Main{

static class Edge{
    int p ;
    int v ;

    Edge(int p, int v){
        this.p = p; //parent
        this.v = v;
    }
}

public static void addEdge(ArrayList<ArrayList<Edge>>graph, int u, int v){
    graph.get(u).add(new Edge(u,v));
    // graph.get(v).add(new Edge(u));       
}

public static int countBVtx(ArrayList<ArrayList<Edge>> graph){

    int count = 0;

    for(int v = 1; v < graph.size(); v++){
        int pSize = graph.get(v).size();
        for(int i = 0; i<pSize; i++){
            Edge ce =graph.get(v).get(i);
            int cSize = graph.get(ce.v).size();
            if(cSize>pSize)
            {
                count++;
            }
        }
    }

    return count;        
}

public static void display(ArrayList<ArrayList<Edge>> graph){
    for(int i =1; i<graph.size(); i++){
        System.out.print(i+"->");
        for(int j =0; j<graph.get(i).size(); j++){
            Edge curr = graph.get(i).get(j);
            System.out.print("(" +curr.p+","+ curr.v + ")-");
        }
        System.out.println();
    }
}



public static Scanner scn = new Scanner(System.in);
public static void main(String[] args){
    ArrayList<ArrayList<Edge>> graph = new ArrayList<>();
    int n = scn.nextInt();      //no of vertex
    int m = scn.nextInt();      //no of edge

    for(int i = 0; i<=n; i++){
        graph.add(new ArrayList<Edge>());
    }

    for(int i = 0; i< m; i++){
        int a = scn.nextInt();
        int b = scn.nextInt();

        addEdge(graph,a,b);
    }
    // display(graph);
    System.out.print(countBVtx(graph));

}

}

Anyone Please … there is a minute error which I am unable to find out

It’s passing for sample test cases but while running it’s giving error for larger test cases. For eg. answer was 6535 and mine is 6531. So please help

The compiler is giving this type of error:Note: Main.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details…However it is giving a warning in my editor also but the code is working. Please help:\

Here is my code:import java.util.*; class Main{ public static int count = 0; public static Scanner scn = new Scanner(System.in); public static void main(String[] args){ int n = scn.nextInt(); int m = scn.nextInt(); ArrayList graph[] = new ArrayList[n+1]; for(int i = 0; i<=n;i++){ graph[i]= new ArrayList(); } for(int i = 0; i<m; i++){ int x = scn.nextInt(); int y = scn.nextInt(); graph[x].add(y); graph[y].add(x); } boolean [] vis = new boolean[n+1]; for(int i = 1; i<=n; i++){ if(!vis[i]){ dfs(graph,vis,i,-1); } } System.out.println(count); } public static void dfs(ArrayList graph[], boolean[] vis, int curr, int par){ vis[curr] = true; if(par!=-1){ int currChild = graph[curr].size(); int parChild = graph[par].size(); if(currChild>parChild) count++; } for(int i = 0; i<graph[curr].size(); i++){ if(!vis[graph[curr].get(i)]){ dfs(graph,vis,graph[curr].get(i),curr); } } } }

Will anyone atleast reply? TA’s???

Okay I will check for errors.

I take that your latest code which you have provided is the one which is producing errors.

Hi
You cannot create arrays of parameterized types. So your line 8 is wrong instead use an arraylist like :
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>(n+1);

also you have an error of par variable as in function call you have written it as pr
Finally, a tip, never use Scanner outside of a function in a static manner. This is the reason for your unsafe message you are receiving.