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

{
    "树的重心": {
        "prefix": "acm-4.3add",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define Re register int",
            "using namespace std;",
            "const int N=2e4+3,inf=2e9;",
            "int n,m,x,y,o,T,head[N];",
            "struct QAQ{int w,to,next;}a[N<<1];",
            "inline void add(Re x,Re y,Re z){a[++o].w=z,a[o].to=y,a[o].next=head[x],head[x]=o;}",
            "inline void in(Re &x){",
            "\tRe fu=0;x=0;char ch=getchar();",
            "\twhile(ch<'0'||ch>'9')fu|=ch=='-',ch=getchar();",
            "\twhile(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();",
            "\tx=fu?-x:x;",
            "}",
            "struct Tree_Diam_DP1{",
            "\tint ans,dp[N],size[N];",
            "\tinline void dfs(Re x,Re fa){",
            "\t\tsize[x]=1,dp[x]=0;",
            "\t\tfor(Re i=head[x],to,tmp;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=fa){",
            "\t\t\t\tdfs(to,x),size[x]+=size[to];",
            "\t\t\t\tdp[x]=max(dp[x],size[to]);",
            "\t\t\t}",
            "\t\tif(n-size[x])dp[x]=max(dp[x],n-size[x]);",
            "\t}",
            "\tinline void sakura(){",
            "\t\tdfs(1,0),dp[ans=0]=inf;",
            "\t\tfor(Re i=1;i<=n;++i)if(dp[i]<dp[ans])ans=i;",
            "\t}",
            "}T1;",
            "int main(){",
            "\tin(T);",
            "\twhile(T--){",
            "\t\tmemset(head,0,sizeof(head));",
            "\t\tin(n),m=n-1,o=0;",
            "\t    while(m--)in(x),in(y),add(x,y,1),add(y,x,1);",
            "\t    T1.sakura();",
            "\t\tprintf(\"%d %d\\n\",T1.ans,T1.dp[T1.ans]);",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 树的重心"
    }
}