题意
给定一棵大小为 \(n\) 的树,有 \(k\) 个人,第 \(i\) 个人在节点 \(a_i\)。
从第 \(1\) 秒开始,依次操作第 \(1, 2, 3, \ldots, k, 1, 2, 3, \ldots, k, \ldots, k, \ldots\) 个人,把这个人移动到没有走过的点。
Sol
调了 \(3h\),给哥们整吐了。
不难想到二分答案时间,算出每个人走了多少步。
然后考虑如何 \(check\)。
记 \(f_i\) 表示当前节点往下还可以走多远。
\(g_i\) 表示当前节点需要往上走多远。
不难发现:\(f_x = \max f_v + 1\)。
注意到如果有多个节点需要往上走,或者当前节点也需要走,那么不合法。
把这几个条件判完就过了。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <bitset>
#include <assert.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
const int N = 2e5 + 5, M = 4e5 + 5;
namespace G {
array <int, N> fir;
array <int, M> nex, to;
int cnt = 1;
void add(int x, int y) {
cnt++;
nex[cnt] = fir[x];
to[cnt] = y;
fir[x] = cnt;
}
}
bitset <N> vis;
array <int, N> dis, f, g;
bool dfs(int x, int fa) {
int res = -1;
if (vis[x])
res = dis[x];
for (int i = G::fir[x]; i; i = G::nex[i]) {
if (G::to[i] == fa) continue;
bool tp = dfs(G::to[i], x);
if (!tp) return tp;
if (!vis[G::to[i]]) {
f[x] = max(f[x], f[G::to[i]] + 1);
continue;
}
if (~res && g[G::to[i]]) return false;
else if (g[G::to[i]]) res = g[G::to[i]] - 1;
}
if (!~res) return true;
vis[x] = 1;
if (res > f[x]) g[x] = res;
else g[x] = 0;
return (x == 1 ? !g[x] : 1);
}
array <int, N> idx;
bool check(int x, int n, int k) {
for (int i = 1; i <= n; i++)
dis[i] = vis[i] = f[i] = g[i] = 0;
for (int i = 1; i <= k; i++)
dis[idx[i]] = x / k, vis[idx[i]] = 1;
for (int i = 1; i <= x % k; i++)
dis[idx[i]]++;
return dfs(1, 0);
}
void solve() {
int n = read();
for (int i = 1; i <= n; i++)
G::fir[i] = 0;
G::cnt = 1;
for (int i = 2; i <= n; i++) {
int x = read(), y = read();
G::add(x, y), G::add(y, x);
}
int k = read();
for (int i = 1; i <= k; i++)
idx[i] = read();
int l = 0, r = n + 1;
int ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid, n, k)) ans = mid, l = mid + 1;
else r = mid - 1;
}
write(ans), puts("");
}
int main() {
int T = read();
while (T--) solve();
return 0;
}
标签:fir,cnt,return,int,res,CF1795F,include,Chips,Blocking
From: https://www.cnblogs.com/cxqghzj/p/17937321