P6653 [YsOI2020]造林题解

· · 题解

萌新第一次发题解QAQ

基本推论

1.当将一个点插入到另一个点的时候,只有重心到这个点之间的路径上的点的值不会受到影响,也就是说,我们记录全部的点和记录重心到路径上的点是一样的.

2.距离重心越远的点,他的值越大(而且是严格单调)

思路与其他题解基本一致,但我不会写双哈希我们有一种方法可以100%避免HASH冲突.

那就是不用HASH

那就是用平衡树

请允许我介绍一种算法, 由字典树的思想与平衡树的框架拼凑而成.

设想一下,如果这道题直接用字典树,而又会有可能出现菊花图,也可能出现链,这就意味着我们需要的空间最坏接近 O(n^2) ,在 n<=2 * 10^6 的数据规模下,显然是不行的.

那么我们能不能牺牲一小部分的时间来换取空间呢? 答案是可以的.

我们放弃可以直接访问的性质,换成一种结构,它可以用log(n)的时间完成插入与访问.可否?
这种结构就是平衡树.

怎么用,我们来模拟一下.直接上图.

正确性验证(不重性验证)

我们捋一捋这道题的关键性质.

首先,一个基本推论:距离重心越远那么他的值就会越大(严格单调).

其次,我们记录的是重心到各个点,也就意味着,大家的起点都是相同的(都是重心).

那么就会有这样一个推论:两条重复的链必然长度是相同的. 因此不会出现相同的链条,被重复计算(而不被当作一条).

一些对比

至于为什么我会想这个算法呢?因为我以为要按字典序输出.
如果是按字典序输出,他就具有薄纱HASH的优越性了.

HASH一次插入 O(1) ,平衡树一次插入 O(log(n))

但如果是要按字典序输出,根据平衡树的 BST 性质可以直接输出,复杂度为 O(n) 【因为本质上其实就是以平衡树的方式重新建了一棵树】.

而HASH却要相当于字符串排序的时间复杂度.

完整代码:

双重心与其他题解的处理方式基本一致,那就是裂成两半.

平衡树我是用FHQ-TREAP实现的

#include <iostream>
#include <algorithm>
#include <math.h>
#include <list>

using namespace std;

const int N = 2 * 1000001;
list<int>line[N];
list<int>edge[N];
int n, siz[N], maxn[N], maxl[N], minn, tot, heart[10];
void dfs1(int u, int fat) {
    siz[u] = 1;
    for (auto it = line[u].begin(); it != line[u].end(); it++) {
        int v = *it;
        if (v == fat)continue;
        dfs1(v, u);
        if (siz[v] > maxl[u])maxl[u] = siz[v];
        siz[u] += siz[v];
    }
}

void dfs2(int u, int fat, int g) {
    maxn[u] = max(g, maxl[u]);
    if (maxn[u] == minn && tot < 2)
        heart[++tot] = u;
    if (maxn[u] < minn) {
        heart[tot = 1] = u;
        minn = maxn[u];
    }//双重心
    for (auto it = line[u].begin(); it != line[u].end(); it++) {
        int v = *it;
        if (v == fat)continue;
        dfs2(v, u, siz[u] - siz[v] + g);
    }
}

struct node {
    int num, rnk, cnt;
    node *ch[2];
    node *nxt;
    void init() {
        cnt = 1;
        rnk = rand();
        ch[0] = ch[1] = nxt = nullptr;
    }
}*root;

typedef pair<node*, node*> pr;
pr split(node *cur, int val) {
    if (cur == nullptr)return {nullptr, nullptr};
    if (cur->num <= val) {
        auto ntr = split(cur->ch[1], val);
        cur->ch[1] = ntr.first;
        return {cur, ntr.second};
    } else {
        auto ntr = split(cur->ch[0], val);
        cur->ch[0] = ntr.second;
        return {ntr.first, cur};
    }
}

node *merge(node *cur1, node *cur2) {
    if (cur1 == nullptr || cur2 == nullptr) {
        if (cur1 != nullptr)return cur1;
        return cur2;
    }
    if (cur1->rnk <= cur2->rnk) {
        cur1->ch[1] = merge(cur1->ch[1], cur2);
        return cur1;
    } else {
        cur2->ch[0] = merge(cur1, cur2->ch[0]);
        return cur2;
    }
}

int tmp = 0, last = 0;
node* dfs3(int u, int fat, node *cur) {
    auto ntr1 = split(cur, maxn[u]);
    auto ntr2 = split(ntr1.first, maxn[u] - 1);
    if (cur != root) {
        if (ntr2.second == nullptr) {
            node *ncur = new node;
            ncur->init();
            ncur->num = maxn[u];
            ntr2.second = ncur;
        } else ntr2.second->cnt++;
    }
    auto ntr = merge(ntr2.first, ntr2.second);
    cur = merge(ntr, ntr1.second);
    for (auto it = line[u].begin(); it != line[u].end(); it++) {
        int v = *it;
        if (v == fat || v == heart[1] || v == heart[2])continue; //确保两棵树互不相连
        ntr2.second->nxt = dfs3(v, u, ntr2.second->nxt);
    }
    return cur;
}

int ans[N * 2], sum;
void check(node *cur, int dep) {
    if (cur->ch[0] != nullptr)check(cur->ch[0], dep);
    if (cur->ch[1] != nullptr)check(cur->ch[1], dep);
    if (cur->nxt != nullptr)check(cur->nxt, dep + 1);
    ans[++sum] = cur->cnt;
}

bool cm_p(int a, int b) {
    return a < b;
}
int main() {
    minn = N;
    ios::sync_with_stdio(0);
    cin >> n;
    int x, y;
    for (int i = 1; i < n; i++) {
        cin >> x >> y;
        line[x].push_back(y);
        line[y].push_back(x);
    }
    dfs1(1, 1);//统计子树
    dfs2(1, 1, 0); //找重心
    root = new node; //root是重心
    root->init();
    root->cnt = tot;
    root->num = maxn[heart[1]];
    for (int i = 1; i <= tot; i++)
        root = dfs3(heart[i], heart[i], root);
    check(root, 0);
    cout << sum << "\n";
    sort(ans + 1, ans + sum + 1, cm_p);
    for (int i = 1; i <= sum; i++)
        cout << ans[i] << "\n";
    return 0;
}

最后送一个样例三的重心建完后的图 (以便调试)