fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int n,e;
  6. cin>>n>>e;
  7. vector<int>g[n+1];
  8. for(int i=0;i<e;i++){
  9. int x,y;
  10. cin>>x>>y;
  11. g[x].push_back(y);
  12. g[y].push_back(x);
  13. }
  14. vector<int>v(n+1,0);
  15. int s;
  16. cin>>s;
  17. queue<int>q;
  18. q.push(s);
  19. v[s]=1;
  20. while(!q.empty()){
  21. for(auto it:g[q.front()]){
  22. if(v[it]==0){
  23. v[it]=1;
  24. q.push(it);
  25. }
  26. }
  27. q.pop();
  28. }
  29. for(int i=1;i<=n;i++){
  30. if(v[i]==1){
  31. cout<<i<<" can be visited\n";
  32. }
  33. else cout<<i<<" can't be visited\n";
  34. }
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5316KB
stdin
6 4
1 2
1 3
1 4
5 6
1
stdout
1 can be visited
2 can be visited
3 can be visited
4 can be visited
5 can't be visited
6 can't be visited