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