fork download
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. int d[501][501];
  5. bool visit[501];
  6.  
  7. int main(void) {
  8. int n, m;
  9. scanf("%d %d", &n, &m);
  10. while (m--) {
  11. int x, y;
  12. scanf("%d %d", &x, &y);
  13. d[x][y] = 1;
  14. d[y][x] = 1;
  15. }
  16.  
  17. int ans = 0;
  18. for (int i = 1; i <= n; i++) {
  19. if (d[1][i] == 1 && visit[i] == false) {
  20. ans += 1;
  21. visit[i] = true;
  22. for (int j = 1; j <= n; j++) {
  23. if (d[i][j] == 1 && visit[j] == false) {
  24. ans += 1;
  25. visit[j] = true;
  26. }
  27. }
  28. }
  29. }
  30.  
  31. printf("%d\n", ans);
  32. return 0;
  33. }
Success #stdin #stdout 0s 10408KB
stdin
6
5
1 2
1 3
3 4
2 3
4 5
stdout
3