NOIP求助 树剖求LCA30分MLE

P3379 【模板】最近公共祖先(LCA)

@[BLUE_EYE](/space/show?uid=58354) 注意看 ```cpp void dfs1(int x){ size[x] = 1;de[x] = de[fa[x]] + 1; for(int i=head[x];i;i=a[i].next){ int to = a[i].to; if(to == fa[x]) continue; fa[to] = x; dfs1(to); size[to] += size[x]; if(size[to] > size[son[x]]) son[x] = to; } } ``` 把 ```cpp size[to] += size[x]; ``` 改成 ```cpp size[x] += size[to]; ``` 因为size[x]记录的是以x为根的树的结点数量,选重儿子肯定要选子树的size[v]最大的子结点呀。
by throusea @ 2018-11-09 09:05:18


@[throusea](/space/show?uid=59951) 然后他TLE了。。。。。我也发现了这个,但是加上去开O2MLE,不开TLE
by Thosaka_Forest @ 2018-11-09 09:06:01


@[g21glf](/space/show?uid=31639) 竟然还有!
by throusea @ 2018-11-09 09:06:48


@[BLUE_EYE](/space/show?uid=58354) ok你犯了两个错,一个是上面那位巨佬指出的
by Thosaka_Forest @ 2018-11-09 09:08:16


另一个是这一段: ```cpp void dfs2(int x){ id[x] = ++cnt;id_map[cnt] = x; if(x == son[fa[x]]) top[x] = top[fa[x]]; else top[x] = x; if(son[x]) dfs2(son[x]); for(int i=head[x];i;i=a[i].next){ int to = a[i].to; if(son[to] == to || to == fa[x]) continue; dfs2(to); } } ``` 应该是if(son[x]==to.....),因为x的重儿子已经dfs过了,所以遇到他的重儿子就跳过
by Thosaka_Forest @ 2018-11-09 09:09:33


@[throusea](/space/show?uid=59951) 找出来了qwq
by Thosaka_Forest @ 2018-11-09 09:09:43


@[throusea](/space/show?uid=59951) 还有一个 ```cpp if(son[to] == to || to == fa[x]) continue; ``` 这里很明显是son[x]好吧。不知道你有没有理解树剖。 交上去A了。
by throusea @ 2018-11-09 09:10:23


QAQ 蒟蒻太久没打树剖练板子没想到这么多错
by BLUE_EYE @ 2018-11-09 09:14:00


变成轻链剖分了
by BLUE_EYE @ 2018-11-09 09:14:27


谢谢大佬们 @[throusea](/space/show?uid=59951) @[g21glf](/space/show?uid=31639)
by BLUE_EYE @ 2018-11-09 09:19:10


|