#include #include #include #include #include #include #include using namespace std; typedef vector > Graph; const int MAX_N = 100001; int root; bool visited[MAX_N]; bool finished[MAX_N]; void dfs(int u, int parent, const Graph &G, stack &path) { visited[u] = true; path.push(u); for (int v : G[u]) { if (v == parent) continue; if (finished[v]) continue; if (visited[v] && !finished[v]) { path.push(v); root = v; return; } dfs(v, u, G, path); if (root != -1) return; } path.pop(); finished[u] = true; } vector cycle_detection(int start, const Graph &G) { root = -1; memset(visited, false, sizeof(visited)); memset(finished, false, sizeof(finished)); stack path; dfs(start, -1, G, path); vector res; while (!path.empty()) { res.push_back(path.top()); path.pop(); } return res; } int main() { int N; cin >> N; Graph G(N + 1); map> E; int a, b; for (int i = 0; i < N; ++i) { cin >> a >> b; E[a][b] = i + 1; E[b][a] = i + 1; G[a].push_back(b); G[b].push_back(a); } vector path = cycle_detection(1, G); if (path.empty()) { cout << -1 << endl; } else { while (path[0] != path.back()) { path.pop_back(); } path.pop_back(); int L = path.size(); vector ids; for (int i = 0; i < L; ++i) { int u = path[i]; int v = path[(i + 1) % L]; ids.push_back(E[u][v]); } sort(ids.begin(), ids.end()); cout << L << endl; for (int id : ids) { cout << id << " "; } cout << endl; } return 0; }