跳到正文

题解

题解:P9520 [JOIST 2022] 监狱 / Jail

ForgetOIDuck约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年7月7日
收录
2026年7月13日
更新
2026年11月17日
篇幅
约 8 分钟
获赞
2
题目
P9520

版本

  1. v1当前

题意

一棵 NN 个点的树与 MM 个人,初始第 ii 个人在位置 SiS_i,要到 TiT_i。

求问是否存在操作序列形如:每次让一个人从当前点走到另一个相邻点,要求每个人最终到达了 TiT_i,且任意时刻都没有两个人位于同一个点。

思路

猜测一个人走路在操作序列上一定是连续的,也就是说相当于每次直接让一个人走到终点。事实也确实如此,可以通过调整法证明。

于是我们思考本题满足条件的限制是什么。具体有如下几种,记 D(x,y)D(x,y) 表示树上位于 xx 到 yy 路径上的所有点组成的点集。

  • Sj∈D(Si,Ti)S_j\in D(S_i,T_i),说明 jj 要在 ii 之前走;

  • Tj∈D(Si,Ti)T_j\in D(S_i,T_i),说明 jj 要在 ii 之后走。

若我们得知 ii 在 jj 之前走就连一条有向边 (i,j)(i,j),最后按照拓扑序一个个走即可,若有环则无解。

暴力连边是 O(n2)O(n^2) 的,这很不好。考虑到这个相当于一个点对一条链 // 一条链对一个点连边,我们考虑线段树优化建图。

具体地,我们定义三种点:AiA_i 表示第 i\pmb{i} 个人,B[l,r]B_{[l,r]} 表示入树的区间 [l,r]\pmb{[l, r]},C[l,r]C_{[l,r]} 表示出树的区间 [l,r]\pmb{[l, r]}。入树表示:某个点要向该树某个区间的所有点连边;出树表示:该树某个区间的所有点要向某个点连边。

初始化时:

  • B[l,r]B_{[l,r]} 向 B[l,mid],B[mid+1,r]B_{[l,mid]},B_{[mid+1,r]} 连边(l<rl<r);

  • C[l,mid],C[mid+1,r]C_{[l,mid]},C_{[mid+1,r]} 向 C[l,r]C_{[l,r]} 连边(l<rl<r);

  • B[dfnTi,dfnTi]B_{[\text{dfn}_{T_i},\text{dfn}_{T_i}]} 向 AiA_i 连边;

  • AiA_i 向 C[dfnSi,dfnSi]C_{[\text{dfn}_{S_i},\text{dfn}_{S_i}]} 连边。

连边时就使用重链剖分拆成若干个线段树上区间即可。

注意:

  • 由于 Ai,Ti∈D(Si,Ti)A_i,T_i\in D(S_i,T_i),所以可能会不小心连出自环,需要在建边时抠掉该端点,不能抠多也不能抠少。我试过了是真的。

  • 点数要开足够,最好是开 8×n+m8\times n + m。我试过了是真的。

  • 要区分好树上边和限制边。我没试过不知道是不是真的。

  • 多测不清空,亲人两行泪。我试过了是真的。

时间复杂度 O(mlog⁡2n)O(m\log^2 n),常数比较小,听说比单 log⁡\log 的倍增还快来着,不知道是不是真的。

