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

· · 题解

题意

一棵 N 个点的树与 M 个人,初始第 i 个人在位置 S_i,要到 T_i。

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

思路

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

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

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

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

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

初始化时:

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

注意:

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

#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;
    }
}