fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, m, a, b, visited[101];
  4. vector<int> adj[101];
  5.  
  6. int go(int here){
  7. visited[here] = 1;
  8. int cnt = 1;
  9. for(int there : adj[here]){
  10. if(visited[there]) continue;
  11. cnt += go(there);
  12. }
  13. return cnt;
  14. }
  15.  
  16. int main(){
  17. cin >> n >> m;
  18. for(int i = 0; i < m; i++){
  19. cin >> a >> b;
  20. adj[a].push_back(b);
  21. adj[b].push_back(a);
  22. }
  23.  
  24. cout << go(1) << '\n';
  25. }
Success #stdin #stdout 0s 5320KB
stdin
7
6
1 2
2 3
1 5
5 2
5 6
4 7
stdout
5