应该是lca的问题:
inline int LCA(int x,int y) { int res=lca(x,y),res1=lca(x,root),res2=lca(y,root); if (res1==x&&res2!=root) return x; if (res2==y&&res1!=root) return y; if (lca(res,root)!=res) return res; return depth[res1]>depth[res2]?res1:res2; }