fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define nmax 200005
  5. vector<int> a[nmax];
  6. ll cha[nmax] , dinhdau = 0 , dinhcuoi = 0;
  7. bool visited[nmax] , instack[nmax] , cycle[nmax];
  8. bool dfs(ll u , ll parent){
  9. visited[u] = true;
  10. for (auto i : a[u]){
  11. if (i == parent){
  12. continue;
  13. }
  14. if (visited[i]){
  15. dinhdau = i;
  16. dinhcuoi = u;
  17. return true;
  18. }
  19. else if (!visited[i]){
  20. cha[i] = u;
  21. if (dfs(i , u)) return true;
  22. }
  23. }
  24. return false;
  25. }
  26. int main(){
  27. int n , m;
  28. cin >> n >> m;
  29. for (int i = 1 ; i <= m ; i++){
  30. int x , y;
  31. cin >> x >> y;
  32. a[x].push_back(y);
  33. a[y].push_back(x);
  34. }
  35. for (int i = 1 ; i <= n ; i++){
  36. if (!visited[i]){
  37. if (dfs(i , -1)){
  38. break;
  39. }
  40. }
  41. }
  42. vector<ll> ans;
  43. ll cur = dinhcuoi;
  44. while(cur != dinhdau){
  45. ans.push_back(cur);
  46. cur = cha[cur];
  47. }
  48. if (dinhdau == 0 or dinhcuoi == 0){
  49. cout << "IMPOSSIBLE";
  50. return 0;
  51. }
  52. ans.push_back(dinhdau);
  53. reverse(ans.begin() , ans.end());
  54. ans.push_back(dinhdau);
  55. cout << ans.size() << endl;
  56. for (auto i : ans){
  57. cout << i << " ";
  58. }
  59.  
  60.  
  61. // for (int i = 1 ;i <= n ;i++){
  62. // cout << cycle[i] << endl;
  63. // }
  64. }
Success #stdin #stdout 0.01s 9880KB
stdin
Standard input is empty
stdout
IMPOSSIBLE