警示后人关于找重心的写法

P3806 【模板】点分治 1

yhylivedream @ 2024-08-11 15:48:31

我之前点分治一直这样写重心:

void G(int x, int fa, int qsz) {
  sz[x] = 1, mx[x] = 0;
  for (int v : e[x]) {
    if (v != fa && !f[v]) {
      G(v, x, qsz), sz[x] += sz[v];
      mx[x] = max(mx[x], sz[v]); 
    }
  }
  mx[x] = max(mx[x], qsz - sz[x]), mx[x] <= qsz / 2 && (rt = x);
}

但这样是错的,在 CF161D 里会挂掉正确的是这样的:

void G(int x, int fa, int qsz) {
  sz[x] = 1, mx[x] = 0;
  for (int v : e[x]) {
    if (v != fa && !f[v]) {
      G(v, x, qsz), sz[x] += sz[v];
      mx[x] = max(mx[x], sz[v]); 
    }
  }
  mx[x] = max(mx[x], qsz - sz[x]), mx[x] < mx[rt] && (rt = x);
}

by niumachaoren @ 2024-08-11 15:51:26

@yhylivedream 谢谢谢


by sunkuangzheng @ 2024-08-11 15:51:56

猜你在找:https://liu-cheng-ao.blog.uoj.ac/blog/2969。


|