#include <bits/stdc++.h> #include "sonlib.h" using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define pb pop_back #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define Sim template<class n Sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} Sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} Sim, class s> void mini(n &p, s y) {if(p>y) p = y;} Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " = ", (x), " " #define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " " const int M = 512; int n, vis[M], prev[M], done[M], licznik; VI V[M]; void addedge(int a, int b) { V[a].PB(b); V[b].PB(a); } int MoveProbe2(int x) { licznik++; return MoveProbe(x); } void Exam(int v) { vis[v] = 1; Examine(); for (auto x : V[v]) { if (!vis[x]) { MoveProbe2(x); Exam(x); MoveProbe2(v); } } } unordered_set<int> secik[M][M]; VI getPath2(int p, int k) { int x = rand() % n + 1; while (x == p || x == k || secik[p][k].count(x)) { x = rand() % n + 1; } secik[p][k].insert(x); MoveProbe2(x); if (MoveProbe2(p)) { MoveProbe2(x); return {p, x, k}; } VI tmp; FOR(i, 1, n) { if (i != p && i != k && i != x) { tmp.PB(i); } } random_shuffle(ALL(tmp)); for (auto qwe : tmp) { if (MoveProbe2(qwe)) { return getPath2(k, qwe); } } MoveProbe2(k); return getPath2(p, k); } VI getPath() { VI todo; FOR(i, 2, n) { todo.PB(i); } random_shuffle(ALL(todo)); FOR(i, 0, SIZ(todo) - 1) { FOR(j, 0, n - 2) { if (MoveProbe2(todo[(i + j) % SIZ(todo)])) { return getPath2(1, todo[(i + j) % SIZ(todo)]); } } MoveProbe2(1); } FOR(i, 2, n) { addedge(1, i); } Exam(1); } VI tmp; int vis3[M], last[M]; void DFS(int v, int dep, int par, int par2) { vis3[v] = 1; if (!dep) { FOR(i, 1, n) { if (done[i]) { continue; } if (MoveProbe2(i)) { done[i] = 1; addedge(v, i); MoveProbe2(v); } } } else { FOR(i, 1, n) { if (done[i]) { continue; } MoveProbe2(i); if (MoveProbe2(par2)) { addedge(v, i); done[i] = 1; MoveProbe2(i); MoveProbe2(v); continue; } if (MoveProbe2(par)) { MoveProbe2(par2); MoveProbe2(v); continue; } if (MoveProbe2(par2)) { MoveProbe2(par); MoveProbe2(v); } else { done[i] = 1; addedge(v, i); MoveProbe2(v); } } } FOR(i, 0, SIZ(V[v]) - 1) { if (V[v][i] != par && !vis3[V[v][i]]) { MoveProbe2(V[v][i]); if (par != 0) { DFS(V[v][i], 1 - dep, v, par); } else { assert(SIZ(V[v]) > 1); DFS(V[v][i], 1 - dep, v, V[v][(i + 1) % SIZ(V[v])]); } MoveProbe2(v); } } } int main() { srand(69); n = GetN(); VI X = getPath(); addedge(X[0], X[1]); addedge(X[1], X[2]); for (auto x : X) { done[x] = 1; } DFS(X[1], 0, 0, 0); Exam(X[1]); }
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 | #include <bits/stdc++.h> #include "sonlib.h" using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define pb pop_back #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define Sim template<class n Sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} Sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} Sim, class s> void mini(n &p, s y) {if(p>y) p = y;} Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " = ", (x), " " #define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " " const int M = 512; int n, vis[M], prev[M], done[M], licznik; VI V[M]; void addedge(int a, int b) { V[a].PB(b); V[b].PB(a); } int MoveProbe2(int x) { licznik++; return MoveProbe(x); } void Exam(int v) { vis[v] = 1; Examine(); for (auto x : V[v]) { if (!vis[x]) { MoveProbe2(x); Exam(x); MoveProbe2(v); } } } unordered_set<int> secik[M][M]; VI getPath2(int p, int k) { int x = rand() % n + 1; while (x == p || x == k || secik[p][k].count(x)) { x = rand() % n + 1; } secik[p][k].insert(x); MoveProbe2(x); if (MoveProbe2(p)) { MoveProbe2(x); return {p, x, k}; } VI tmp; FOR(i, 1, n) { if (i != p && i != k && i != x) { tmp.PB(i); } } random_shuffle(ALL(tmp)); for (auto qwe : tmp) { if (MoveProbe2(qwe)) { return getPath2(k, qwe); } } MoveProbe2(k); return getPath2(p, k); } VI getPath() { VI todo; FOR(i, 2, n) { todo.PB(i); } random_shuffle(ALL(todo)); FOR(i, 0, SIZ(todo) - 1) { FOR(j, 0, n - 2) { if (MoveProbe2(todo[(i + j) % SIZ(todo)])) { return getPath2(1, todo[(i + j) % SIZ(todo)]); } } MoveProbe2(1); } FOR(i, 2, n) { addedge(1, i); } Exam(1); } VI tmp; int vis3[M], last[M]; void DFS(int v, int dep, int par, int par2) { vis3[v] = 1; if (!dep) { FOR(i, 1, n) { if (done[i]) { continue; } if (MoveProbe2(i)) { done[i] = 1; addedge(v, i); MoveProbe2(v); } } } else { FOR(i, 1, n) { if (done[i]) { continue; } MoveProbe2(i); if (MoveProbe2(par2)) { addedge(v, i); done[i] = 1; MoveProbe2(i); MoveProbe2(v); continue; } if (MoveProbe2(par)) { MoveProbe2(par2); MoveProbe2(v); continue; } if (MoveProbe2(par2)) { MoveProbe2(par); MoveProbe2(v); } else { done[i] = 1; addedge(v, i); MoveProbe2(v); } } } FOR(i, 0, SIZ(V[v]) - 1) { if (V[v][i] != par && !vis3[V[v][i]]) { MoveProbe2(V[v][i]); if (par != 0) { DFS(V[v][i], 1 - dep, v, par); } else { assert(SIZ(V[v]) > 1); DFS(V[v][i], 1 - dep, v, V[v][(i + 1) % SIZ(V[v])]); } MoveProbe2(v); } } } int main() { srand(69); n = GetN(); VI X = getPath(); addedge(X[0], X[1]); addedge(X[1], X[2]); for (auto x : X) { done[x] = 1; } DFS(X[1], 0, 0, 0); Exam(X[1]); } |