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
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define MAX(dst,src) dst = max(dst, (src))
#define MIN(dst,src) dst = min(dst, (src))

#define pb push_back
#define mp make_pair
#define st first
#define nd second

#include "sonlib.h"

bool debug = false;

int N;

int T = -1;
vector<int> history;
vector<bool> confirmed;
vector<int> lastVisit(500,-1);

void updateHistory(int k, bool result) {
  history.pb(k);
  ++T;
  confirmed.pb(result);
  lastVisit[k] = T;
}

int P[500];
bool move(int k) {
  bool result = MoveProbe(P[k]);
  if (debug) printf("Move to %d(%d)\n", k, result);
  updateHistory(k, result);
  return result;
}

void star(int v) {
  FOR(i,1,N+1) if (i != v) {
    move(i);
    Examine();
    move(v);
  }
  Examine();
}

int confirmZero() {
    vector<int> edgeFrom(N+1,-1);
    vector<PII> goodSegment(N+1, {-1, -1});

    while (true) {
      int v = history[T];
      if (edgeFrom[v] > -1) {
        move(edgeFrom[v]);
        return edgeFrom[v];
      }
      if (T >= 2 && confirmed[T-2]) {
        move(history[T-1]);
        return history[T-2];
      }

      int S = T;
      auto seg = goodSegment[v];
      if (seg.st > -1) {
        if (debug) printf("Reproducing [%d %d]\n", seg.st, seg.nd);
        bool reproduced = true;
        FOR(i,seg.st+2,seg.nd+1) {
          int result = move(history[i]);
          if (result != confirmed[i]) {
            reproduced = false;
            break;
          }
        }
        if (reproduced) {
          goodSegment[v] = {S, T};
        } else {
          edgeFrom[v] = history[seg.st+1];
          edgeFrom[history[seg.nd]] = history[seg.st+1];

          if (!confirmed[T]) {
            FOR(i,1,N+1) if (lastVisit[i] < S && move(i)) break;
            if (!confirmed[T]) assert(move(v));
          }
        }
      } else {
        FOR(u,1,N+1) if (u != v) {
          move(u);
          FOR(x,1,N+1) if (x != u && x != v) {
            if (move(x)) {
              goodSegment[v] = {S, T};
              goto done;
            }
          }
          assert(move(v));
          S = T;
        }
        star(v);
        done:;
      }
    }
}

vector<int> adj[500];

bool discovered[500];
void discover(int v) {
  if (discovered[v]) return;
  if (debug) printf("Examining %d\n", v);
  Examine();
  discovered[v] = true;
}

void discoverGraph(int r) {
  discover(r);

  vector<int> children;
  FOR(i,1,N+1) if (!discovered[i]) {
    if (move(i)) {
      discover(i);

      adj[i].pb(r);
      adj[r].pb(i);
      children.pb(i);
      move(r);
    }
  }
  if (!children.size()) {
    return;
  }

  PII triangle = {-1, -1};

  for (auto c: children) {
    int other_child = adj[r][0] == c ? adj[r][1] : adj[r][0];
    assert(adj[r].size() > 1);

    if (triangle.st == -1) {
      assert(move(c));
      move(other_child);
      if (move(r)) {
        triangle = { c, other_child };
      }
    }

    if (triangle.st > -1) {
      move(c);
      discoverGraph(c);
      assert(move(r));
      continue;
    }

    assert(move(c));
    FOR(i,1,N+1) if (!discovered[i]) {
      move(i);
      if (move(other_child)) {
        // We moved to i and other_child, both had to be successful
        adj[c].pb(i);
        adj[i].pb(c);
        move(i);
        discoverGraph(i);
        assert(move(c));
        continue;
      }

      assert(!move(r));

      // Exactly one move succeeded;
      // Coudn't be other_child because then r would succeed too. So we're either in i or r.
      // Let's see if we're able to move to other_child
      if (!move(other_child)) {
        // Yup, we're in i
        adj[c].pb(i);
        adj[i].pb(c);
        discoverGraph(i);
        assert(move(c));
      } else {
        move(r);
        assert(move(c));
      }
    }

    move(r);
  }

  if (triangle.st > -1) {
    move(triangle.st);
    assert(move(triangle.nd));
    move(r);
  }
}

int main(int argc, char* argv[]) {
  N = GetN();
  FOR(i,1,N+1) P[i] = i;
  srand(N);
  random_shuffle(P+2,P+N+1);
  updateHistory(1, true);
  int r = confirmZero();
  discoverGraph(r);
  exit(1);
}