//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "最近公共祖先LCA": {
        "prefix": "acm-4.3倍增",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define Re register int",
            "using namespace std;",
            "const int N=5e5+3,M=5e5+3,logN=19;",
            "int T,Q,n,m,x,y,o,root,head[N];",
            "struct QAQ{int to,next;}a[M<<1];",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "struct LCA{",
            "\tint deep[N],ant[N][23];",
            "\tinline void dfs(Re x,Re fa){",
            "\t\tdeep[x]=deep[ant[x][0]=fa]+1;",
            "\t\tfor(Re i=1;(1<<i)<=deep[x];++i)ant[x][i]=ant[ant[x][i-1]][i-1];",
            "\t\tfor(Re i=head[x];i;i=a[i].next)if(a[i].to!=fa)dfs(a[i].to,x);",
            "\t}",
            "\tinline int lca(Re x,Re y){",
            "\t\tif(deep[x]<deep[y])swap(x,y);",
            "\t\tfor(Re i=logN;i>=0;--i)if(deep[ant[x][i]]>=deep[y])x=ant[x][i];",
            "\t\tif(x==y)return x;",
            "\t\tfor(Re i=logN;i>=0;--i)if(ant[x][i]!=ant[y][i])x=ant[x][i],y=ant[y][i];",
            "\t\treturn ant[x][0];",
            "\t}",
            "}T1;",
            "inline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}",
            "int main(){",
            "\tin(n),in(Q),in(root),m=n-1;",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tT1.dfs(root,0);",
            "\twhile(Q--)in(x),in(y),printf(\"%d\\n\",T1.lca(x,y));",
            "}",
            ""
        ],
        "description": "插入 最近公共祖先LCA"
    }
}