| #include <bits/stdc++.h> |
| using namespace std; |
|
|
| int main() { |
| ios::sync_with_stdio(false); |
| cin.tie(nullptr); |
| int sub, n; |
| cin >> sub >> n; |
| mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); |
| vector<int> all(n); |
| iota(all.begin(), all.end(), 1); |
| int kk = min(30, n / 3); |
| vector<int> seeds; |
| int max_trials = 1000; |
| for (int trial = 0; trial < max_trials; ++trial) { |
| random_shuffle(all.begin(), all.end()); |
| vector<int> cand(all.begin(), all.begin() + kk); |
| vector<int> seq = cand; |
| random_shuffle(seq.begin(), seq.end()); |
| cout << seq.size(); |
| for (int x : seq) cout << " " << x; |
| cout << "\n"; |
| cout.flush(); |
| vector<int> res(seq.size()); |
| for (int &x : res) cin >> x; |
| bool good = true; |
| for (int b : res) if (b) { |
| good = false; |
| break; |
| } |
| if (good) { |
| seeds = cand; |
| break; |
| } |
| if (trial == max_trials - 1) { |
| |
| kk /= 2; |
| trial = -1; |
| } |
| } |
| |
| vector<deque<int>> chains; |
| set<int> known_set; |
| for (int s : seeds) { |
| chains.emplace_back(deque<int>{s}); |
| known_set.insert(s); |
| } |
| int num_chains = chains.size(); |
| |
| while (known_set.size() < n) { |
| |
| vector<int> ends; |
| map<int, pair<int, int>> end_info; |
| for (int ci = 0; ci < chains.size(); ++ci) { |
| int fr = chains[ci].front(); |
| int bk = chains[ci].back(); |
| if (find(ends.begin(), ends.end(), fr) == ends.end()) { |
| ends.push_back(fr); |
| end_info[fr] = {ci, 0}; |
| } |
| if (find(ends.begin(), ends.end(), bk) == ends.end()) { |
| ends.push_back(bk); |
| end_info[bk] = {ci, 1}; |
| } |
| } |
| |
| vector<int> current_f = ends; |
| bool ind_ok = false; |
| while (!ind_ok && !current_f.empty()) { |
| vector<int> order = current_f; |
| random_shuffle(order.begin(), order.end()); |
| cout << order.size(); |
| for (int x : order) cout << " " << x; |
| cout << "\n"; |
| cout.flush(); |
| vector<int> res_ind(order.size()); |
| for (int &x : res_ind) cin >> x; |
| int hit_pos = -1; |
| for (int j = 0; j < res_ind.size(); ++j) { |
| if (res_ind[j]) { |
| hit_pos = j; |
| break; |
| } |
| } |
| if (hit_pos == -1) { |
| ind_ok = true; |
| current_f = order; |
| break; |
| } |
| |
| int conf = order[hit_pos]; |
| vector<int> prev_order(order.begin(), order.begin() + hit_pos); |
| |
| vector<int> seq_m; |
| seq_m.push_back(conf); |
| for (int p : prev_order) { |
| seq_m.push_back(p); |
| seq_m.push_back(p); |
| } |
| seq_m.push_back(conf); |
| cout << seq_m.size(); |
| for (int x : seq_m) cout << " " << x; |
| cout << "\n"; |
| cout.flush(); |
| vector<int> res_m(seq_m.size()); |
| for (int &x : res_m) cin >> x; |
| |
| vector<bool> adjs(prev_order.size(), false); |
| int base = 1; |
| for (int ii = 0; ii < prev_order.size(); ++ii) { |
| int fb_idx = base + 2 * ii; |
| adjs[ii] = res_m[fb_idx]; |
| } |
| |
| int target_ii = -1; |
| int cnt = 0; |
| for (int ii = 0; ii < adjs.size(); ++ii) { |
| if (adjs[ii]) { |
| target_ii = ii; |
| ++cnt; |
| } |
| } |
| assert(cnt == 1); |
| int target = prev_order[target_ii]; |
| |
| auto [c1, s1] = end_info[conf]; |
| auto [c2, s2] = end_info[target]; |
| if (c1 == c2) { |
| |
| assert(false); |
| } |
| |
| |
| deque<int> ch1 = chains[c1]; |
| if (s1 == 0) reverse(ch1.begin(), ch1.end()); |
| deque<int> ch2 = chains[c2]; |
| if (s2 == 1) reverse(ch2.begin(), ch2.end()); |
| |
| for (int x : ch2) ch1.push_back(x); |
| chains[c1] = ch1; |
| |
| chains.erase(chains.begin() + c2); |
| |
| vector<int> new_f; |
| for (int ee : current_f) { |
| if (ee != conf && ee != target) new_f.push_back(ee); |
| } |
| current_f = new_f; |
| |
| } |
| if (current_f.empty()) { |
| |
| break; |
| } |
| |
| |
| vector<int> light_seq = current_f; |
| random_shuffle(light_seq.begin(), light_seq.end()); |
| |
| vector<int> remain_l; |
| for (int i = 1; i <= n; ++i) { |
| if (known_set.find(i) == known_set.end()) remain_l.push_back(i); |
| } |
| int rsz = remain_l.size(); |
| |
| vector<int> seq = light_seq; |
| for (int u : remain_l) { |
| seq.push_back(u); |
| seq.push_back(u); |
| } |
| |
| for (int f : light_seq) seq.push_back(f); |
| cout << seq.size(); |
| for (int x : seq) cout << " " << x; |
| cout << "\n"; |
| cout.flush(); |
| vector<int> res(seq.size()); |
| for (int &x : res) cin >> x; |
| |
| vector<int> hit_u; |
| int light_sz = light_seq.size(); |
| for (int ri = 0; ri < rsz; ++ri) { |
| int pos_after_on = light_sz + 2 * ri; |
| if (res[pos_after_on]) hit_u.push_back(remain_l[ri]); |
| } |
| |
| int fsz = current_f.size(); |
| if (!hit_u.empty()) { |
| vector<int> match_seq; |
| for (int uu : hit_u) { |
| match_seq.push_back(uu); |
| vector<int> f_order = current_f; |
| random_shuffle(f_order.begin(), f_order.end()); |
| for (int ff : f_order) { |
| match_seq.push_back(ff); |
| match_seq.push_back(ff); |
| } |
| match_seq.push_back(uu); |
| } |
| cout << match_seq.size(); |
| for (int x : match_seq) cout << " " << x; |
| cout << "\n"; |
| cout.flush(); |
| vector<int> res_match(match_seq.size()); |
| for (int &x : res_match) cin >> x; |
| |
| int mbase = 0; |
| for (int hi = 0; hi < hit_u.size(); ++hi) { |
| int usize = 1 + 2 * fsz + 1; |
| int uu = hit_u[hi]; |
| |
| vector<int> connected_f; |
| int base_pos = mbase + 1; |
| for (int fi = 0; fi < fsz; ++fi) { |
| int fb_pos = base_pos + 2 * fi; |
| if (res_match[fb_pos]) connected_f.push_back(current_f[fi]); |
| } |
| mbase += usize; |
| |
| assert(connected_f.size() >= 1 && connected_f.size() <= 2); |
| for (int cf : connected_f) { |
| |
| auto [ci, si] = end_info[cf]; |
| |
| |
| |
| int target_ci = -1; |
| int target_si = -1; |
| for (int cii = 0; cii < chains.size(); ++cii) { |
| if (chains[cii].front() == cf) { |
| target_ci = cii; |
| target_si = 0; |
| break; |
| } |
| if (chains[cii].back() == cf) { |
| target_ci = cii; |
| target_si = 1; |
| break; |
| } |
| } |
| assert(target_ci != -1); |
| |
| if (target_si == 0) { |
| chains[target_ci].push_front(uu); |
| } else { |
| chains[target_ci].push_back(uu); |
| } |
| known_set.insert(uu); |
| |
| |
| adj[uu].push_back(cf); |
| adj[cf].push_back(uu); |
| } |
| if (connected_f.size() == 2) { |
| |
| int ci1 = -1, si1 = -1, ci2 = -1, si2 = -1; |
| |
| for (int cii = 0; cii < chains.size(); ++cii) { |
| if (chains[cii].front() == connected_f[0]) { |
| ci1 = cii; |
| si1 = 0; |
| break; |
| } |
| if (chains[cii].back() == connected_f[0]) { |
| ci1 = cii; |
| si1 = 1; |
| break; |
| } |
| } |
| |
| for (int cii = 0; cii < chains.size(); ++cii) { |
| if (cii == ci1) continue; |
| if (chains[cii].front() == connected_f[1]) { |
| ci2 = cii; |
| si2 = 0; |
| break; |
| } |
| if (chains[cii].back() == connected_f[1]) { |
| ci2 = cii; |
| si2 = 1; |
| break; |
| } |
| } |
| assert(ci1 != -1 && ci2 != -1); |
| |
| |
| deque<int> ch1 = chains[ci1]; |
| if (si1 == 0) reverse(ch1.begin(), ch1.end()); |
| deque<int> ch2 = chains[ci2]; |
| if (si2 == 1) reverse(ch2.begin(), ch2.end()); |
| |
| ch1.push_back(uu); |
| |
| |
| for (int x : ch2) ch1.push_back(x); |
| |
| chains[ci1] = ch1; |
| |
| chains.erase(chains.begin() + ci2); |
| } |
| } |
| } |
| } |
| |
| vector<vector<int>> adj(n + 1); |
| |
| for (auto &ch : chains) { |
| for (int ii = 0; ii + 1 < ch.size(); ++ii) { |
| int a = ch[ii], b = ch[ii + 1]; |
| adj[a].push_back(b); |
| adj[b].push_back(a); |
| } |
| } |
| |
| set<int> final_end_set; |
| for (auto &ch : chains) { |
| if (!ch.empty()) { |
| final_end_set.insert(ch.front()); |
| final_end_set.insert(ch.back()); |
| } |
| } |
| vector<int> final_ends(final_end_set.begin(), final_end_set.end()); |
| int fends = final_ends.size(); |
| if (fends > 0) { |
| |
| vector<pair<int, int>> possible_pairs; |
| for (int i = 0; i < fends; ++i) { |
| for (int j = i + 1; j < fends; ++j) { |
| possible_pairs.emplace_back(final_ends[i], final_ends[j]); |
| } |
| } |
| int np = possible_pairs.size(); |
| vector<int> final_seq; |
| for (auto [a, b] : possible_pairs) { |
| final_seq.push_back(a); |
| final_seq.push_back(b); |
| final_seq.push_back(a); |
| final_seq.push_back(b); |
| } |
| cout << final_seq.size(); |
| for (int x : final_seq) cout << " " << x; |
| cout << "\n"; |
| cout.flush(); |
| vector<int> res_final(final_seq.size()); |
| for (int &x : res_final) cin >> x; |
| |
| for (int pi = 0; pi < np; ++pi) { |
| int base = 4 * pi; |
| int b_pos = base + 1; |
| if (res_final[b_pos]) { |
| int a = possible_pairs[pi].first, b = possible_pairs[pi].second; |
| adj[a].push_back(b); |
| adj[b].push_back(a); |
| } |
| } |
| } |
| |
| vector<int> perm; |
| int start = 1; |
| int curr = start; |
| int pre = -1; |
| set<int> visited; |
| perm.push_back(curr); |
| visited.insert(curr); |
| while (true) { |
| int nxt = -1; |
| for (int nb : adj[curr]) { |
| if (nb != pre) { |
| nxt = nb; |
| break; |
| } |
| } |
| if (nxt == -1 || visited.count(nxt)) break; |
| pre = curr; |
| curr = nxt; |
| perm.push_back(curr); |
| visited.insert(curr); |
| if (curr == start) break; |
| } |
| |
| if (perm.size() < n) { |
| perm.clear(); |
| visited.clear(); |
| curr = start; |
| pre = -1; |
| perm.push_back(curr); |
| visited.insert(curr); |
| |
| int other_n = -1; |
| for (int nb : adj[start]) { |
| if (nb != adj[start][0]) { |
| other_n = nb; |
| break; |
| } |
| } |
| pre = adj[start][0]; |
| curr = other_n; |
| perm.push_back(curr); |
| visited.insert(curr); |
| while (true) { |
| int nxt = -1; |
| for (int nb : adj[curr]) { |
| if (nb != pre) { |
| nxt = nb; |
| break; |
| } |
| } |
| if (nxt == -1 || visited.count(nxt)) break; |
| pre = curr; |
| curr = nxt; |
| perm.push_back(curr); |
| visited.insert(curr); |
| if (curr == start) break; |
| } |
| } |
| |
| assert(perm.size() == n); |
| |
| cout << -1; |
| for (int p : perm) cout << " " << p; |
| cout << "\n"; |
| cout.flush(); |
| return 0; |
| } |