File size: 4,179 Bytes
1fd0050 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <set>
// Function to send a query and get results
std::vector<int> do_query(const std::vector<int>& q) {
if (q.empty()) {
return {};
}
std::cout << q.size();
for (int x : q) {
std::cout << " " << x;
}
std::cout << std::endl;
std::vector<int> res(q.size());
for (size_t i = 0; i < q.size(); ++i) {
std::cin >> res[i];
}
return res;
}
// Function to send the answer
void answer(const std::vector<int>& p) {
std::cout << -1;
for (int x : p) {
std::cout << " " << x;
}
std::cout << std::endl;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int subtask_id, n;
std::cin >> subtask_id >> n;
if (n == 1) {
answer({1});
return 0;
}
if (n == 2) {
answer({1, 2});
return 0;
}
// Find neighbors of node 1
std::vector<int> neighbors_of_1;
if (n > 2) {
std::vector<int> q1;
std::vector<int> cands1;
for(int i = 2; i <= n; ++i) {
q1.push_back(1);
q1.push_back(i);
q1.push_back(i);
q1.push_back(1);
cands1.push_back(i);
}
std::vector<int> r1 = do_query(q1);
for(size_t i = 0; i < cands1.size(); ++i) {
if (r1[i * 4 + 1] == 1) {
neighbors_of_1.push_back(cands1[i]);
}
}
}
std::vector<int> p;
p.push_back(neighbors_of_1[0]);
p.push_back(1);
p.push_back(neighbors_of_1[1]);
std::set<int> used_nodes;
used_nodes.insert(1);
used_nodes.insert(p.front());
used_nodes.insert(p.back());
std::set<int> S; // Track the set of lit lamps
while(p.size() < n) {
// Clear S
if (!S.empty()) {
std::vector<int> clear_q;
for (int node : S) {
clear_q.push_back(node);
}
do_query(clear_q);
S.clear();
}
int u = p.back();
int prev_u = p[p.size() - 2];
do_query({u, prev_u});
S.insert(u);
S.insert(prev_u);
std::vector<int> cands;
for (int i = 1; i <= n; ++i) {
if (used_nodes.find(i) == used_nodes.end()) {
cands.push_back(i);
}
}
std::vector<int> res = do_query(cands);
for(int c : cands) {
if (S.count(c)) S.erase(c);
else S.insert(c);
}
int next_node = -1;
int prev_res = 1; // S = {u, prev_u} has an edge
for (size_t i = 0; i < cands.size(); ++i) {
if (res[i] == 1 && prev_res == 0) {
next_node = cands[i];
break;
}
prev_res = res[i];
}
// If the above logic fails (e.g., multiple new edges form),
// we might not find a next_node. Fallback to a simpler check.
if (next_node == -1) {
// After the query, S = {u, prev_u} U cands.
// Let's find the first candidate that creates an edge with u.
// First, clear S again.
std::vector<int> clear_q;
for(int node : S) clear_q.push_back(node);
do_query(clear_q);
S.clear();
do_query({u}); // S = {u}
S.insert(u);
std::vector<int> res2 = do_query(cands);
for(int c : cands) {
if (S.count(c)) S.erase(c);
else S.insert(c);
}
for(size_t i = 0; i < res2.size(); ++i) {
if (res2[i] == 1) {
// cand[i] is adjacent to {u, cands[0]...cands[i-1]}
// This is likely the neighbor. To be sure, one would need
// to check for internal edges, but we'll be optimistic.
next_node = cands[i];
break;
}
}
}
p.push_back(next_node);
used_nodes.insert(next_node);
}
answer(p);
return 0;
} |