题解:P17851 「RiOI-(-2)」houran 和芒果 / [ROI 2026 Day1] 泰莫利亚的调查 加强版
lichengxi1111 · · 题解
妙妙题。
Part 1
贪心的想法还是挺好想的。
其实本质上就是选一堆叶子节点然后往上跑。要求点不相交。
所以设选出的叶子节点集合为
那么考虑把儿子按边权从小到大考虑。那么
这样我们可以考虑建一些虚点。
具体连边方式如下:
如下图:
于是原题就转化成了
Part 2
这个问题有一个经典的贪心方法。就是直接长链剖分,然后答案就是前
换一个理解方式就是设
可如果不建虚点我们也可以做。如下图:
倒序枚举每个儿子,初始
那么从后往前考虑,现在的
如果
反之同理。这样就做完了。
Code
::::info[代码]
#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;
}
::::
::::info[卡常版代码]
#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;
}
::::