版本比较
浅谈树链相关信息维护
正文
增加 3 行删除 3 行
384 行未改动全部展开3853863873883893893903913923933943953963973983993994004014024034044054064074084094094104114124136 行未改动384 行未改动全部展开 for(auto & y:vec[x]) if(y!=f){ dfs1(y,x); sz[x]+=sz[y]; hson[x]=(sz[hson[x]]>sz[y]?hson[x]:y); }}inline void dfs2(int x,int tp){ top[x]=tp; if(!hson[x]) return; dfs2(hson[x],tp); for(auto & y:vec[x]) if(y!=fa[x] and y!=hson[x]) dfs2(y,y); for(auto & y:vec[x]) if(y!=fa[x] and y!=hson[x]) dfs2(y,y);}int main(){ ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin>>n>>m>>root; for(int i=1; i<n; i++){ cin>>fr>>to; vec[fr].push_back(to); vec[to].push_back(fr); } } dfs1(root,0); dfs2(root,root); while(m--){ cin>>A>>B; while(top[A]!=top[B]) if(dep[top[A]]<dep[top[B]]) B=fa[top[B]]; else A=fa[top[A]]; cout<<(dep[A]<dep[B]?A:B)<<"\n"; } return 0;} }```:::
6 行未改动