//無向グラフでの基本的なdfs //計算量 パス検出・閉路検出:O(E+V) //verified with //https://yukicoder.me/problems/no/1254 #include using namespace std; struct io_setup{ io_setup(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; template struct Graph{ struct edge{ int to, id; edge(int to, int id) : to(to), id(id){} }; vector> es; vector used; vector keep; const int n; int m; Graph(int n) : n(n), m(0){ es.resize(n), used.resize(n); } void add_edge(int from, int to){ es[from].emplace_back(to, m); if(!directed) es[to].emplace_back(from, m); m++; } bool trace(int now, int t){ used[now] = true; if(now == t){ //keep.emplace_back(now) return true; } for(auto &e : es[now]){ if(used[e.to]) continue; if(trace(e.to, t)){ //keep.emplace_back(now); keep.emplace_back(e.id); return true; } } return false; } vector find_path(int s, int t){ keep.clear(), fill(begin(used), end(used), 0); trace(s, t), reverse(begin(keep), end(keep)); return keep; } int detect(int now, int pre = -1){ if(used[now]++) return 1; for(auto &e: es[now]){ if(e.id == pre) continue; int k = detect(e.to, e.id); if(k == 2) return 2; if(k == 1){ //keep.emplace_back(now); keep.emplace_back(e.id); return used[now]; } } return 0; } vector find_loop(){ keep.clear(), fill(begin(used), end(used), 0); for(int i = 0; i < n; i++){ if(used[i]) continue; detect(i); if(!keep.empty()){ reverse(begin(keep), end(keep)); return keep; } } return keep; } }; int main(){ int V; cin >> V; Graph G(V); for(int i = 0; i < V; i++){ int u, v; cin >> u >> v; u--, v--; G.add_edge(u, v); } vector loop = G.find_loop(); cout << loop.size() << '\n'; for(auto &e: loop) cout << e+1 << ' '; cout << '\n'; }