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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#ifndef LOCAL
#pragma GCC optimize ("O3")
#endif

#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (c it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <typename A, typename B>
using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>;
using namespace __gnu_pbds;
template <typename T> using ordered_set =
  __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
                   __gnu_pbds::tree_order_statistics_node_update>;
// ordered_set<int> s; s.insert(1); s.insert(2);
// s.order_of_key(1);    // Out: 0.
// *s.find_by_order(1);  // Out: 2.

using ld = long double;
using ll = long long;

constexpr int mod = 1000 * 1000 * 1000 + 7;
constexpr int odw2 = (mod + 1) / 2;

void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; }
int Odejmij(int a, int b) { OdejmijOd(a, b); return a; }
void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; }
int Dodaj(int a, int b) { DodajDo(a, b); return a; }
int Mnoz(int a, int b) { return (ll) a * b % mod; }
void MnozDo(int& a, int b) { a = Mnoz(a, b); }
int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; }
int Odw(int a) { return Pot(a, mod - 2); }
void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); }
int Podziel(int a, int b) { return Mnoz(a, Odw(b)); }
int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; }

template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }

#include "sonlib.h"

void Losuj(vector<int>& v) {
  random_shuffle(v.begin(), v.end());
}

constexpr int nax = 405;

int n;
int ile_ruchow = 0;
bool last_result = false;

bool Rusz(int w) {
  assert(1 <= w and w <= n);
  ile_ruchow++;
  const bool result = MoveProbe(w);
  debug() << "Rusz(" imie(w) "): " << result;
  last_result = result;
  return result;
}

int poprzedni[nax];
bool dobry[nax];

// vector<int> Sciezka(int w) {
//   vector<int> path = {w};
//   while (path.back() != -1) {
//     path.push_back(poprzedni[path.back()]);
//   }
//   path.pop_back();
//   assert(!path.empty());
//   return path;
// }

int moja_pozycja = -1;

void IdzDoSzybko(int w) {
  vector<vector<int>> graf(n + 1);
  for (int i = 1; i <= n; i++) {
    if (!dobry[i]) continue;
    if (poprzedni[i] != -1) {
      graf[i].push_back(poprzedni[i]);
      graf[poprzedni[i]].push_back(i);
    }
  }
  vector<int> skad(n + 1, -1);
  skad[w] = -2;
  vector<int> kol = {w};
  for (int i = 0; i < (int) kol.size(); i++) {
    const int v = kol[i];
    if (moja_pozycja == v) {
      break;
    }
    for (int x : graf[v]) {
      if (skad[x] == -1) {
        skad[x] = v;
        kol.push_back(x);
      }
    }
  }

  while (moja_pozycja != w) {
    moja_pozycja = skad[moja_pozycja];
    Rusz(moja_pozycja);
  }
}

// void IdzZRootaDo(int w) {
//   vector<int> path = Sciezka(w);
//   path.pop_back();
//   reverse(path.begin(), path.end());
//   for (int x : path) {
//     Rusz(x);
//   }
// }
//
// void IdzDoRootaZ(int w) {
//   vector<int> path = Sciezka(w);
//   assert(path[0] == w);
//   path.erase(path.begin());
//   for (int x : path) {
//     Rusz(x);
//   }
// }

// 400 * 401 = 160400.
void Zakoncz() {
  for (int i = 1; i <= n; i++) {
    if (!dobry[i]) continue;
    // IdzZRootaDo(i);
    IdzDoSzybko(i);
    Examine();
    // IdzDoRootaZ(i);
  }
  assert(false);
}

// IdzDo/Z = 400 * 401 = 160400.
void FajnaWarstwa(vector<int> sasiedzi, vector<int> reszta);
void GlupiaWarstwa(vector<int> sasiedzi, vector<int> reszta);

// 7 * 200 * 200 = 280000.
// S * R: 5
void GlupiaWarstwa(vector<int> sasiedzi, vector<int> reszta) {
  debug() << "GlupiaWarstwa(" imie(sasiedzi) imie(reszta) ")";

  Losuj(sasiedzi);
  Losuj(reszta);

  if (reszta.empty()) {
    return Zakoncz();
  }
  vector<int> nastepna;
  for (int s : sasiedzi) {
    // assert((int) Sciezka(s).size() >= 3);
    vector<int> pozostale;
    // IdzZRootaDo(s);
    IdzDoSzybko(s);
    assert(last_result);

    const int a = poprzedni[s];
    assert(1 <= a and a <= n);
    const int b = poprzedni[a];
    assert(1 <= b and b <= n);

    for (int x : reszta) {
      if (Rusz(x)) assert(false);
      if (Rusz(a)) assert(false);
      if (Rusz(b)) {
        if (Rusz(a)) assert(false);
        if (!Rusz(s)) assert(false);
        // nie.
        pozostale.push_back(x);
      } else {
        // tak.
        if (!Rusz(s)) assert(false);
        poprzedni[x] = s;
        nastepna.push_back(x);
      }
    }

    reszta = move(pozostale);

    // IdzDoRootaZ(s);
  }

  FajnaWarstwa(nastepna, reszta);
}

