跳到正文

个人记录

P3979 遥远的国度 做题记录

sample_mean_VVV约 12 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月23日
篇幅
约 12 分钟

版本

  1. v1当前

这道题其他地方都是板子,但是有换根的操作,如果每次暴力来做就等着 30 分吧!(其实由于修改防御力还要修改 a 数组的值导致我暴力只拿了 20 分:暴力20)所以让我们来想想正解:

画个图: 由于是自己的做题记录就随便画了,首先我们选一个点作为 idid: 再选个点为 noot,(new root == noot 不愧是我)我们发现当 noot 就是 id 时,答案就是整棵树的最小值: 那换个点呢,我们可以发现,当 noot 是 id 的祖先或者八竿子打不着关系时,答案不会变:

但 noot 在 id 的子树里时,答案会变成 id 子树中除了 noot 所在子树外的子树中的最小值比如下面这个图,答案将变成红框内的最小值:

再比如说这个:

总结一下:当 noot 是 id 的子节点时,我们能找到一个点 son,其是 id 的儿子,也是 noot 的祖先,(废话)而答案就是 min⁡1∼dfn[son]−1\min_{1\sim dfn[son]-1} 与 min⁡dfn[son]+size[son]∼n\min_{dfn[son]+size[son]\sim n} 的最小值,而这个 son 该怎么求呢,我们可以结合上下图发现,当 id 只有一个儿子时,这个儿子一定是它的重儿子,且 son 就是它,如果有多个儿子,那 son 一定是 id 的儿子中是 top[noot]top[noot] 的存在。

所以我们可以让 noot 往其的 top 跳,当 top[noot]==top[id]top[noot]==top[id] 那就说明 noot 在 id 的重链上了,son 就是 id 的重儿子,而当 fa[top[noot]]==idfa[top[noot]]==id 时,那 son 就是 top[noot]top[noot] 了,而对于本来就在 id 重链上的 noot,son 就也是 id 的重儿子了。

最后贴出代码:

C++
//Just Sayori
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <utility>
#include <cmath>
#include <stack>
#include <vector>
#include <climits>
#define ll long long
#define rnt register int
#define N 100005
#define M 0x3f3f3f3f3f3f
using namespace std;
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - 48;
        ch = getchar();
    }
    return x * f;
}
struct edge
{
    int v, next;
} e[N << 1];
int head[N], cnt;
void tdd(int u, int v)
{
    e[++cnt] = {v, head[u]}, head[u] = cnt;
    e[++cnt] = {u, head[v]}, head[v] = cnt;
}
int n, m, u, v, w, root;
int a[N], k[N], fa[N], ans[N], dfn[N], top[N], bson[N], deep[N], size[N];
void dfs1(int u, int f)
{
    fa[u] = f, size[u] = 1;
    deep[u] = deep[f] + 1;
    int mize = 0;
    for (rnt i = head[u]; i; i = e[i].next)
    {
        int v = e[i].v;
        if (v == f) continue;
        dfs1(v, u);
        size[u] += size[v];
        if (size[v] > mize) bson[u] = v, mize = size[v];
    }
    if (!mize) bson[u] = 0;
}
void dfs2(int u, int t)
{
    top[u] = t;
    dfn[u] = ++cnt;
    k[cnt] = a[u];
    if (!bson[u]) return;
    dfs2(bson[u], t);
    for (rnt i = head[u]; i; i = e[i].next)
    {
        int v = e[i].v;
        if (v == fa[u] || v == bson[u]) continue;
        dfs2(v, v);
    }
}
struct tree
{
    int l, r, w, tag;
} s[N << 2];
void set(int u, int w)
{
    s[u].w = w;
}
void push_up(int u)
{
    s[u].w = min(s[u << 1].w, s[u << 1 | 1].w);
}
void push_down(int u)
{
    if (s[u].tag == -1) return;
    s[u << 1].w = s[u << 1 | 1].w = s[u].tag;
    s[u << 1].tag = s[u << 1 | 1].tag = s[u].tag;
    s[u].tag = -1;
}
void build(int u, int l, int r)
{
    s[u].l = l, s[u].r = r, s[u].tag = -1;
    if (l == r) return set(u, k[l]);
    int mid = (l + r) >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    push_up(u);
}
void modify(int u, int l, int r, int w)
{
    if (l <= s[u].l && s[u].r <= r)
    {
        s[u].w = w;
        s[u].tag = w;
        return;
    }
    push_down(u);
    int mid = (s[u].l + s[u].r) >> 1;
    if (l <= mid) modify(u << 1, l, r, w);
    if (r >= mid + 1) modify(u << 1 | 1, l, r, w);
    push_up(u);
}
ll query(int u, int l, int r)
{
    if (l <= s[u].l && s[u].r <= r) return s[u].w;
    push_down(u);
    ll ans = M;
    int mid = (s[u].l + s[u].r) >> 1;
    if (l <= mid) ans = query(u << 1, l, r);
    if (r >= mid + 1) ans = min(ans, query(u << 1 | 1, l, r));
    return ans;
}
void get_lca(int a, int b, int w)
{
    while (top[a] != top[b])
    {
        if (deep[top[a]] < deep[top[b]]) swap(a, b);
        modify(1, dfn[top[a]], dfn[a], w);
        a = fa[top[a]];
    }
    if (deep[a] > deep[b]) swap(a, b);
    modify(1, dfn[a], dfn[b], w);
}
int main()
{
    //freopen("xx.txt", "r", stdin), freopen("xxx.txt", "w", stdout);
    n = read(), m = read();
    for (rnt i = 1; i < n; i++) u = read(), v = read(), tdd(u, v);
    for (rnt i = 1; i <= n; i++) a[i] = read();
    root = read();
    cnt = 0, dfs1(root, 0), dfs2(root, root), build(1, 1, n);
    for (rnt i = 1; i <= m; i++)
        switch (read())
        {
            case 1:
                root = read();
                break;
            case 2:
                u = read(), v = read(), w = read();
                get_lca(u, v, w);
                break;
            case 3:
                u = read();
                if (root == u) printf("%lld\n", query(1, 1, n)); //节点是根,全搜
                else if (dfn[root] > dfn[u] && dfn[root] <= dfn[u] + size[u] - 1)//如果根在节点子树上,除了根那个子树,其他全搜
                {
                    int now = root, son = 0;
                    while (top[now] != top[u])
                        if (fa[top[now]] == u)
                        {
                            son = top[now];
                            break;
                        }
                        else
                            now = fa[top[now]];
                    if (!son) son = bson[u];
                    printf("%lld\n", min(query(1, 1, dfn[son] - 1), query(1, dfn[son] + size[son], n)));
                }
                else//其他情况,不变
                    printf("%lld\n", query(1, dfn[u], dfn[u] + size[u] - 1));
        }
    return 0;
}
原文