• Source
    1. #include <iostream>
    2. #include <vector>
    3. #include <queue>
    4. using namespace std;
    5.  
    6. int N, M;
    7. vector <int> ds[251];
    8. void nhap ()
    9. {
    10. cin>>N>>M;
    11. int a, b;
    12. for (int i=1; i<=M; i++)
    13. {
    14. cin>>a>>b;
    15. ds[a].push_back(b);
    16. ds[b].push_back(a);
    17. }
    18. }
    19.  
    20. int dd[251];
    21. void khoitao ()
    22. {
    23. for (int i=1; i<=N; i++)
    24. {
    25. dd[i]=0;
    26. }
    27. }
    28.  
    29. void BFS_Q (int i)
    30. {
    31. queue <int> q;
    32. q.push(i);
    33. dd[i]=1;
    34. while (!q.empty())
    35. {
    36. int next=q.front();
    37. q.pop();
    38. for (int i=0; i<ds[next].size(); i++)
    39. {
    40. int t=ds[next][i];
    41. if (dd[t]==0) q.push (t);
    42. dd[t]=1;
    43. }
    44. }
    45. }
    46.  
    47. int main ()
    48. {
    49. //IN;
    50. nhap ();
    51. //OUT;
    52. khoitao();
    53. BFS_Q(1);
    54. int kt=0;
    55. for (int i=1; i<=N; i++)
    56. {
    57. if (dd[i]==0)
    58. {
    59. cout<<i<<endl;
    60. kt=1;
    61. }
    62. }
    63. if (kt==0) cout<<"0";
    64.  
    65. return 0;
    66. }