跳到正文

题解

题解:P17851 「RiOI-(-2)」houran 和芒果 / [ROI 2026 Day1] 泰莫利亚的调查 加强版

lichengxi1111约 14 分钟
原文
文章信息与版本

文章信息

发布
2026年11月8日
收录
2026年11月12日
更新
2026年11月22日
篇幅
约 14 分钟
题目
P17851

版本

  1. v1当前

妙妙题。

Part 1

贪心的想法还是挺好想的。

其实本质上就是选一堆叶子节点然后往上跑。要求点不相交。

所以设选出的叶子节点集合为 SS。那么对于一个点 uu 而言,设它的儿子(v1,v2,…,vtv_1, v_2, \dots, v_t)中有一些会被叶子节点覆盖。那么 uu 的贡献必定是这些儿子中到它的边权最大的。

那么考虑把儿子按边权从小到大考虑。那么 uu 的贡献如果是 wviw_{v_i} 的话,就说明 i+1i+1 到 tt 中没有被叶子覆盖的。即他是最后一个能被叶子覆盖的点。

这样我们可以考虑建一些虚点。

具体连边方式如下:

  • kik_i 向 ki+1k_i+1 连边。

  • kik_i 点权为 wi+1−wiw_{i + 1} - w_i。

  • kik_i 向 viv_i 连边。

如下图:

于是原题就转化成了 mm 条链,点不重合,要求覆盖点权最大值。

Part 2

这个问题有一个经典的贪心方法。就是直接长链剖分,然后答案就是前 mm 长的长链。

换一个理解方式就是设 dud_u 是 uu 往下的长链。那么考虑 uu 的每个儿子 vv,du=max⁡dv+wud_u = \max{d_v} + w_u。设长儿子是 sonuson_u,那么 dud_u 就是一个可能的长链,而除了 sonuson_u 的别的儿子 vv 的 dvd_v 也是可能的长链。

可如果不建虚点我们也可以做。如下图:

倒序枚举每个儿子,初始 dud_u 为 00。

那么从后往前考虑,现在的 dud_u 表示 ki+1k_{i+1} 时的长链。那么 du=max⁡(dv,du′)+wud_u = \max(d_v, d'_u) + w_u。

如果 dv>du′d_v > d'_u,那么相当于 du′d'_u 不是这个的长链,是新的可能作为答案的长链,就加入这个链。

反之同理。这样就做完了。

Code

代码
C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#pragma GCC optimize(2, 3)

const int V = 1 << 17;
const int N = 2e5 + 5;
int n, fa[N], w[N], W[N], X[4], m, T, tot;
int cnt[N], id1[N], id2[N], l[N], r[N];
ll d[N], D[N];

inline void chkmin (int &x, int y) {
	x = (x > y) ? y : x;
}

inline void chkmax (int &x, int y) {
	x = (x < y) ? y : x;
}

int main () {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	freopen ("mango.in", "r", stdin);
	freopen ("mango.out", "w", stdout);
	cin >> n;
	for (int i = 2; i <= n; ++i) cin >> fa[i];
	for (int i = 2; i <= n; ++i) cin >> W[i];
	
	cin >> T;
	while (T--) {
		for (int i = 0; i < 4; ++i) cin >> X[i]; cin >> m;
		for (int i = 2; i <= n; ++i) w[i] = W[i] ^ X[i % 4];
		
		for (int i = 0; i < V; ++i) cnt[i] = 0;
		for (int i = 2; i <= n; ++i) cnt[w[i]]++;
		for (int i = 1; i < V; ++i) cnt[i] += cnt[i - 1];
		for (int i = n; i >= 2; --i) id1[cnt[w[i]]--] = i;
		for (int i = 0; i <= n; ++i) cnt[i] = 0;
		for (int i = 1; i < n; ++i) cnt[fa[id1[i]]]++;
		for (int i = 2; i <= n; ++i) cnt[i] += cnt[i - 1];
		for (int i = n - 1; i >= 1; --i) id2[cnt[fa[id1[i]]]--] = id1[i];
		
		for (int i = 1; i <= n; ++i) l[i] = n + 1, r[i] = 0;
		for (int i = 1; i < n; ++i) chkmin(l[fa[id2[i]]], i);
		for (int i = 1; i < n; ++i) chkmax(r[fa[id2[i]]], i);
		
		tot = 0;
		for (int i = 1; i <= n; ++i) d[i] = 0;
		for (int x = n; x >= 1; --x) {
			for (int j = r[x]; j >= l[x]; --j) {
				int y = id2[j];
				int val = (j == l[x]) ? w[y] : w[y] - w[id2[j - 1]];
				if (d[y] > d[x]) {
					D[++tot] = d[x];
					d[x] = d[y] + val;
				} else {
					D[++tot] = d[y];
					d[x] = d[x] + val;
				}
			}
		}
		D[++tot] = d[1];
		
		ll ans = 0; m = min(m, tot);
		nth_element(D + 1, D + m, D + tot + 1, greater<ll>());
		for (int i = 1; i <= m; ++i) ans += D[i];
		cout << ans << '\n';
	}
	return 0;
}
卡常版代码
C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 2e5 + 5;
const int LOW_SIZE  = 512;
const int HIGH_SIZE = 256;

int n, fa[N], w[N], W[N], X[4], m, T, tot;
int cnt[N], id1[N], id2[N], l[N], r[N];
int baseCnt[N];
ll d[N], D[N];

static char ibuf[1 << 22];
static int ipos = 0, ilen = 0;
inline int gc() {
    if (ipos >= ilen) {
        ilen = fread(ibuf, 1, sizeof(ibuf), stdin);
        ipos = 0;
        if (ilen == 0) return -1;
    }
    return ibuf[ipos++];
}
inline int read() {
    int s = 0; int ch = gc();
    while (ch < '0' || ch > '9') ch = gc();
    while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = gc(); }
    return s;
}

