标签: 长链剖分

  • CF1009F Dominant Indices 长剖入门题 复习

    复习长链剖分。

    首先划分长链,走到每个长链顶就给这个长链开个数组拿来记答案,数组的长度(从0开始算)是整个长链上点的个数,下标为k的位置所表示的意义是到长链顶距离为k的点的个数,沿着重链走的时候,直接指针偏移一位就行了(毕竟状态在深度上连续)。

    然后DFS,走到每一个点,先给这个点的DP数组指针(设为arr),下标为0的位置加个1,表示这个点自己的答案。然后如果这个点有重子节点,就先沿着重子节点走,走的时候DP数组直接偏移上1,然后这个点的答案先记录为重子节点的答案+1(我们要考虑所有子节点的答案,一会拿这玩意来更新)。

    对于非重子节点的点,开个新的DP数组,然后直接传下去往下递归。

    非重子节点返回后尝试更新答案。枚举非重子节点DP数组上的每一个状态,把他的值加到当前节点的DP值上(即合并子链状态,把状态合并到重链上),然后顺带更新一波结果(如果某个深度的值出现的比当前的多,就直接更新,如果相当就比一下深度),合并晚状态后直接把非重子节点的内存回收掉即可。

    显然复杂度是$\theta(n)$的,每条重链会有一个DP数组,这个DP数组的长度是重链的长度,每条重链的答案只会被合并一次,而所有重链的长度之和是$n$。

    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <vector>
    using int_t = long long int;
    
    const int_t LARGE = 1e6;
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int_t results[LARGE + 1];
    
    int_t depth[LARGE + 1];
    int_t maxDepth[LARGE + 1];
    int_t maxChd[LARGE + 1];
    
    std::vector<int_t> graph[LARGE + 1];
    int_t n;
    
    void DFS1(int_t vtx, int_t from = -1) {
        maxChd[vtx] = -1;
        maxDepth[vtx] = depth[vtx];
        for (auto to : graph[vtx])
            if (to != from) {
                depth[to] = depth[vtx] + 1;
                DFS1(to, vtx);
                if (maxChd[vtx] == -1 || maxDepth[to] > maxDepth[maxChd[vtx]]) {
                    maxChd[vtx] = to;
                }
                maxDepth[vtx] = std::max(maxDepth[vtx], maxDepth[to]);
            }
    }
    // arr 距离为x的点的个数
    void DFS2(int_t vtx, int_t from = -1, int_t* arr = nullptr) {
        arr[0]++;
        auto& result = results[vtx];
        if (maxChd[vtx] != -1) {
            DFS2(maxChd[vtx], vtx, arr + 1);
            result = results[maxChd[vtx]] + 1;  //答案初始值
        }
        for (auto to : graph[vtx])
            if (to != from && to != maxChd[vtx]) {
                int_t arrlen = maxDepth[to] - depth[vtx] + 1;
                int_t* next = new int_t[arrlen];
                std::fill(next, next + arrlen, 0);
                // next[0] = 1;
                DFS2(to, vtx, next + 1);
                for (int_t i = 1; i < arrlen; i++) {
                    arr[i] += next[i];
                    if (arr[i] > arr[result])
                        result = i;
                    else if (arr[i] == arr[result] && i < result)
                        result = i;
                }
                delete[] next;
            }
        if (arr[result] == 1)
            result = 0;  //最小的距离
    }
    int main() {
        std::ios::sync_with_stdio(false);
        cin >> n;
        for (int_t i = 1; i <= n - 1; i++) {
            int_t u, v;
            cin >> u >> v;
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
        depth[1] = 1;
        DFS1(1);
    #ifdef DEBUG
    
        for (int_t i = 1; i <= n; i++) {
            cout << "maxchd " << i << " = " << maxChd[i] << endl;
        }
    #endif
        static int_t arr[LARGE + 1];
        DFS2(1, -1, arr);
        for (int_t i = 1; i <= n; i++) {
            cout << results[i] << endl;
        }
    
        return 0;
    }

     

  • POI2014 Hotels

    设$f(vtx,n)$表示vtx为根的子树内,到vtx距离为n的点的个数。

    设$g(vtx,n)$表示vtx内,如果在vtx再接上一个长度为u的链(目标为v)就能和v形成符合题目要求的三元组的点对个数。

    转移的时候枚举子节点,先统计答案,然后合并状态。

    对于边$(u,v)$,能贡献的答案为$g(u,x)\times f(v,x-1)+f(u,x)\times g(v,x+1)$。

    然后合并进去,$g(u,x)+=f(u,x)\times f(v,x-1)+g(v,x+1)$以及$f(u,x)+=f(v,x-1)$。

    注意要先在统计答案的同时合并状态,因为我们统计的是无序三元组。

    复杂度$O(n^3)$,优化一下到$O(n^2)$。

    考虑长链剖分优化。

    每个点的状态直接从重子节点拖过来,然后其他节点的答案参考上面暴力合并进去。

    #include <iostream>
    #include <vector>
    using int_t = long long int;
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 2e5;
    int_t depth[LARGE + 1], maxChd[LARGE + 1], maxDepth[LARGE + 1];
    std::vector<int_t> graph[LARGE + 1];
    
    void DFS1(int_t vtx, int_t from = -1) {
        if (from == -1) depth[vtx] = 1;
        maxDepth[vtx] = depth[vtx];
        for (int_t to : graph[vtx]) {
            if (to != from) {
                depth[to] = depth[vtx] + 1;
                DFS1(to, vtx);
                maxDepth[vtx] = std::max(maxDepth[vtx], maxDepth[to]);
                if (maxDepth[to] > maxDepth[maxChd[vtx]]) maxChd[vtx] = to;
            }
        }
    }
    // f(vtx,u)表示vtx子树内到vtx距离为u的点的个数
    // g(vtx,u)表示vtx内,如果在vtx再接上一个长度为u的链(目标为v)就能和v形成符合题目要求的三元组的点对个数
    void DFS2(int_t vtx, int_t from, int_t* f, int_t* g, int_t& result) {
        //递归重子节点
        if (maxChd[vtx]) {
            DFS2(maxChd[vtx], vtx, f + 1, g - 1, result);
    #ifdef DEBUG
            cout << "at vtx " << vtx << " done heavys" << endl;
            for (int_t i = 0; i <= maxDepth[vtx] - depth[vtx]; i++)
                cout << "f " << i << " = " << f[i] << endl;
            for (int_t i = 0; i <= maxDepth[vtx] - depth[vtx]; i++)
                cout << "g " << i << " = " << g[i] << endl;
            cout << endl;
    #endif
        }
        f[0] += 1;
        //重链贡献的答案
        result += g[0];
        //递归轻子节点
        for (int_t to : graph[vtx]) {
            if (to != from && to != maxChd[vtx]) {
                const int_t size = maxDepth[to] - depth[vtx] + 1;
                auto nextF = new int_t[size];
                auto nextG = new int_t[size * 2];
                std::fill(nextF, nextF + size, 0);
                std::fill(nextG, nextG + 2 * size, 0);
                nextG += size;
                DFS2(to, vtx, nextF, nextG, result);
                for (int_t i = 0; i < maxDepth[to] - depth[vtx]; i++) {
                    result += g[i + 1] * (nextF)[i];
                    if (i) result += (nextG)[i] * f[i - 1];
                }
                //合并答案
                for (int_t i = 0; i < maxDepth[to] - depth[vtx]; i++) {
                    //合并g
                    g[i + 1] += f[i + 1] * (nextF)[i];
                    if (i) g[i - 1] += (nextG)[i];
                    f[i + 1] += (nextF)[i];
                }
    #ifdef DEBUG
                cout << "at vtx " << vtx << " done chd " << to << endl;
                for (int_t i = 0; i <= maxDepth[vtx] - depth[vtx]; i++)
                    cout << "f " << i << " = " << f[i] << endl;
                for (int_t i = 0; i <= maxDepth[vtx] - depth[vtx]; i++)
                    cout << "g " << i << " = " << g[i] << endl;
                cout << endl;
    #endif
            }
        }
    #ifdef DEBUG
        cout << "at vtx " << vtx << endl;
        for (int_t i = 0; i <= maxDepth[vtx] - depth[vtx]; i++)
            cout << "f " << i << " = " << f[i] << endl;
        for (int_t i = 0; i <= maxDepth[vtx] - depth[vtx]; i++)
            cout << "g " << i << " = " << g[i] << endl;
        cout << endl;
    #endif
        // result += g[0];
    }
    int n;
    int main() {
        scanf("%d", &n);
        for (int i = 1; i <= n - 1; i++) {
            int from, to;
            scanf("%d%d", &from, &to);
            graph[from].push_back(to);
            graph[to].push_back(from);
        }
        DFS1(1);
    #ifdef DEBUG
        for (int_t i = 1; i <= n; i++) {
            cout << "maxChd " << i << " = " << maxChd[i] << endl;
        }
    #endif
        int_t result = 0;
        int_t size = n + 1;
        auto f = new int_t[size];
        auto g = new int_t[size * 2];
        std::fill(f, f + size, 0);
        std::fill(g, g + size * 2, 0);
        DFS2(1, -1, f, g + size, result);
        cout << result << endl;
        return 0;
    }

     

     

     

  • CF1009F Dominant Indices

    直接长链剖分优化。

    一个点的答案直接从他的重子节点拖过来,然后暴力合并上其他非重子节点的答案。

    每条长链开个数组即可。

    复杂度为所有长链的长度和,$O(n)$。

    #include <iostream>
    #include <vector>
    using int_t = long long int;
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 1e6;
    int_t depth[LARGE + 1], maxChd[LARGE + 1], maxDepth[LARGE + 1];
    std::vector<int_t> graph[LARGE + 1];
    int_t results[LARGE + 1];
    void DFS1(int_t vtx, int_t from = -1) {
        if (from == -1) depth[vtx] = 1;
        maxDepth[vtx] = depth[vtx];
        for (int_t to : graph[vtx])
            if (to != from) {
                depth[to] = depth[vtx] + 1;
                DFS1(to, vtx);
                maxDepth[vtx] = std::max(maxDepth[vtx], maxDepth[to]);
                if (maxDepth[to] > maxDepth[maxChd[vtx]]) {
                    maxChd[vtx] = to;
                }
            }
    }
    //当前链使用的状态数组为arr
    void DFS2(int_t vtx, int_t from, int_t* arr) {
        arr[0] += 1;
        int_t& result = ::results[vtx];
        //递归重子节点
        if (maxChd[vtx]) {
            DFS2(maxChd[vtx], vtx, arr + 1);
            result = results[maxChd[vtx]] + 1;
        }
        //递归处理其他子节点
        for (int_t to : graph[vtx]) {
            if (to == from || to == maxChd[vtx]) continue;
            int_t* next = new int_t[maxDepth[to] - depth[vtx] + 1];
            std::fill(next, next + maxDepth[to] - depth[vtx] + 1, 0);
            DFS2(to, vtx, next + 1);
            for (int_t i = 1; i <= maxDepth[to] - depth[vtx]; i++) {
                arr[i] += next[i];
                if (arr[i] > arr[result]) {
                    result = i;
                } else if (arr[i] == arr[result] && i < result)
                    result = i;
            }
        }
        if (arr[result] == 1) result = 0;
    }
    int n;
    int main() {
        scanf("%d", &n);
        for (int i = 1; i <= n - 1; i++) {
            int from, to;
            scanf("%d%d", &from, &to);
            graph[from].push_back(to);
            graph[to].push_back(from);
        }
        DFS1(1);
    #ifdef DEBUG
        for (int_t i = 1; i <= n; i++)
            cout << "maxChd " << i << " = " << maxChd[i] << endl;
    #endif
        auto ptr = new int_t[n + 1];
        std::fill(ptr, ptr + n + 1, 0);
        DFS2(1, -1, ptr);
        for (int i = 1; i <= n; i++) {
            printf("%d\n", (int)results[i]);
        }
        return 0;
    }

     

  • 十二省联考2019 春节(清明)十二响

    把你骨灰都给你扬了!

    真是送分题…可惜因为心态原因考场上想出来正解了都没敢写..

    首先考虑链的情况怎么做?

    既然根不是链的端点,那么这条链一定分为两部分,把两部分的所有权值扔堆里,每次各拿出来两部分的最大值进行配对,然后扔到结果里。

    最后加起来即可。

    推广到树上呢?

    很自然的想到写个DFS,这个DFS返回一个堆,表示这棵子树的答案。

    然后对于一个点把他的子节点的堆拿出来每次取出所有的最大值合并

    这样也是正确的,可惜时间复杂度和空间复杂度都可以被卡到$O(n^2)$。

    怎么优化呢?考虑到一个到子树内叶子节点最远距离为x的点,堆里只会有x个取值。

    所以我们可以长链剖分。

    对于一个点,我们优先递归它的深子节点,然后把其他的子节点的答案合并到这个深子节点的堆上。

    具体实现就是对于一条长链顶,我们新建一个堆,然后这条长链都使用这一个堆。

    当从长链顶返回时,合并完答案后,删除这个堆即可。

    时间复杂度为所有长链长度之和乘上堆的复杂度,即$O(nlogn)$。

    #include <assert.h>
    #include <iostream>
    #include <numeric>
    #include <queue>
    #include <set>
    using int_t = long long int;
    using std::cin;
    using std::cout;
    using std::endl;
    
    using heap_t = std::multiset<int_t, std::greater<int_t>>;
    
    const int_t LARGE = 2e5;
    int_t maxChd[LARGE + 1], depth[LARGE + 1], maxDepth[LARGE + 1];
    int_t vals[LARGE + 1];
    int_t n;
    std::vector<int_t> graph[LARGE + 1];
    
    void DFS1(int_t vtx) {
        maxDepth[vtx] = depth[vtx];
        for (int_t to : graph[vtx]) {
            depth[to] = depth[vtx] + 1;
            DFS1(to);
            maxDepth[vtx] = std::max(maxDepth[vtx], maxDepth[to]);
        }
        for (int_t to : graph[vtx]) {
            if (maxDepth[to] > maxDepth[maxChd[vtx]]) {
                maxChd[vtx] = to;
            }
        }
        if (maxChd[vtx] == vtx) maxChd[vtx] = 0;
    }
    void DFS2(int_t vtx, heap_t* heap) {
        //先递归重子节点
        if (maxChd[vtx]) DFS2(maxChd[vtx], heap);
        //递归非重子节点
        std::vector<heap_t*> heaps;
        for (int_t to : graph[vtx]) {
            if (to != maxChd[vtx]) {
                auto next = new heap_t;
                DFS2(to, next);
                heaps.push_back(next);
            }
        }
        std::vector<int_t> result;
        //合并答案
        while (true) {
            bool hasNonEmpty = false;
            int_t max = 0;
            for (auto ptr : heaps) {
                if (ptr->empty() == false) {
                    max = std::max(max, *(ptr->begin()));
                    ptr->erase(ptr->begin());
                    hasNonEmpty = true;
                }
            }
            if (hasNonEmpty == false) break;
            assert(heap->empty() == false);
            if (heap->empty() == false) {
                auto ptr = heap->begin();
                max = std::max(max, *ptr);
                heap->erase(ptr);
            }
            result.push_back(max);
        }
        for (auto ptr : heaps) {
            delete ptr;
        }
        result.push_back(vals[vtx]);
        for (int_t x : result) heap->insert(x);
    }
    int main() {
        scanf("%lld", &n);
        for (int_t i = 1; i <= n; i++) scanf("%lld", &vals[i]);
        for (int_t i = 2; i <= n; i++) {
            int_t x;
            scanf("%lld", &x);
            graph[x].push_back(i);
        }
        depth[1] = 1;
        DFS1(1);
        auto heap = new heap_t;
        DFS2(1, heap);
        int_t result = std::accumulate(heap->begin(), heap->end(), 0ll);
        cout << result << endl;
        return 0;
    }