题解:CF2126F 1-1-1, Free Tree!

· · 题解

翻译:

给定一个 n 个节点的树,树上的第 i 个节点有一个颜色 A_i,编号为 i 的边连接 X_i,Y_i,并且有一个值 C_i。

定义第 i 条边的美丽值为:

如果这条边连接的节点颜色相同(即 A_{X_i}=A_{Y_i}),美丽值为 0;否则美丽值为 C_i。

有 Q 次操作,每次操作会改变一个点的颜色,并且询问此时这棵树所有边的美丽值之和。

正难则反。我们预处理出所有 C_i 之和,然后减去所有连接的两个节点相同的边的 C_i 就是它的美丽值。

考虑根号分治。设置阈值为 l,当一个点的度数大于等于 l,则称这个点为度较多的点,否则为度较少的点。

我们可以用个数组 \tt{cont},\mathtt{cont}[i][j] 表示当第 i 个度较多的点颜色为 j 时,对美丽值的减少产生的贡献。

在查询时,对于度较少的点,直接暴力统计;而对于度较多的点,直接在 \tt{cont} 中查询即可。得出答案后别忘了更新 \tt{cont} 数组。

显然度较多的点不会超过 \frac{n}{l},而每次查询暴力统计的点数不超过 l,故时间复杂度为 O(ql+\frac{qn}{l}),当 l 取 \sqrt{n} 时,时间复杂度为 O(q\sqrt n)。

大功告成……了吗?

交上去会发现直接 MLE 了,原来数组大小约为 9\times 10^7,已经超过 256MB 的限制了。

注意到本题时间限制较为宽松,故考虑调整 l 的大小,用时间换空间。这里我通过调整 l,使度较多的点的数量严格小于 100。此时 \tt{cont} 的大小就降至 100\times (2\times 10^5)=2\times 10^7,可以通过。

代码如下:

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <algorithm>
#include <iostream>
#include <random>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#define int long long

using namespace __gnu_pbds;
using namespace std;

mt19937 rng;
unsigned s1, s2, s3;

int a[200005];
int sizes[200005];
vector <pair <int, int>> v[200005];

int siz;
vector <int> big;//记录度较多的点
int idx[200005];
vector <pair <int, int>> v2[200005];
int cont[105][200005];//建立一个数组直接统计

void solve(){
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    int ans = 0;

    for(int i = 1; i < n; i++){
        int x, y, z;
        cin >> x >> y >> z;
        v[x].push_back({y, z});
        v[y].push_back({x, z});
        sizes[x]++, sizes[y]++;
        if(a[x] != a[y]) ans += z;//预处理出所有 C_i 的和
    }
    nth_element(sizes + 1, sizes + min(100ll, n) + 1, sizes + n + 1, greater <int>());
    siz = sizes[min(100ll, n)] + 1;
    for(int i = 1; i <= n; i++){
        if(v[i].size() >= siz){//记录度较多的点
            big.push_back(i), idx[i] = big.size() - 1;
        }
    }

    for(int i = 1; i <= n; i++){
        for(auto j:v[i]){
            if(v[j.first].size() >= siz){
                v2[i].push_back(j);
                cont[idx[j.first]][a[i]] += j.second;//预处理出贡献
            }
        }
    }

    while(m--){
        int x, y;
        cin >> x >> y;
        if(v[x].size() < siz){//度较少的点直接暴力计算
            for(auto i:v[x]){
                if(a[i.first] != a[x]) ans -= i.second;
                if(a[i.first] != y) ans += i.second;
            }
        }else{
            ans += cont[idx[x]][a[x]];//直接查表
            ans -= cont[idx[x]][y];
        }
        for(auto i:v2[x]){
            cont[idx[i.first]][a[x]] -= i.second;//更新数组
            cont[idx[i.first]][y] += i.second;
        }
        a[x] = y;
        cout << ans << '\n';
    }

    for(int i = 1; i <= n; i++){//记得清空
        v[i].clear();
        v2[i].clear();
        idx[i] = 0;
    }
    for(int i = 1; i <= 100; i++){
        for(int j = 1; j <= n; j++){
            cont[i][j] = 0;
        }
    }
    big.clear();
} 
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    rng.seed(time(0));
    s1 = rng(), s2 = rng(), s3 = rng();
    int t;
    cin >> t;
    while(t--) solve();

    return 0;
}

通过记录