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
| #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <cmath> #include <queue> #include <ctime> #include "perm.h"
using namespace std;
const int INF = 0x7fffffff;
namespace graph { const int MAXN = 800 + 10; const int MAXM = MAXN * MAXN;
struct LFS { int to, next; } ; LFS Link[MAXM]; int Head[MAXN], size = 0; void Insert (int u, int v) { Link[++ size].to = v; Link[size].next = Head[u];
Head[u] = size; }
int N; int dfn[MAXN], low[MAXN], bel[MAXN], ord = 0, bcc = 0; void init () { memset (Head, 0, sizeof (Head)), size = ord = bcc = 0; for (int i = 1; i <= N; i ++) dfn[i] = low[i] = bel[i] = 0; } int stack[MAXN], top = 0; bool insta[MAXN]= {false}; void Tarjan (int u) { dfn[u] = low[u] = ++ ord; stack[++ top] = u, insta[u] = true; for (int i = Head[u]; i; i = Link[i].next) { int v = Link[i].to; if (! dfn[v]) { Tarjan (v); low[u] = min (low[u], low[v]); } else if (insta[v]) low[u] = min (low[u], dfn[v]); } if (low[u] == dfn[u]) { bcc ++; int tp; do { tp = stack[top --]; bel[tp] = bcc, insta[tp] = false; } while (tp != u); } } void work () { for (int i = 1; i <= N; i ++) if (! dfn[i]) Tarjan (i); } } ; bool con[1000]= {false}; namespace net { const int MAXN = 800 + 10; const int MAXM = MAXN * MAXN;
struct LFS { int to, cap, next; } ; LFS Link[MAXM]; int Head[MAXN]= {0}, size; void Insert (int u, int v, int cap) { Link[++ size].to = v; Link[size].cap = cap; Link[size].next = Head[u];
Head[u] = size; } void addedge (int u, int v, int cap) { Insert (u, v, cap), Insert (v, u, 0); }
int n, N, S, T; void init () { memset (Head, 0, sizeof (Head)), size = 1; } queue<int> que; int depth[MAXN]; bool BFS () { while (! que.empty()) que.pop(); memset (depth, 0, sizeof (depth)); depth[S] = 1, que.push(S); while (! que.empty()) { int u = que.front(); que.pop(); for (int i = Head[u]; i; i = Link[i].next) { int v = Link[i].to, cap = Link[i].cap; if (depth[v] || ! cap) continue; depth[v] = depth[u] + 1; if (v == T) return true; que.push(v); } } return false; } int cur[MAXM]; int Dinic (int u, int flow) { if (u == T) return flow; int rest = flow; for (int& i = cur[u]; i && rest; i = Link[i].next) { int v = Link[i].to, cap = Link[i].cap; if (depth[v] != depth[u] + 1 || ! cap) continue; int k = Dinic (v, min (rest, cap)); if (! k) depth[v] = - 1; Link[i].cap -= k, Link[i ^ 1].cap += k; rest -= k; } return flow - rest; } int maxflow () { int ret = 0; while (BFS ()) { for (int i = 1; i <= N; i ++) cur[i] = Head[i]; ret += Dinic (S, INF); } return ret; } int match[MAXN]; bool check () { graph::N = 2 * n, graph::init (); for (int i = 1; i <= n; i ++) for (int j = Head[i]; j; j = Link[j].next) { int v = Link[j].to, cap = Link[j].cap; if (! cap) graph::Insert (i, match[i] = v); else graph::Insert (v, i); } graph::work (); bool suc = true; for (int i = 1; i <= n; i ++) { if (graph::bel[i] == graph::bel[match[i]]) suc = false; else con[i] = true; } return suc; } } ;
const int MAXN = 400 + 10;
bool ext[MAXN][MAXN]; int N, S, B = 10; vector<int> trans (vector<int> p) { vector<int> ret; ret.resize(N); for (int i = 0; i < N; i ++) ret[p[i] - 1] = i + 1; return ret; } vector<int> ask (vector<int> p) { return query (trans (p)); } vector<int> q[11], r, rkc, rkd, rk; vector<int> qq[11], qr, f1, f2; vector<int> ret; int cnt1[MAXN]= {0}, cnt2[MAXN]= {0}; void build () { net::init (), net::n = N, net::N = 2 * N + 2; net::S = 2 * N + 1, net::T = 2 * N + 2; for (int i = 1; i <= N; i ++) for (int j = 1; j <= N; j ++) if (ext[i][j]) net::addedge (i, j + N, 1); for (int i = 1; i <= N; i ++) net::addedge (net::S, i, 1); for (int i = 1; i <= N; i ++) net::addedge (i + N, net::T, 1); }
int vis1[MAXN], vis2[MAXN]; bool work () { int p = rand () % B + 1; rkc.clear(); for (int i = 0; i < N; i ++) if (! con[i + 1]) rkc.push_back(i); random_shuffle (rkc.begin(), rkc.end()); rkd.clear(); for (int i = 0; i < N; i ++) if (con[i + 1]) rkd.push_back(i); random_shuffle (rkd.begin(), rkd.end()); for (int i = 0; i < (int) rkd.size(); i ++) rkc.push_back(rkd[i]); rk.clear(); for (int i = 0; i < S; i ++) rk.push_back(rkc[i]); for (int i = 1; i < S; i ++) swap (rk[i], rk[rand () % i]); r.resize(N); for (int i = 0; i < N; i ++) r[i] = q[p][i]; for (int i = 0; i < S; i ++) r[rkc[i]] = q[p][rk[i]]; qr = ask (r); for (int i = 0; i < N; i ++) cnt1[i] = cnt2[i] = 0; for (int i = 0; i < N; i ++) cnt1[qq[p][i]] ++, cnt2[qr[i]] ++; f1.clear(), f2.clear(); for (int i = 0; i < N; i ++) { while (cnt1[i] > cnt2[i]) f1.push_back(i), cnt1[i] --; while (cnt1[i] < cnt2[i]) f2.push_back(i), cnt1[i] ++; } if ((int) f1.size() != S || (int) f2.size() != S) return false; for (int i = 0; i < S; i ++) { int x1 = q[p][rkc[i]], x2 = r[rkc[i]]; for (int i = 1; i <= N; i ++) vis1[i] = vis2[i] = 0; for (int j = 0; j < S; j ++) { if (x1 - f1[j] >= 1) vis1[x1 - f1[j]] = 1; if (x1 + f1[j] <= N) vis1[x1 + f1[j]] = 1; if (x2 - f2[j] >= 1) vis2[x2 - f2[j]] = 1; if (x2 + f2[j] <= N) vis2[x2 + f2[j]] = 1; } for (int j = 1; j <= N; j ++) if (! vis1[j] || ! vis2[j]) ext[rkc[i] + 1][j] = false; } build (); int flow = net::maxflow (); if (flow < N) return false; if (! net::check ()) return false; for (int i = 1; i <= N; i ++) ret[net::match[i] - N - 1] = i; return true; } vector<int> guess (int pn){ N = pn; if (N <= 3) { for (int i = 1; i <= N; i ++) ret.push_back(i); while (query (ret).back()) next_permutation (ret.begin(), ret.end()); return ret; } srand (time (NULL)); S = sqrt (N) / 2; S = max (S, 2); for (int i = 1; i <= B; i ++) { for (int j = 1; j <= N; j ++) q[i].push_back(j); random_shuffle(q[i].begin(), q[i].end()); qq[i] = ask (q[i]); } for (int i = 1; i <= N; i ++) for (int j = 1; j <= N; j ++) ext[i][j] = true; ret.resize(N); while (! work ()); return ret; }
|