static char obuf[1 << 16];
static int opos = 0;
inline void write(ll x) {
    if (x == 0) { obuf[opos++] = '0'; obuf[opos++] = '\n'; goto flush; }
    { char tmp[25]; int p = 0;
      while (x) { tmp[p++] = '0' + x % 10; x /= 10; }
      while (p) obuf[opos++] = tmp[--p];
      obuf[opos++] = '\n'; }
flush:
    if (opos > (1 << 15)) { fwrite(obuf, 1, opos, stdout); opos = 0; }
}

int main() {
    n = read();
    for (int i = 2; i <= n; ++i) fa[i] = read();
    for (int i = 2; i <= n; ++i) W[i] = read();
    for (int i = 2; i <= n; ++i) ++baseCnt[fa[i]];
    for (int i = 1; i <= n; ++i) baseCnt[i] += baseCnt[i - 1];
    T = read();
    while (T--) {
        for (int i = 0; i < 4; ++i) X[i] = read();
        m = read();
        int a = X[0], b = X[1], c = X[2], e = X[3];
        int i = 2;
        for (; i + 3 <= n; i += 4) {
            w[i]   = W[i]   ^ c;
            w[i+1] = W[i+1] ^ e;
            w[i+2] = W[i+2] ^ a;
            w[i+3] = W[i+3] ^ b;
        }
        for (; i <= n; ++i) w[i] = W[i] ^ X[i & 3];
        memset(cnt, 0, sizeof(int) * LOW_SIZE);
        for (int i = 2; i <= n; ++i) ++cnt[w[i] & 511];
        for (int i = 1; i < LOW_SIZE; ++i) cnt[i] += cnt[i - 1];
        for (int i = n; i >= 2; --i) {
            int v = w[i] & 511;
            id1[cnt[v]--] = i;
        }
        memset(cnt, 0, sizeof(int) * HIGH_SIZE);
        for (int i = 1; i < n; ++i) ++cnt[w[id1[i]] >> 9];
        for (int i = 1; i < HIGH_SIZE; ++i) cnt[i] += cnt[i - 1];
        for (int i = n - 1; i >= 1; --i) {
            int v = w[id1[i]] >> 9;
            id2[cnt[v]--] = id1[i];
        }
        memcpy(cnt, baseCnt, sizeof(int) * (n + 1));
        for (int i = n - 1; i >= 1; --i) {
            int f = fa[id2[i]];
            id1[cnt[f]--] = id2[i];
        }
        fill(l + 1, l + n + 1, n + 1);
        memset(r, 0, sizeof(int) * (n + 1));
        for (int i = 1; i < n; ++i) {
            int f = fa[id1[i]];
            if (l[f] == n + 1) l[f] = i;
            r[f] = i;
        }
        tot = 0;
        for (int x = n; x >= 1; --x) {
            d[x] = 0;
            int L = l[x], R = r[x];
            for (int j = R; j > L; --j) {
                int y = id1[j];
                int val = w[y] - w[id1[j - 1]];
                if (d[y] > d[x]) {
                    D[++tot] = d[x];
                    d[x] = d[y] + val;
                } else {
                    D[++tot] = d[y];
                    d[x] = d[x] + val;
                }
            }
            if (L <= R) {
                int y = id1[L];
                int val = w[y];
                if (d[y] > d[x]) {
                    D[++tot] = d[x];
                    d[x] = d[y] + val;
                } else {
                    D[++tot] = d[y];
                    d[x] = d[x] + val;
                }
            }
        }
        D[++tot] = d[1];
        if (m >= tot) {
            ll ans = 0;
            for (int i = 1; i <= tot; ++i) ans += D[i];
            write(ans);
        } else {
            nth_element(D + 1, D + m, D + tot + 1, greater<ll>());
            ll ans = 0;
            for (int i = 1; i <= m; ++i) ans += D[i];
            write(ans);
        }
    }
    fwrite(obuf, 1, opos, stdout);
    return 0;
}
原文