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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

// Przykładowe niepoprawne rozwiązanie do zadania Sonda.
// Próbujemy przejść graf w kolejności 1->2->3->4->...->n.
#include "sonlib.h"

const int MR = 410;
const int LIM = 2000;

int g[MR][MR], n, v, done[MR], cnt;

bool isClear(int nr)
{
	FORQ(i, 1, n)
		if (!g[nr][i])
			return 0;

	return 1;
}

bool isMixed(int nr)
{
	bool unknown = 0, known = 0;
	FORQ(i, 1, n)
		if (!g[nr][i])
			unknown = 1;
		else if (g[nr][i] == 1)
			known = 1;

	return unknown && known;
}

bool makeMove(int prev, int nr, bool known)
{
	int res = MoveProbe(nr);
	if (!known)
	{
		// mv % 2 == 0
		if (res)
		{
			g[prev][nr] = g[nr][prev] = 1;
			v = nr;
			return 1;
		}
		
		g[prev][nr] = g[nr][prev] = -1;
		return 0;
	}
	
	v = nr;
	return 1;
}

vector<int> V, path;
int dest;
int prv[MR];
void bfs(int nr)
{
	queue<int> Q;
	Q.push(nr);
	done[nr] = cnt;
	prv[nr] = -1;

	while (!Q.empty())
	{
		int akt = Q.front(); Q.pop();
		FORQ(i, 1, n)
			if (g[akt][i] == 1 && done[i] != cnt)
			{
				done[i] = cnt;
				prv[i] = akt;
				Q.push(i);
			}
	}

	path.clear();

	int akt = dest;
	while (akt != -1)
	{
		path.push_back(akt);
		akt = prv[akt];
	}

	reverse(path.begin(), path.end());
}

//void dfs(int nr)
//{
//	done[nr] = cnt;
//	V.push_back(nr);
//	if (nr == dest)
//		path = V;
//
//	FORQ(i, 1, n)
//		if (g[nr][i] == 1 && done[i] != cnt)
//			dfs(i);
//
//	V.pop_back();
//}

void go(int nr)
{
	done[nr] = cnt;
	Examine();
	
	FORQ(i, 1, n)
		if (g[nr][i] == 1 && done[i] != cnt)
		{
			makeMove(nr, i, 1);
			go(i);
			makeMove(i, nr, 1);
		}
}

int main() {
	n = GetN();
	FORQ(i, 1, n)
		g[i][i] = -1;

	// gdzie znajduje sie Sondka
	v = 1;

	int s = GetSubtask();

	// to mozemy zrobic - odznacz istniejace polaczenia
	g[1][2] = g[2][1] = g[1][3] = g[3][1] = g[2][3] = g[3][2] = 1;

	while (true)
	{
		// spr czy wszystkie czyste
		bool allClear = 1;
		FORQ(i, 1, n)
			if (!isClear(i))
			{
				allClear = 0;
				break;
			}
		if (allClear)
			break;

		// znajdz wierzch, ktory ma niejasne i jasne krawedzie
		// powinnismy dojsc do niego w Nparzystej liczbie ruchow
		// czyli albo dojdz do niego, albo zrob kolko 1->2->3->1 i idz
		FORQ(i, 1, n)
			if (isMixed(i))
			{
				dest = i;
				break;
			}

		// wyznacz path do dest
		cnt++;
		bfs(1);
		if (path.size() & 1)
		{
			// dodaj kolko
			reverse(path.begin(), path.end());
			path.push_back(3);
			path.push_back(2);
			path.push_back(1);
			reverse(path.begin(), path.end());
		}

		// idz po Nparzystej liczbie ruchow do dest
		FOR(i, 1, path.size())
			makeMove(path[i - 1], path[i], 1);

		// teraz jestes w dest
		// wykonaj ruch do nieznajomego
		int mv = 0;
		FORQ(i, 1, n)
			if (!g[dest][i])
			{
				mv = 1 - makeMove(dest, i, 0);
				break;
			}

		// wroc z powrotem do 1 w parzystej/Nparzystej liczbie ruchow - zalezy od testu
		dest = 1;
		cnt++;
		bfs(v);

		if (path.size() % 2 == mv)
		{
			// dodaj kolko
			path.push_back(3);
			path.push_back(2);
			path.push_back(1);
		}

		FOR(i, 1, path.size())
			makeMove(path[i - 1], path[i], 1);
	}

	// teraz mamy caly graf
	cnt++;
	go(1);
	
	return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}