#include "cave.h"
#include <vector>
using namespace std;
#define MAXN 5003
static int N,S[MAXN],D[MAXN];
static bool find[MAXN];
static vector <int> sw;
int dfs(int s,int e,int org,int door)
{
if (s == e) return sw[s];
int i,m=(s+e)>>1,k;
for (i=s;i<=m;i++) S[sw[i]] ^= 1;
k = tryCombination(S);
if ((org==door) != (k==door)) return dfs(s,m,k,door);
else return dfs(m+1,e,k,door);
}
void exploreCave(int n)
{
int i,j,k;
N = n;
for (i=0;i<N;i++){
sw.clear();
for (j=0;j<N;j++) if (!find[j]) sw.push_back(j);
n = sw.size();
k = tryCombination(S);
k = dfs(0,n-1,k,i);
find[k] = 1; D[k] = i;
n = tryCombination(S);
if (n == i) S[k] ^= 1;
}
answer(S,D);
}
I2luY2x1ZGUgImNhdmUuaCIKI2luY2x1ZGUgPHZlY3Rvcj4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgTUFYTiA1MDAzCgpzdGF0aWMgaW50IE4sU1tNQVhOXSxEW01BWE5dOwpzdGF0aWMgYm9vbCBmaW5kW01BWE5dOwpzdGF0aWMgdmVjdG9yIDxpbnQ+IHN3OwoKaW50IGRmcyhpbnQgcyxpbnQgZSxpbnQgb3JnLGludCBkb29yKQp7CiAgICBpZiAocyA9PSBlKSByZXR1cm4gc3dbc107CiAgICBpbnQgaSxtPShzK2UpPj4xLGs7CiAgICBmb3IgKGk9cztpPD1tO2krKykgU1tzd1tpXV0gXj0gMTsKICAgIGsgPSB0cnlDb21iaW5hdGlvbihTKTsKICAgIGlmICgob3JnPT1kb29yKSAhPSAoaz09ZG9vcikpIHJldHVybiBkZnMocyxtLGssZG9vcik7CiAgICBlbHNlIHJldHVybiBkZnMobSsxLGUsayxkb29yKTsKfQoKdm9pZCBleHBsb3JlQ2F2ZShpbnQgbikKewogICAgaW50IGksaixrOwogICAgTiA9IG47CiAgICBmb3IgKGk9MDtpPE47aSsrKXsKICAgICAgICBzdy5jbGVhcigpOwogICAgICAgIGZvciAoaj0wO2o8TjtqKyspIGlmICghZmluZFtqXSkgc3cucHVzaF9iYWNrKGopOwogICAgICAgIG4gPSBzdy5zaXplKCk7CiAgICAgICAgayA9IHRyeUNvbWJpbmF0aW9uKFMpOwogICAgICAgIGsgPSBkZnMoMCxuLTEsayxpKTsKICAgICAgICBmaW5kW2tdID0gMTsgRFtrXSA9IGk7CiAgICAgICAgbiA9IHRyeUNvbWJpbmF0aW9uKFMpOwogICAgICAgIGlmIChuID09IGkpIFNba10gXj0gMTsKICAgIH0KICAgIGFuc3dlcihTLEQpOwp9Cg==