fork download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. int n, m;
  14. n = scanner.nextInt();
  15. m = scanner.nextInt();
  16.  
  17. ArrayList<Integer>[] g = new ArrayList[n];
  18. for (int i = 0; i < n; i++) {
  19. g[i] = new ArrayList<>();
  20. }
  21.  
  22. for (int i = 1; i <= m; i++) {
  23. // Taking input for an undirected graph.
  24. int x, y;
  25. x = scanner.nextInt();
  26. y = scanner.nextInt();
  27. g[x].add(y);
  28. g[y].add(x);
  29. }
  30.  
  31. for (int i = 0; i < n; i++) {
  32. int c = g[i].size();
  33. System.out.println(i + " " + c);
  34. }
  35. }
  36. }
Success #stdin #stdout 0.14s 60764KB
stdin
5 4 
0 1 
1 2 
2 3 
4 2 
stdout
0 1
1 2
2 3
3 1
4 1