警示后人
查看原帖
警示后人
552165
ComplexPlanck楼主2022/12/16 12:37

注意子树大小。

如果你和我一样,使用了如下 naive 的代码:

void get_root(int u, int father, int allar)
{
    large[u] = 1, maxub[u] = 0;
    for (int i = head[u]; i != -1; i = nxt[i])
    {
         int e = to[i];
         if (vis[e] || e == father) continue;
         get_root(e, u, allar);
         large[u] += large[e];
         maxub[u] = std::max(maxub[u], large[e]);
    }
    maxub[u] = std::max(maxub[u], allar - maxub[u]);
    if (!root || maxub[root] > maxub[u]) root = u;
    return;
}
void dfsearch(int u)
{
    vis[u] = true, tr1[u].init(large[u] + DEL), tr2[u].init(large[u] + DEL);
    for (int i = head[u]; i != -1; i = nxt[i])
    {
         int e = to[i], sz = large[e];
         if (vis[e]) continue;
         root = 0, get_root(e, u, sz);
         // !!!!!!!!
         dAdd(u, root), fa[root] = u, dfsearch(root);
    }
    return;
}

那么显然地,你的 large/size 求的是错的,应该再跑一边,也就是:root = 0, get_root(e, u, sz), get_root(root, -1, sz);

神奇地可以过题面样例和讨论区中的样例。

2022/12/16 12:37
加载中...