fork download
  1. import java.util.ArrayList;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. int n = 5, m = 4;
  6.  
  7. int[][] edges = {
  8. {0, 1},
  9. {1, 2},
  10. {2, 3},
  11. {3, 4}
  12. };
  13.  
  14. ArrayList<Integer>[] graph = new ArrayList[n];
  15. for (int i = 0; i < n; i++) {
  16. graph[i] = new ArrayList<>();
  17. }
  18.  
  19. for (int[] edge : edges) {
  20. int x = edge[0], y = edge[1];
  21. graph[x].add(y);
  22. graph[y].add(x);
  23. }
  24.  
  25. for (int i = 0; i < n; i++) {
  26. System.out.println(i + " " + graph[i].size());
  27. }
  28. }
  29. }
  30.  
Success #stdin #stdout 0.15s 57536KB
stdin
Standard input is empty
stdout
0 1
1 2
2 2
3 2
4 1