1. void find_cycle(){
  2. color.assign(n, 0); // Directed graph
  3. visited.assign(n, false); // Undirected graph
  4. parent.assign(n, -1);
  5. cycle_start = -1;
  6. for(int v = 0; v<n; v++){
  7. if(color[v] == 0 and dfs(v)){
  8. break;
  9. }
  10. }
  11. if(cycle_start == -1){
  12. //cout << "Ascyclic Graph"<<endl;
  13. //cout << -1 << endl;
  14. cout << "IMPOSSIBLE"<< endl;
  15. }
  16. else{
  17. std::vector<int> cycle;
  18. cycle.push_back(cycle_start);
  19. for(int v = cycle_end; v != cycle_start; v = parent[v]){
  20. cycle.push_back(v);
  21. }
  22. cycle.push_back(cycle_start);
  23. reverse(cycle.begin(), cycle.end());
  24. // sort(cycle.begin(), cycle.end());
  25. cout << cycle.size()<< endl;
  26. for(int v:cycle){
  27. cout << v+1 <<' ';
  28. }
  29. }
  30. }