P1600 [NOIP2016 提高组] 天天爱跑步 题解

· · 闲话

P1600 [NOIP2016 提高组] 天天爱跑步 题解

前言

这道题堪称在当时的 NOIP 最难题目,时至今日仍然能在 NOIP 题目难度中排进前十。

当然我是完全不会的,看题解都看了一个小时。

Problem

给出一棵树,有 m 个人,每个人从自己的起点跑向终点,一秒跑一条边。同时每个点上都会有观察哨,第 i 个观察哨在第 w_i 时刻观察一次,它能观察到当前跑到这个节点的玩家。求出每个观察哨在游戏过程中能观察到的玩家数量。

Solution

首先,暴力枚举 m 个玩家显然不可行。我们用树上问题的思想来处理一下。

很明显,每一个玩家的路程是一条树上的链,所以我们考虑将它拆成两部分,即 (s_i\to lca_{s_i,t_i}) 和 (lca_{s_i,t_i}\to t_i)。

现在需要动一点脑子,上面说了枚举 m 个玩家不可行,那我们就不枚举玩家了,我们考虑玩家对观察哨的贡献,也就是改为枚举观察哨。

综合上面两段,我们就可以开始分类讨论。设当前枚举的观察哨是 P,节点 i 的深度为 dep_i,树上两点间距离为 dis(i,j)。

1)由 (s_i\to lca_{s_i,t_i}) 部分(上升部分)做出贡献

由上升部分做出贡献,需要满足 dep_{s_i}=dep_P+w_P。

2)由 (lca_{s_i,t_i}\to t_i) 部分(下降部分)做出贡献

由下降部分做出贡献,需要满足 dis(s_i,t_i)-dep_{s_i}=w_P-dep_P

(上面两个式子都不难得出,可以画图理解)

上面两种情况我们需要分别处理。但我们先来看一个显然的结论:如果这个点要对点 P 做出贡献,那么不管这个点是起点还是终点,都必然在 P 的子树上。 根据这个结论,我们就有了大致的思路,即在遍历整棵树的过程中求出答案。

接下来考虑怎么求出答案。暴力枚举肯定不行,我们考虑用一个桶(准确来讲是两个)来存储每个点的贡献。例如枚举完 P 的子树时,上升部分做出的贡献就是 bucket1(dep_P + w_P),下降部分作出的贡献是 bucket2(w_P-dep_P)。

但这样直接提取,会发现有一些点的贡献算的不对,所以这里还需要完善一下定义,一个点所接受的贡献是它子树遍历前后桶内差值的总和。

到这里基本上就分析完了,在考虑一个小小的问题:如果枚举的点 P 正好是一条路径上的 lca,那么这条路径只会对点 P 做出贡献,对点 P 的祖先没有任何贡献,所以在离开 dfs 的时候要将这部分减去。

然后就是代码实现了,具体看注释。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int Inf = 2e9;
const int Maxn = 1e5 + 5;

int n, m;

int head[Maxn], h1[Maxn], h2[Maxn], edgenum, num1, num2;
struct node{
    int nxt, to;
}edge[Maxn], ed[Maxn], LCA[Maxn];
/*
三个数组分别代表:
链式前向星存图
用链表方式存入以某个点为终点的路径
用链表方式存入以某个点作为路径上 lca 的路径
*/

void add(int from, int to) {
    edge[++edgenum].nxt = head[from];
    edge[edgenum].to = to;
    head[from] = edgenum;
}

void add1(int from, int to) {
    ed[++num1].nxt = h1[from];
    ed[num1].to = to;
    h1[from] = num1;
}

void add2(int from, int to) {
    LCA[++num2].nxt = h2[from];
    LCA[num2].to = to;
    h2[from] = num2;
}
//正常加边

int w[Maxn];
struct player {
    int s, t, dis;
}a[Maxn];

int fa[Maxn][21], dep[Maxn];

void dfs(int x, int fat) {
    dep[x] = dep[fat] + 1;
    fa[x][0] = fat;
    for(int i = 1; i <= 20; i++) {
        fa[x][i] = fa[fa[x][i - 1]][i - 1];
    }
    for(int i = head[x]; i; i = edge[i].nxt) {
        int to = edge[i].to;
        if(to == fat) continue;
        dfs(to, x);
    }
}
//正常预处理

int lca(int x, int y) {
    if(dep[y] < dep[x]) swap(x, y);
    for(int i = 20; i >= 0; i--) {
        if(dep[fa[y][i]] >= dep[x]) {
            y = fa[y][i];
        }
    }
    if(x == y) return x;
    for(int i = 20; i >= 0; i--) {
        if(fa[x][i] != fa[y][i]) {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}
//求 lca

int st[Maxn], ans[Maxn];//st[i] 表示以第 i 个点作为起点的路径总数
int up[Maxn], down[Maxn << 1];//两个桶
//注意由于 w[x] - dep[x] 可能为负数,所以要用一个偏移量

void solve(int x) {
    int s1 = up[dep[x] + w[x]], s2 = down[w[x] - dep[x] + Maxn];//记录遍历前桶内的值
    for(int i = head[x]; i; i = edge[i].nxt) {//搜索子树
        int to = edge[i].to;
        if(to == fa[x][0]) continue;
        solve(to);
    }
    up[dep[x]] += st[x];//这个点所做出的贡献(上升部分)
    for(int i = h1[x]; i; i = ed[i].nxt) {//枚举以这个点作为终点的路径
        int to = ed[i].to;
        down[a[to].dis - dep[a[to].t] + Maxn]++;//这个点做出的贡献(下降部分)
    }
    ans[x] += up[dep[x] + w[x]] - s1 + down[w[x] - dep[x] + Maxn] - s2;//求差值
    for(int i = h2[x]; i; i = LCA[i].nxt) {//处理当前点为 lca 的情况
        int to = LCA[i].to;
        up[dep[a[to].s]]--;
        down[a[to].dis - dep[a[to].t] + Maxn]--;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        add(u, v);
        add(v, u);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i++) {
        cin >> w[i];
    }
    for(int i = 1; i <= m; i++) {
        cin >> a[i].s >> a[i].t;    
        int Lca = lca(a[i].s, a[i].t);
        a[i].dis = dep[a[i].s] + dep[a[i].t] - 2 * dep[Lca];//路径长度
        st[a[i].s]++;
        add1(a[i].t, i);
        add2(Lca, i);
        if(dep[Lca] + w[Lca] == dep[a[i].s]) ans[Lca]--;//一个小重点
/*
        当一条路径的起点或终点为 lca 的时候,
        计算上升路径和下降路径的时候会把 lca 这个点算两次,
        所以提前将答案减 1 来抵消重复计算
*/
    }
    solve(1);//遍历解决
    for(int i = 1; i <= n; i++) {
        cout << ans[i] << ' ';
    }
    return 0;
}