P10604 BZOJ4317 Atm 的樹 題解

Athanasy發表於2024-07-04

題目連結:P10604 BZOJ4317 Atm 的樹

簡單點分樹題。我們考慮第 \(k\) 大問題常見的兩種做法:

  1. 樹上二分。

  2. 二分 \(+\) check。

顯然這題由於有若干個點對應的路徑關係,並不太好用前者,我們不可能為某個點經過的所有路徑都建立一棵權值線段樹,然後再權值線段樹上二分,因為路徑總數是 \(O(n^2)\) 的。

考慮第二種,我們二分 \(k\) 路徑長:

我們對於大型路徑問題常見的方式就是 點分治 或者 點分樹 來做。這題由於我們使用點分樹來做:

對於點分樹來說:

  1. 某個分治中心將會在它的後代分治中心消失,所以包含該分治中心的點的時間僅僅只有從該點到所有祖先分治中心節點的每個分治中心所在的時間樹,樹高為 \(O(\log{n})\),暴力訪問即為 \(O(\log{n})\)。本質上分治中心可以看做該樹在某一時間下的時間樹,特徵為以該分治中心作為根,其祖先分治中心方向的子樹全部消失

  2. 對於每個分治中心的貢獻路徑,當且僅當該路徑包含該分治中心,我們常見的把路徑分為兩類:
    (1) 以該分治中心作為端點進行衍生。
    (2) 以該分治中心作為分割點,從任意兩棵不相同的子樹中任取兩個點作為端點,拼接而成的路徑。

  3. 列舉祖先分治中心,基於點 \(2\) ,考慮同時經過當前點和該分治中心的點的路徑即為該分治中心對當前這個點作為分治中心的貢獻。

常見的點分樹維護:

  1. 維護以某個點作為根即分治中心的所有鏈資訊。

  2. 維護某個分治中心的父分治中心到該分治中心方向上的鏈資訊。

透過 \(1\) 去掉 \(2\),即為除了當前子樹以外的其他子樹的鏈資訊。

回到本題

考慮我們即為尋找所有經過當前查詢點的路徑中,\(\le mid\) 的數目,判斷是否 \(\ge k\) 即為 \(check\)。那麼最好的方式顯然可以考慮隨便維護一個權值資料結構,這邊直接使用了:動態開點樹狀陣列

考慮查詢,對於一個 \(fa\) 與當前點的貢獻,顯然我們可以使用樹上字首和處理出二者之間的路徑 \(d\),然後查詢 \(\le mid-d\) 的數目,即可查詢到該分治中心的貢獻,這個可以用上述點分樹維護的容斥思路算出,注意 \(d\) 也是一個答案,當然你可以可以考慮維護 \(0\) 這條空鏈,但要注意去掉,我自然是暴力判斷 \(d\) 了。二分的話,上界為 \(wn\),而 \(w\) 又比較小,所以這樣一來單點查詢的複雜度顯然就是 \(O(\log^3{n})\)

參照程式碼
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用於Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c) if (b & 1) (ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-') sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char)) return;
    if (x < 0) x = -x, putchar('-');
    if (x > 9) write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow) return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3()
    {
        one = tow = three = 0;
    }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y) x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y) x = y;
}

struct Hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9;
        x = (x ^ x >> 27) * 0x94d049bb133111eb;
        return x ^ x >> 31;
    }

    static size_t get(const uint64_t x)
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }

    template <typename T>
    size_t operator()(T x) const
    {
        return get(std::hash<T>()(x));
    }

    template <typename F, typename S>
    size_t operator()(pair<F, S> p) const
    {
        return get(std::hash<F>()(p.first)) ^ std::hash<S>()(p.second);
    }
};

constexpr int N = 1e5 + 10;
constexpr int T = log2(N) + 1;
constexpr int MX = 150000;
vector<pii> child[N];
int n, k;

struct
{
    int fa[N][T + 1], deep[N], pre[N];

    void dfs(const int curr, const int pa)
    {
        deep[curr] = deep[fa[curr][0] = pa] + 1;
        forn(i, 1, T) fa[curr][i] = fa[fa[curr][i - 1]][i - 1];
        for (const auto [nxt,val] : child[curr])
        {
            if (nxt != pa) pre[nxt] = pre[curr] + val, dfs(nxt, curr);
        }
    }

    int LCA(int x, int y) const
    {
        if (deep[x] < deep[y]) swap(x, y);
        forv(i, T, 0) if (deep[fa[x][i]] >= deep[y]) x = fa[x][i];
        if (x == y) return x;
        forv(i, T, 0) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
        return fa[x][0];
    }

