跳到正文

个人记录

乱搞

ToBeDetermined约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年4月13日
收录
2026年4月13日
更新
2026年4月13日
篇幅
约 6 分钟

版本

  1. v1当前

P3523

大概是暴力,但是 bfs 覆盖的时候给每个点设置了一个覆盖次数上限。submission

是否有人有兴趣卡到 100 分。

C++
#include <bits/stdc++.h>

#define rep(i, s, e) for(int i = s; i <= (e); ++i)
#define fep(i, s, e) for(int i = s; i < (e); ++i)
#define _rep(i, s, e) for(int i = s; i >= (e); --i)
#define _fep(i, s, e) for(int i = s; i > (e); --i)

// #define int long long
#define pii pair<int, int>

namespace FastIO {
	template <typename _Tp> inline void read(_Tp &x) { int neg = 1; char ch; while(ch = getchar(), !isdigit(ch)) if(ch == '-') neg = -1; x = ch - '0'; while(ch = getchar(), isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ '0'); x *= neg; }
	template <typename _Tp, typename... _Args> inline void read(_Tp &x, _Args &...args) { read(x); read(args...); }
	template <typename _Tp> inline void read(_Tp* begin, _Tp* end) { int len = end - begin; for(int i = 0; i < len; ++i) read(*(begin + i)); }
	template <typename _Tp> inline void write(_Tp x) { if(x < 0) putchar('-'), x= -x; if(x > 9) write(x / 10); putchar(x % 10 + '0'); }
	template <typename _Tp, typename... _Args> inline void write(_Tp x, _Args ...args) { write(x); putchar(' '); write(args...); }
	template <typename _Tp> inline void write(_Tp* begin, _Tp* end) { int len = end - begin; for(int i = 0; i < len; ++i) write(*(begin + i)), putchar(' '); }
}

using namespace std;
using namespace FastIO;

constexpr int inf = numeric_limits<int>::max() >> 1;
constexpr int ninf = numeric_limits<int>::min() >> 1;
constexpr int mod = 1000000007;
constexpr double eps = 1e-9;

int n, m, a[300005], fa[19][300005], dep[300005], b[300005], cnt, cur, tot, lmt, vis[300005];
vector<int> to[300005];
struct Node { int u, v, d; };
queue<Node> q;

void dfs(int u) {
	rep(i, 1, 18) fa[i][u] = fa[i - 1][fa[i - 1][u]];
	if(a[u]) b[++cnt] = u;
	for(int v : to[u]) {
		if(v == fa[0][u]) continue;
		fa[0][v] = u, dep[v] = dep[u] + 1;
		dfs(v);
	}
}

void tag(int x, int y) {
	++vis[x];
	if(not y) return;
	q.push({0, x, 0});
	while(!q.empty()) {
		Node u = q.front(); q.pop();
		for(int v : to[u.v]) {
			if(v == u.u) continue;
			++vis[v];
			if(vis[v] <= lmt and u.d < y - 1) q.push({u.v, v, u.d + 1});
		}
	}
}

bool chk(int x) {
	tot = 0;
	rep(i, 1, n) vis[i] = 0;
	rep(i, 1, cnt) {
		if(vis[b[i]]) continue;
		++tot;
		cur = b[i];
		rep(i, 0, 18) if((x >> i) & 1) {
			cur = fa[i][cur];
		}
		tag(cur, x);
	}
	return tot <= m;
}

void solve() {
	read(n, m);
	read(a + 1, a + 1 + n);
	fep(i, 1, n) {
		int u, v; read(u, v);
		to[u].push_back(v);
		to[v].push_back(u);
	}
	fa[0][1] = 1, dep[1] = 1, dfs(1);
	sort(b + 1, b + 1 + cnt, [](int x, int y) {
		return dep[x] > dep[y];
	});
	lmt = min(n, (int)(2.5e8 / n / log2(n)));
	int l = 0, r = n, mid;
	while(l <= r) {
		mid = (l + r) >> 1;
		if(chk(mid)) r = mid - 1;
		else l = mid + 1;
	}
	write(r + 1), putchar('\n');
}

signed main() {
	// freopen("dyn.in", "r", stdin);
	// freopen("dyn.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T = 1;
	while(T--) solve();
	return 0;
}
原文