// S * R: 2
void FajnaWarstwa(vector<int> sasiedzi, vector<int> reszta) {
  debug() << "FajnaWarstwa(" imie(sasiedzi) imie(reszta) ")";

  Losuj(sasiedzi);
  Losuj(reszta);

  if (reszta.empty()) {
    return Zakoncz();
  }
  vector<int> nastepna;
  for (int s : sasiedzi) {
    vector<int> pozostale;
    // IdzZRootaDo(s);
    IdzDoSzybko(s);
    assert(!last_result);
    for (int x : reszta) {
      if (Rusz(x)) {
        if (Rusz(s)) assert(false);
        poprzedni[x] = s;
        nastepna.push_back(x);
      } else {
        pozostale.push_back(x);
      }
    }
    reszta = move(pozostale);
    // IdzDoRootaZ(s);
  }
  GlupiaWarstwa(nastepna, reszta);
}

// SYF: 10 * 400 = 4000.
// KONIEC: 6 * 200 * 200 = 240000.
void DalejJuzProsto(int a, vector<int> sasiedzi, vector<int> reszta) {
  debug() << "DalejJuzProsto(" imie(a) imie(sasiedzi) imie(reszta) ")";
  assert((int) sasiedzi.size() >= 2);

  moja_pozycja = a;

  Losuj(sasiedzi);
  Losuj(reszta);

  poprzedni[a] = -1;
  for (int s : sasiedzi) {
    poprzedni[s] = a;
  }

  const int b = sasiedzi[0];
  const int c = sasiedzi[1];

  // Istnienie b-c.
  if (!Rusz(b)) assert(false);
  if (Rusz(c)) assert(false);
  if (Rusz(a)) {
    // b-c.
    return FajnaWarstwa(sasiedzi, reszta);
  }
  // Nie b-c.

  vector<int> zadne;
  vector<pair<int, int>> pary;
  for (int x : reszta) {
    if (!Rusz(c)) assert(false);
    if (Rusz(x)) assert(false);
    if (Rusz(b)) {
      // b-x, c-x.
      pary.emplace_back(b, x);
      if (Rusz(a)) assert(false);
      continue;
    }
    if (Rusz(a)) assert(false);
    if (!Rusz(b)) {
      // c-x.
      pary.emplace_back(c, x);
      if (!Rusz(c)) assert(false);
      if (Rusz(a)) assert(false);
      continue;
    }
    if (Rusz(x)) assert(false);
    if (Rusz(a)) assert(false);
    if (Rusz(c)) {
      // .
      zadne.push_back(x);
      if (Rusz(a)) assert(false);
      continue;
    }
    // b-x.
    pary.emplace_back(b, x);
    if (!Rusz(b)) assert(false);
    if (Rusz(a)) assert(false);
  }
  reszta.clear();
  for (int x : zadne) {
    bool zrobione = false;
    for (int i = 2; i < (int) sasiedzi.size(); i++) {
      const int s = sasiedzi[i];
      if (!Rusz(s)) assert(false);
      if (Rusz(x)) assert(false);
      if (Rusz(a)) assert(false);
      if (!Rusz(b)) {
        // s-x.
        pary.emplace_back(s, x);
        if (!Rusz(s)) assert(false);
        if (Rusz(a)) assert(false);
        zrobione = true;
        break;
      }
      // .
      if (Rusz(a)) assert(false);
    }
    if (!zrobione) {
      reszta.push_back(x);
    }
  }

  vector<int> nowa_warstwa;
  for (const auto& [prev, x] : pary) {
    poprzedni[x] = prev;
    nowa_warstwa.push_back(x);
  }
  FajnaWarstwa(nowa_warstwa, reszta);
}

void MamTylkoWierzcholek(int w, vector<int> reszta);

