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
#include "sonlib.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 410;
vector<int> paths[MAXN];
int prec[MAXN];

int main() {
    int n = GetN();
    int s = GetSubtask();

    if (s == 3) {
        if (n == 4) {
            Examine();
            MoveProbe(2);
            if (MoveProbe(3)) {
                Examine();
                MoveProbe(2);
                Examine();
                MoveProbe(3);
                MoveProbe(4);
                Examine();
            }
            else {
                MoveProbe(4);
                Examine();
                MoveProbe(2);
                Examine();
                MoveProbe(4);
                MoveProbe(3);
                Examine();
            }
        }

        srand(62236718);
        vector<int> *perms = new vector<int>[n + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++)
                if (i != j) perms[i].push_back(j);
            random_shuffle(perms[i].begin(), perms[i].end());
        }

        int v = 1;

        int *nxt = new int[n + 1];
        int prevv = 1;
        for (int i = 1; i <= n; i++) nxt[i] = -1;
        bool mod = n & 1;
        for (int i = 1; i <= (mod ? n : n / 2); i++) {
            Examine();
            if (mod && i == n) return 0;
            int nv = 0;
            for (int j = 0; j < 2 * (int)perms[v].size(); j++) {
                int x = perms[v][j % perms[v].size()];
                if (MoveProbe(x)) {
                    if (x == prevv) reverse(perms[v].begin(), perms[v].end());
                    else nxt[v] = j;
                    nv = x;
                    break;
                }
            }

            if (nv == prevv) {
                for (int $ = 0; $ < 2; $++) {
                    for (int j = 0; j < 2 * (int)perms[nv].size(); j++) {
                        int x = perms[nv][j % perms[nv].size()];
                        if (MoveProbe(x)) {
                            if ($ == 1) nxt[v] = j;
                            nv = x;
                            break;
                        }
                    }
                }
            }
            prevv = v;
            v = nv;
        }

        if (!mod) {
            for (int i = 1; i <= n / 2; i++) {
                for (int j = 0; j < nxt[v]; j++) MoveProbe(perms[v][j % perms[v].size()]);
                Examine();
                if (i == n / 2) return 0;
                MoveProbe(perms[v][nxt[v] % perms[v].size()]);
                v = perms[v][nxt[v] % perms[v].size()];
            }
        }
    }
    else if (s == 1) {
        bool *vis = new bool[n + 1];
        for (int i = 1; i <= n; i++) vis[i] = false;
        vis[1] = 1;
        Examine();
        bool git = true;
        while (git) {
            git = false;
            for (int x = 1; x <= n; x++) if (!vis[x]) {
                if (MoveProbe(x)) {
                    git = true;
                    vis[x] = true;
                    Examine();
                    break;
                }
            }
        }
        MoveProbe(1);
        git = true;
        while (git) {
            git = false;
            for (int x = n; x >= 1; x--) if (!vis[x]) {
                if (MoveProbe(x)) {
                    git = true;
                    vis[x] = true;
                    Examine();
                    break;
                }
            }
        }
        MoveProbe(1);
        for (int i = 1; i <= n; i++) if (!vis[i]) {
            MoveProbe(i);
            Examine();
            MoveProbe(1);
        }
    }
    else {
        queue<int> q;
        q.push(1);
        q.push(2);
        q.push(3);
        paths[2].push_back(2);
        paths[3].push_back(3);
        bool *visited = new bool[n + 1];
        for (int i = 0; i <= n; i++) visited[i] = false;
        visited[1] = true;
        visited[2] = true;
        visited[3] = true;
        Examine();
        MoveProbe(2);
        Examine();
        MoveProbe(3);
        Examine();
        MoveProbe(1);
        int pr = 1;
        int visc = 3;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            for (int t = 1; t <= n; t++) if (!visited[t]) {
                if ((paths[x].size() + pr) % 2 == 0) {
                    pr = (1 + pr) % 2;
                    MoveProbe(2);
                    MoveProbe(3);
                    MoveProbe(1);
                }
                for (int pt : paths[x]) MoveProbe(pt);
                pr = (pr + paths[x].size()) % 2;
                int ret = 0;
                while (ret == 0 && t <= n) {
                    if (!visited[t]) {
                        if (MoveProbe(t)) {
                            ret = 1;
                            visc++;
                            visited[t] = true;
                            q.push(t);
                            prec[t] = x;
                            for (int pt : paths[x]) paths[t].push_back(pt);
                            paths[t].push_back(t);

                            Examine();
                            if (visc == n) return 0;
                            MoveProbe(x);
                        }
                        else t++;
                    }
                    else t++;
                }
                if (x != 1) {
                    pr = (pr + paths[x].size()) % 2;
                    for (int i = (int)paths[x].size() - 2; i >= 0; i--) MoveProbe(paths[x][i]);
                    MoveProbe(1);
                }
            }
        }
    }
}