C++
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
ll q, n, m, pidx, rtin, rtou, d[1200002], dep[120002], nfd[120002], st[120002], ed[120002], ts[120002], de[120002], e[24000002], fa[120002], h[1200002], ne[24000002], idx, gen[120002], sz[120002], dfn[120002];
queue<ll> qu;
struct node {
	ll ls, rs;
}tr[2400002];
void add(ll x, ll y) {
	if (!x || !y) return ;
	d[y] ++;
	e[++ idx] = y, ne[idx] = h[x], h[x] = idx;
}
void dfs_sz(ll x) {
	sz[x] = 1;
	for (ll i = h[x]; i; i = ne[i]) {
		ll y = e[i];
		if (y == fa[x]) continue;
		fa[y] = x, dep[y] = dep[x] + 1, dfs_sz(y), sz[x] += sz[y];
	}
}
void dfs_gen(ll x) {
	dfn[x] = ++ idx;
	nfd[idx] = x;
	ll res = 0;
	for (ll i = h[x]; i; i = ne[i]) {
		ll y = e[i];
		if (y == fa[x]) continue;
		if (sz[res] < sz[y]) res = y;
	}
	if (res) gen[res] = gen[x], dfs_gen(res);
	for (ll i = h[x]; i; i = ne[i]) {
		ll y = e[i];
		if (y == fa[x] || y == res) continue;
		gen[y] = y, dfs_gen(y);
	}
}
void build(ll &x, ll l, ll r, ll op) {
	x = ++ pidx;
	if (l == r) return (op ? add(ts[nfd[l]], x) : add(x, de[nfd[l]]));
	ll mid = l + r >> 1;
	build(tr[x].ls, l, mid, op);
	build(tr[x].rs,mid+1,r, op);
	if (op) add(tr[x].ls, x), add(tr[x].rs, x);
	else add(x, tr[x].ls), add(x, tr[x].rs);
}
void update(ll x, ll l, ll r, ll ansl, ll ansr, ll p, ll op) {
	if (ansl > ansr) return ;
	if (ansl <= l && ansr >= r) return (op ? add(x, p) : add(p, x));
	ll mid = l + r >> 1;
	if (mid >= ansl) update(tr[x].ls, l, mid, ansl, ansr, p, op);
	if (mid <  ansr) update(tr[x].rs,mid+1,r, ansl, ansr, p, op);
}
int main() {
	cin >> q;
	while (q -- ) {
		cin >> n; idx = 0;
		for (ll i = 1; i <= n; i ++ ) h[i] = 0;
		for (ll i = 1, x, y; i < n; i ++ ) cin >> x >> y, add(x, y), add(y, x);
		cin >> m;
		idx = pidx = 0;
		dfs_sz(1);
		dfs_gen(1);
		for (ll i = 1; i <= n; i ++ ) d[i] = h[i] = ts[i] = de[i] = 0;
		for (ll i = 1; i <= m; i ++ ) cin >> st[i] >> ed[i], ts[st[i]] = de[ed[i]] = i;
		pidx = m; idx = 0;
		build(rtin, 1, n, 0); build(rtou, 1, n, 1);
		for (ll i = 1; i <= m; i ++ ) {
			ll x = st[i], y = ed[i];
			while (gen[x] != gen[y]) {
				if (dep[gen[x]] < dep[gen[y]]) swap(x, y);
				update(rtin, 1, n, dfn[gen[x]], dfn[x] - (x == ed[i]), i, 0);
				update(rtou, 1, n, dfn[gen[x]], dfn[x] - (x == st[i]), i, 1);
				x = fa[gen[x]];
			}
			if (dep[x] > dep[y]) swap(x, y);
			update(rtin, 1, n, dfn[x] + (x == ed[i]), dfn[y] - (y == ed[i]), i, 0);
			update(rtou, 1, n, dfn[x] + (x == st[i]), dfn[y] - (y == st[i]), i, 1);
		}
		ll cnt = 0;
		for (ll i = 1; i <= pidx; i ++ ) if (!d[i]) qu.push(i);
		while (!qu.empty()) {
			ll x = qu.front(); qu.pop();
			cnt ++;
			for (ll i = h[x]; i; i = ne[i]) {
				ll y = e[i];
				if (!(-- d[y])) qu.push(y);
			}
		}
		cout << (cnt == pidx ? "Yes\n" : "No\n");
		for (ll i = 1; i <= pidx; i ++ ) d[i] = h[i] = 0;
	}
}
原文