#include "sonlib.h" #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; int n; inline bool make_move(int x) { return MoveProbe(x+1); } void solve_bipartite() { int known_right = -1; FOR(i,1,n) { if (make_move(i%n)) { known_right = i-1; break; } } assert(known_right != -1); make_move(known_right); vector<bool> is_left(n,false); REP(i,n) if (i!=known_right) { if (make_move(i)) { Examine(); is_left[i] = true; make_move(known_right); } } make_move(0); REP(i,n) if(!is_left[i]) { make_move(i); Examine(); make_move(0); } } namespace solve_triangle_helpers { vector<int> parent; vector<vector<int>> children; void add_connection(int a, int b) { children[a].push_back(b); parent[b] = a; } void dfs(int x, int depth) { if (depth==0) { Examine(); REP(y,n) if (parent[y]==-1) { if (make_move(y)) { add_connection(x,y); make_move(x); } } } else { for (int y : children[x]) { make_move(y); dfs(y, depth-1); make_move(x); } } } } void solve_triangle() { using namespace solve_triangle_helpers; parent.assign(n, -1); children.resize(n); parent[0] = -2; for (int depth=0;;++depth) { make_move(1); make_move(2); make_move(0); dfs(0, depth); } } void solve_cycle() { mt19937 rng(418691082); //random_device rng; vector<int> cycle; vector<vector<int>> transition; vector<int> found(n,false); cycle.push_back(0); found[0] = true; int want = n == 4 ? 2 : n%2==0 ? n/2+1 : (n+1)/2; while (int(cycle.size()) < want) { vector<int> possible; REP(i,n) if (!found[i]) possible.push_back(i); if (n%2==0 && int(cycle.size())==n/2) possible.push_back(0); for(;;) { shuffle(possible.begin(), possible.end(), rng); for (int x : possible) { if (make_move(x)) { cycle.push_back(x); transition.push_back(possible); found[x] = 1; goto step_success; } } make_move(cycle.back()); } step_success:; } if (n==4) { vector<int> remaining; REP(i,n) if(i!=cycle[0] && i!=cycle[1]) remaining.push_back(i); Examine(); make_move(remaining[0]); Examine(); make_move(cycle[0]); Examine(); make_move(remaining[1]); Examine(); return; } if (n%2==1) { Examine(); make_move(cycle[0]); } REP(i,int(transition.size())) { Examine(); for (int x : transition[i]) { if (x==cycle[i+1]) break; make_move(x); } Examine(); make_move(cycle[i+1]); } } /* namespace solve_generic_helpers { int current; vector<int> double_connection; vector<<vector<int>> outgoing_permutation; vector<bool> leaf; bool try_escape() { vector<int> perm; REP(i,n) if(i!=current && !leaf[i]) { if (make_move(i)) { double_connection[current] = i; outgoing_permutation[current] = perm; return true; } perm.push_back(i); } make_move(current); return false; } void halve() { int n2 = int(outgoing_permutation[current].size())/2; REP(i,n2) { int x = outgoing_permutation[current][i]; make_move(x); } if (make_move(double_connection[current])) { outgoing_permutation[current].resize(n2); current = double_connection[current]; return; } FOR(i,n2,int(outgoing_permutation[current].size())-1) { int x = outgoing_permutation[current][i]; if (make_move(x)) { dfs(x,double_connection[current], current); return; } } make_move(double_connection[current]); outgoing_permutation[current].erase(outgoing_permutation[current].begin(), outgoing_permutation[current].begin()+n2); current = double_connection[current]; } vector<int> visited; void dfs(int ,int,int); void dfs2(int a,int b) { if(visited[b]) return; Examine(); visited[b] = true; // } void dfs(int a,int b,int c) { if(visited[a]) return; Examine(); visited[a] = true; make_move(b); dfs2(a,b); make_move(a); REP(d,n) if (!visited[d]) { make_move(d); if (make_move(b)) { make_move(d); dfs2(a,d); make_move(a); } else { if (make_move(c)) { make_move(d); if(make_move(a)) { } else { } } else { dfs2(a,d); make_move(a); } } } } } void solve_generic() { using namespace solve_generic_helpers; leaf.assign(n, false); double_connection.assign(n,-1); outgoing_permutation.resize(n); visited.assign(n,false); current = 0; REP(i,n-1) if (try_escape()) goto escaped_0; // no exit from 0 Examine(); FOR(i,1,n-1) { make_move(i); Examine(); make_move(0); } return; escaped_0: for (;;) { if (double_connection[current]==-1) { for (;;) if (try_escape()) break; } else if(outgoing_permutation.size()==1u) { dfs(current, outgoing_permutation[0], double_connection[current]); return; } else { halve(); } } } */ int main() { n = GetN(); switch (GetSubtask()) { case 1: solve_bipartite(); break; case 2: solve_triangle(); break; case 3: solve_cycle(); break; default: //solve_generic(); break; } }
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | #include "sonlib.h" #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; int n; inline bool make_move(int x) { return MoveProbe(x+1); } void solve_bipartite() { int known_right = -1; FOR(i,1,n) { if (make_move(i%n)) { known_right = i-1; break; } } assert(known_right != -1); make_move(known_right); vector<bool> is_left(n,false); REP(i,n) if (i!=known_right) { if (make_move(i)) { Examine(); is_left[i] = true; make_move(known_right); } } make_move(0); REP(i,n) if(!is_left[i]) { make_move(i); Examine(); make_move(0); } } namespace solve_triangle_helpers { vector<int> parent; vector<vector<int>> children; void add_connection(int a, int b) { children[a].push_back(b); parent[b] = a; } void dfs(int x, int depth) { if (depth==0) { Examine(); REP(y,n) if (parent[y]==-1) { if (make_move(y)) { add_connection(x,y); make_move(x); } } } else { for (int y : children[x]) { make_move(y); dfs(y, depth-1); make_move(x); } } } } void solve_triangle() { using namespace solve_triangle_helpers; parent.assign(n, -1); children.resize(n); parent[0] = -2; for (int depth=0;;++depth) { make_move(1); make_move(2); make_move(0); dfs(0, depth); } } void solve_cycle() { mt19937 rng(418691082); //random_device rng; vector<int> cycle; vector<vector<int>> transition; vector<int> found(n,false); cycle.push_back(0); found[0] = true; int want = n == 4 ? 2 : n%2==0 ? n/2+1 : (n+1)/2; while (int(cycle.size()) < want) { vector<int> possible; REP(i,n) if (!found[i]) possible.push_back(i); if (n%2==0 && int(cycle.size())==n/2) possible.push_back(0); for(;;) { shuffle(possible.begin(), possible.end(), rng); for (int x : possible) { if (make_move(x)) { cycle.push_back(x); transition.push_back(possible); found[x] = 1; goto step_success; } } make_move(cycle.back()); } step_success:; } if (n==4) { vector<int> remaining; REP(i,n) if(i!=cycle[0] && i!=cycle[1]) remaining.push_back(i); Examine(); make_move(remaining[0]); Examine(); make_move(cycle[0]); Examine(); make_move(remaining[1]); Examine(); return; } if (n%2==1) { Examine(); make_move(cycle[0]); } REP(i,int(transition.size())) { Examine(); for (int x : transition[i]) { if (x==cycle[i+1]) break; make_move(x); } Examine(); make_move(cycle[i+1]); } } /* namespace solve_generic_helpers { int current; vector<int> double_connection; vector<<vector<int>> outgoing_permutation; vector<bool> leaf; bool try_escape() { vector<int> perm; REP(i,n) if(i!=current && !leaf[i]) { if (make_move(i)) { double_connection[current] = i; outgoing_permutation[current] = perm; return true; } perm.push_back(i); } make_move(current); return false; } void halve() { int n2 = int(outgoing_permutation[current].size())/2; REP(i,n2) { int x = outgoing_permutation[current][i]; make_move(x); } if (make_move(double_connection[current])) { outgoing_permutation[current].resize(n2); current = double_connection[current]; return; } FOR(i,n2,int(outgoing_permutation[current].size())-1) { int x = outgoing_permutation[current][i]; if (make_move(x)) { dfs(x,double_connection[current], current); return; } } make_move(double_connection[current]); outgoing_permutation[current].erase(outgoing_permutation[current].begin(), outgoing_permutation[current].begin()+n2); current = double_connection[current]; } vector<int> visited; void dfs(int ,int,int); void dfs2(int a,int b) { if(visited[b]) return; Examine(); visited[b] = true; // } void dfs(int a,int b,int c) { if(visited[a]) return; Examine(); visited[a] = true; make_move(b); dfs2(a,b); make_move(a); REP(d,n) if (!visited[d]) { make_move(d); if (make_move(b)) { make_move(d); dfs2(a,d); make_move(a); } else { if (make_move(c)) { make_move(d); if(make_move(a)) { } else { } } else { dfs2(a,d); make_move(a); } } } } } void solve_generic() { using namespace solve_generic_helpers; leaf.assign(n, false); double_connection.assign(n,-1); outgoing_permutation.resize(n); visited.assign(n,false); current = 0; REP(i,n-1) if (try_escape()) goto escaped_0; // no exit from 0 Examine(); FOR(i,1,n-1) { make_move(i); Examine(); make_move(0); } return; escaped_0: for (;;) { if (double_connection[current]==-1) { for (;;) if (try_escape()) break; } else if(outgoing_permutation.size()==1u) { dfs(current, outgoing_permutation[0], double_connection[current]); return; } else { halve(); } } } */ int main() { n = GetN(); switch (GetSubtask()) { case 1: solve_bipartite(); break; case 2: solve_triangle(); break; case 3: solve_cycle(); break; default: //solve_generic(); break; } } |