void MamKrawedz(int a, int b, vector<int> reszta) {
  debug() << "MamKrawedz(" imie(a) imie(b) imie(reszta) ")";

  Losuj(reszta);

  // Sprawia, że a jest kolorowy.
  if (!Rusz(b)) {
    swap(a, b);
  } else {
    if (Rusz(a)) assert(false);
  }
  // a jest kolorowy.

  vector<int> sasiedzi = {b};
  vector<int> pozostale;
  for (int x : reszta) {
    if (Rusz(x)) {
      sasiedzi.push_back(x);
      if (Rusz(a)) assert(false);
    } else {
      pozostale.push_back(x);
    }
  }
  reszta = pozostale;
  assert(!sasiedzi.empty());
  if ((int) sasiedzi.size() == 1) {
    // a-b-(reszta).
    Examine();  // a.
    return MamTylkoWierzcholek(b, reszta);
  }

  dobry[a] = true;
  dobry[b] = true;
  for (int x : sasiedzi) dobry[x] = true;
  for (int x : reszta) dobry[x] = true;

  DalejJuzProsto(a, sasiedzi, reszta);
}

void SzukajKrawedziWCiagu(vector<int> ciag, vector<int> reszta);

// Ciagi: 400 * 401 / 2 = 80200.

void PunktStaly(vector<int> ciag, vector<int> reszta) {
  debug() << "PunktStaly(" imie(ciag) imie(reszta) ")";
  assert((int) ciag.size() > 3);
  for (int i = 2; i < (int) ciag.size(); i++) {
    if (Rusz(ciag[i])) {
      while (i + 1 < (int) ciag.size()) {
        reszta.push_back(ciag.back());
        ciag.pop_back();
      }
      reszta.push_back(ciag[1]);
      ciag.erase(ciag.begin() + 1);
      reverse(ciag.begin(), ciag.end());
      return SzukajKrawedziWCiagu(move(ciag), move(reszta));
    }
  }

  Rusz(ciag[1]);
  Rusz(ciag[0]);
  while ((int) ciag.size() > 2) {
    reszta.push_back(ciag.back());
    ciag.pop_back();
  }

  return MamKrawedz(ciag[0], ciag[1], move(reszta));
}

void SzukajKrawedziWCiagu(vector<int> ciag, vector<int> reszta) {
  assert((int) ciag.size() >= 3);
  if ((int) ciag.size() == 3) {
    reszta.push_back(ciag[2]);
    return MamKrawedz(ciag[0], ciag[1], move(reszta));
  }

  for (int i = 1; i < (int) ciag.size(); i++) {
    if (!Rusz(ciag[i])) {
      continue;
    }
    assert(i + 1 <= (int) ciag.size());
    if (i + 1 == (int) ciag.size()) {
      reverse(ciag.begin(), ciag.end());
      return PunktStaly(move(ciag), move(reszta));
    }
    while (i + 1 < (int) ciag.size()) {
      reszta.push_back(ciag.back());
      ciag.pop_back();
    }
    reverse(ciag.begin(), ciag.end());
    return SzukajKrawedziWCiagu(move(ciag), move(reszta));
  }

  if (!Rusz(ciag[0])) assert(false);
  reszta.push_back(ciag[1]);
  ciag.erase(ciag.begin() + 1);
  return SzukajKrawedziWCiagu(move(ciag), move(reszta));
}

// 400 * 400 = 160000.
void SzukajKrawedzi(int w, vector<int> reszta) {
  assert((int) reszta.size() >= 2);

  for (int i = 0; i < (int) reszta.size(); i++) {
    for (int j = 0; j < (int) reszta.size(); j++) {
      const int x = reszta[j];
      if (Rusz(x)) {
        vector<int> mam = {w};
        mam.insert(mam.end(), reszta.begin(), reszta.begin() + j + 1);
        vector<int> pozostale(reszta.begin() + j + 1, reszta.end());
        reverse(mam.begin(), mam.end());
        return SzukajKrawedziWCiagu(move(mam), move(pozostale));
      }
    }
    if (!Rusz(w)) assert(false);
    rotate(reszta.begin(), reszta.begin() + 1, reszta.end());
  }

  // Gwiazda o środku w w.
  Examine();
  for (int x : reszta) {
    Rusz(x);
    Examine();
    Rusz(w);
  }
  // Już powinno być zaakceptowane.
  assert(false);
}

void MamTylkoWierzcholek(int w, vector<int> reszta) {
  if (reszta.empty()) {
    Examine();
    assert(false);
  }
  if ((int) reszta.size() == 1) {
    Examine();
    Rusz(reszta[0]);
    Examine();
    assert(false);
  }

  SzukajKrawedzi(w, move(reszta));
}

int main() {
  n = GetN();
  vector<int> reszta;
  for (int i = 2; i <= n; i++) {
    reszta.push_back(i);
  }
  Losuj(reszta);
  MamTylkoWierzcholek(1, move(reszta));
  assert(false);
  return 0;
}