voidmerge(int u, int v){ u = find(u), v = find(v); if (u == v) return; if (siz[u] > siz[v]) std::swap(u, v); fa[u] = v, siz[v] += siz[u]; U[++top] = u, V[top] = v; } } // DSU usingnamespace DSU;
voidmark(int x, int y){ vis[x].set(y); for (int k = 0, p = pos(x, y), nx, ny; k < 8; ++k) { nx = x + dx[k], ny = y + dy[k]; if (nx < 1 || nx > N) continue; if (ny < 1) ny = M << 1; if (ny > M << 1) ny = 1; if (vis[nx][ny]) merge(p, pos(nx, ny)); } }
intmain(){ cin >> N >> M >> K; for (int i = 1; i <= N * M << 1; ++i) fa[i] = i, siz[i] = 1; // 初始化! for (int x, y; K; --K) { top = 0; cin >> x >> y; mark(x, y), mark(x, y + M); if (find(pos(x, y)) == find(pos(x, y + M))) { vis[x].reset(y), vis[x].reset(y + M); do { siz[V[top]] -= siz[U[top]]; fa[U[top]] = U[top]; } while (--top); } else ++ans; } printf("%d", ans); return0; }
std::bitset<2013> ex, vis; int N, M, K, S, T, tot; iarrN cur, dep, head;
structEdge { int nxt, to, w; } e[6000003];
void _aE(int u, int v, int w); voidaddEdge(int u, int v, int w);
voidinit(){ tot = 1, S = 0, T = N + M + 1; ex.reset(), vis.reset(); memset(head, 0, (N + M + 2) << 2);
for (int i = 1, x, y; i <= K; ++i) { cin >> x >> y; addEdge(x, y + N, inf); if (!ex[x]) addEdge(S, x, 1), ex.set(x); if (!ex[y + N]) addEdge(y + N, T, 1), ex.set(y + N); } }
/* dinic: returns maxflow */
voidmark(int u){ // dfs vis.set(u); for (int i = head[u]; i; i = e[i].nxt) if (e[i].w && !vis[e[i].to]) mark(e[i].to); }
intmain(){ while (cin >> N >> M >> K, N) { init(); std::cout << dinic(S, T); mark(S);
for (int i = 1; i <= N; ++i) if (ex[i] && !vis[i]) std::cout << " r" << i; for (int i = N + 1; i <= N + M; ++i) if (ex[i] && vis[i]) std::cout << " c" << i - N; std::cout << '\n'; } return0; }
int N, K, tot; iarrN head, siz, a, b; double t[103], f[103][103];
structEdge { int nxt, to; } e[203];
voidaddEdge(int u, int v);
voiddfs(int u, int fa){ f[u][0] = 0, siz[u] = 1; for (int i = head[u], v; i; i = e[i].nxt) { if ((v = e[i].to) == fa) continue; dfs(v, u); siz[u] += siz[v]; for (int j = std::min(siz[u], K); j; --j) for (int k = 0; k <= std::min(siz[v], j); ++k) f[u][j] = std::max(f[u][j], f[u][j - k] + f[v][k]); } for (int i = std::min(siz[u], K); i; --i) f[u][i] = f[u][i - 1] + t[u]; }
boolcheck(double mid){ for (int i = 1; i <= N; ++i) { t[i] = a[i] - mid * b[i]; for (int j = 1; j <= K; ++j) f[i][j] = -inf; } dfs(1, 0); for (int i = 1; i <= N; ++i) if (f[i][K] > -eps) return1; return0; }
intmain(){ cin >> N >> K; K = N - K; for (int i = 1; i <= N; ++i) cin >> a[i]; for (int i = 1; i <= N; ++i) cin >> b[i]; for (int i = 1, u, v; i < N; ++i) { cin >> u >> v; addEdge(u, v), addEdge(v, u); }
double l = 0, r = 10000; for (double mid; r - l > eps; ) { mid = (l + r) / 2; if (check(mid)) l = mid; else r = mid; } printf("%.1lf", r); return0; }