//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<cstdio>",
            "#define Re register int",
            "using namespace std;",
            "const int N=5e5+3;",
            "int n,m,x,y,T,root;",
            "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 Killed_Tree{",
            "\tstruct QAQ{int to,next;}a[N<<1];",
            "\tint o,head[N];",
            "\tinline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}",
            "\tint id_o,id[N],fa[N],son[N],top[N],deep[N],size[N];",
            "\tinline void dfs1(Re x,Re Fa){",
            "\t\tsize[x]=1,deep[x]=deep[fa[x]=Fa]+1;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=Fa){",
            "\t\t\t\tdfs1(to,x);",
            "\t\t\t\tsize[x]+=size[to];",
            "\t\t\t\tif(size[to]>size[son[x]])son[x]=to;",
            "\t\t\t}",
            "\t}",
            "\tinline void dfs2(Re x,Re rt){",
            "\t\tid[x]=++id_o,top[x]=rt;",
            "\t\tif(!son[x])return;",
            "\t\tdfs2(son[x],rt);",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=fa[x]&&to!=son[x])dfs2(to,to);",
            "\t}",
            "\tinline void kill(Re rt){dfs1(rt,0),dfs2(rt,rt);}",
            "\tinline int lca(Re x,Re y){",
            "\t\twhile(top[x]!=top[y]){",
            "\t\t\tif(deep[top[x]]<deep[top[y]])swap(x,y);",
            "\t\t\tx=fa[top[x]];",
            "\t\t}",
            "\t\tif(deep[x]>deep[y])swap(x,y);",
            "\t\treturn x;",
            "\t}",
            "}T1;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T),in(root),m=n-1;",
            "\twhile(m--)in(x),in(y),T1.add(x,y),T1.add(y,x);",
            "\tT1.kill(root);",
            "\twhile(T--)in(x),in(y),printf(\"%d\\n\",T1.lca(x,y));",
            "}",
            ""
        ],
        "description": "插入 最近公共祖先LCA"
    }
}