fork download
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. int n, m;
  9. n = scanner.nextInt();
  10. m = scanner.nextInt();
  11.  
  12. ArrayList<Integer>[] g = new ArrayList[n];
  13. for (int i = 0; i < n; i++) {
  14. g[i] = new ArrayList<>();
  15. }
  16.  
  17. for (int i = 1; i <= m; i++) {
  18. // Taking input for an undirected graph.
  19. int x, y;
  20. x = scanner.nextInt();
  21. y = scanner.nextInt();
  22. g[x].add(y);
  23. g[y].add(x);
  24. }
  25.  
  26. for (int i = 0; i < n; i++) {
  27. int c = g[i].size();
  28. System.out.println(i + " " + c);
  29. }
  30. }
  31. }
Success #stdin #stdout 0.23s 60836KB
stdin
5 4 
0 1 
1 2 
2 3 
4 2 
stdout
0 1
1 2
2 3
3 1
4 1