// clang-format off #include #include #include #define mp make_pair #define fst first #define snd second #define forn(i,n) for (int i = 0; i < int(n); i++) #define forn1(i,n) for (int i = 1; i <= int(n); i++) #define popcnt __builtin_popcountll #define ffs __builtin_ffsll #define ctz __builtin_ctzll #define clz __builtin_clz #define clzll __builtin_clzll #define all(a) (a).begin(), (a).end() using namespace std; using namespace __gnu_pbds; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using pii = pair; using pli = pair; using pil = pair; using pll = pair; template using vec = vector; using vi = vec; using vl = vec; template using que = queue; template using deq = deque; template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; template using ordered_map = tree, rb_tree_tag, tree_order_statistics_node_update>; template T id(T b) {return b;}; template void chmax(T &x, T y) {if (x < y) x = y;} template void chmin(T &x, T y) {if (x > y) x = y;} template bool contains(S &s, K k) { return s.find(k) != s.end(); } template bool getf(T flag, size_t i) { return (flag>>i) & 1; } template T setf(T flag, size_t i) { return flag | (T(1)< T unsetf(T flag, size_t i) { return flag & ~(T(1)<> h >> w >> n; vec> r(h), c(w); vec xy; forn(i, n) { int x, y; cin >> x >> y; x--, y--; r[x].emplace_back(y, i); c[y].emplace_back(x, i); xy.emplace_back(x, y); } for (auto& r : r) { sort(all(r)); } for (auto& c : c) { sort(all(c)); } vec g(n); forn(i, h) { for (int j = 0; j < int(r[i].size()) - 1; j++) { g[r[i][j].snd].push_back(r[i][j + 1].snd); g[r[i][j + 1].snd].push_back(r[i][j].snd); } } forn(i, w) { for (int j = 0; j < int(c[i].size()) - 1; j++) { g[c[i][j].snd].push_back(c[i][j + 1].snd); g[c[i][j + 1].snd].push_back(c[i][j].snd); } } vec in_path(n); vec vis(n); vi path; function dfs = [&](int u, int p) { if (in_path[u]) { path.push_back(u); return true; } vis[u] = true; in_path[u] = true; path.push_back(u); for (auto v : g[u]) { if (v != p) { if (dfs(v, u)) return true; } } in_path[u] = false; path.pop_back(); return false; }; forn(i, n) { if (!vis[i]) if (dfs(i, -1)) break; } if (path.empty()) { cout << "-1\n"; return 0; } reverse(all(path)); while (path.back() != path.front()) { path.pop_back(); } auto [x, y] = xy[path[0]]; vi ans; ans.push_back(path[0]); bool k = false; for (int i = 1; i < int(path.size()) - 1; i++) { auto [px, py] = xy[path[i - 1]]; auto [nx, ny] = xy[path[i + 1]]; if ((k and ny != py) or (!k and nx != px)) { k = not k; ans.push_back(path[i]); } x = nx; y = ny; } if (xy[ans[0]].snd != xy[ans[1]].snd) { auto v = ans.front(); ans.erase(ans.begin()); ans.push_back(v); } cout << path.size() - 1 << "\n"; for (auto x : ans) { cout << x + 1 << " "; } cout << "\n"; return 0; }