    int dist(const int x, const int y) const
    {
        return pre[x] + pre[y] - 2 * pre[LCA(x, y)];
    }
} LCA;

struct
{
    hash2<int, int, Hash> bit[N];

    void add(const int root, int x)
    {
        while (x <= MX) bit[root][x]++, x += lowBit(x);
    }

    int query(const int root, int x)
    {
        int ans = 0;
        while (x)
        {
            if (bit[root].find(x) != bit[root].end()) ans += bit[root][x];
            x -= lowBit(x);
        }
        return ans;
    }
} cnt, cntFa;

struct
{
    bool del[N];
    int sumSize, maxSon, root;
    int siz[N], dist[N][T + 1], fa[N], deep[N];

    void makeRoot(const int curr, const int pa)
    {
        siz[curr] = 1;
        int currSize = 0;
        for (const auto nxt : child[curr] | views::keys)
        {
            if (del[nxt] or nxt == pa) continue;
            makeRoot(nxt, curr);
            siz[curr] += siz[nxt];
            uMax(currSize, siz[nxt]);
        }
        uMax(currSize, sumSize - siz[curr]);
        if (currSize < maxSon) maxSon = currSize, root = curr;
    }

    void dfs(const int curr, const int pa)
    {
        siz[curr] = 1;
        for (const int nxt : child[curr] | views::keys)
        {
            if (!del[nxt] and nxt != pa) dfs(nxt, curr), siz[curr] += siz[nxt];
        }
    }

    void buildCurr(const int curr, const int v, const int rt, const int pa)
    {
        for (const auto [nxt,val] : child[curr])
        {
            if (del[nxt] or nxt == pa) continue;
            cnt.add(rt, v + val);
            buildCurr(nxt, v + val, rt, curr);
        }
    }

    void buildFa(const int curr, const int v, const int rt, const int pa)
    {
        cntFa.add(rt, v);
        for (const auto [nxt,val] : child[curr])
        {
            if (del[nxt] or nxt == pa) continue;
            buildFa(nxt, v + val, rt, curr);
        }
    }

    void build(const int curr)
    {
        del[curr] = true;
        buildCurr(curr, 0, curr, 0);
        for (const auto [nxt,val] : child[curr])
        {
            if (del[nxt]) continue;
            sumSize = maxSon = siz[nxt];
            makeRoot(nxt, 0);
            dfs(root, 0);
            buildFa(nxt, val, root, curr);
            deep[root] = deep[fa[root] = curr] + 1;
            build(root);
        }
    }

    void init()
    {
        LCA.dfs(1, 0);
        sumSize = maxSon = n;
        makeRoot(1, 0);
        build(root);
        forn(son, 1, n)
        {
            for (int rt = fa[son]; rt; rt = fa[rt]) dist[son][deep[son] - deep[rt]] = LCA.dist(son, rt);
        }
    }

    bool check(const int curr, const int len) const
    {
        int ans = cnt.query(curr, len);
        for (int nxt = curr; fa[nxt]; nxt = fa[nxt])
        {
            const int d = dist[curr][deep[curr] - deep[fa[nxt]]];
            if (len >= d)
            {
                ans++;
                ans += cnt.query(fa[nxt], len - d);
                ans -= cntFa.query(nxt, len - d);
            }
        }
        return ans >= k;
    }

    int query(const int curr) const
    {
        int l = 1, r = MX;
        while (l < r)
        {
            const int mid = l + r >> 1;
            if (check(curr, mid)) r = mid;
            else l = mid + 1;
        }
        return l;
    }
} pointTree;

inline void solve()
{
    cin >> n >> k;
    forn(i, 1, n-1)
    {
        int u, v, val;
        cin >> u >> v >> val;
        child[u].emplace_back(v, val);
        child[v].emplace_back(u, val);
    }
    pointTree.init();
    forn(i, 1, n) cout << pointTree.query(i) << endl;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test) solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[預處理倍增陣列:\ O(n\log{n}) \]

\[構建點分樹:\ O(n\log{n}) \]

\[預處理兩個容斥樹狀陣列:\ O(n\log^2{n}) \]

\[預處理每個分治到祖先分治中心的距離陣列:\ O(n\log^2{n}) \]

\[單點查詢:\ 二分+點分樹上暴力跳祖先分治中心+樹狀陣列容斥查詢=O(\log^3{n}) \]

\[所以總複雜度為:\ O(n\log^3{n}) \]

相關文章