//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 的格式要求是 "插入 [该代码片段的中文名]"

{
    "长链剖分优化dp": {
        "prefix": "acm-4.3Dominant Indices",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e6+3;",
            "int n,m,x,y,o,Sz[N],son[N],Ans[N],dep[N],head[N];int *SZ=Sz,*dp[N];",
            "struct QAQ{int to,next;}a[N<<1];",
            "inline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}",
            "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;",
            "}",
            "inline void dfs1(Re x,Re fa){",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)!=fa){",
            "\t\t\tdfs1(to,x);",
            "\t\t\tif(dep[to]>dep[son[x]])son[x]=to;",
            "\t\t}",
            "\tdep[x]=dep[son[x]]+1;",
            "}",
            "inline void dfs2(Re x,Re fa){",
            "\tdp[x][0]=1;",
            "\tif(son[x])dp[son[x]]=dp[x]+1,dfs2(son[x],x),Ans[x]=Ans[son[x]]+1;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)!=fa&&to!=son[x]){",
            "\t\t\tdp[to]=SZ,SZ+=dep[to],dfs2(to,x);",
            "\t\t\tfor(Re j=1;j<=dep[to];++j){",
            "\t\t\t\tdp[x][j]+=dp[to][j-1];",
            "\t\t\t\tif(dp[x][j]>dp[x][Ans[x]])Ans[x]=j;",
            "\t\t\t\telse if(dp[x][j]==dp[x][Ans[x]])Ans[x]=min(Ans[x],j);",
            "\t\t\t}",
            "\t\t}",
            "\tif(dp[x][Ans[x]]==1)Ans[x]=0;",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),m=n-1;",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tdfs1(1,0),dp[1]=SZ,SZ+=dep[1],dfs2(1,0);",
            "\tfor(Re i=1;i<=n;++i)printf(\"%d\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 长链剖分优化dp"
    }
}