//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.3dp",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e4+3,M=3e5+3;",
            "int n,m,x,y,o,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,dis[N];",
            "\tinline void dfs(Re x,Re fa){",
            "\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);",
            "\t\t\t\tans=max(ans,dis[x]+dis[to]+a[i].w);",
            "\t\t\t\tdis[x]=max(dis[x],dis[to]+a[i].w);",
            "\t\t\t}",
            "\t}",
            "\tinline int sakura(){dfs(1,0);return ans;}",
            "}T1;",
            "struct Tree_Diam_DP2{",
            "\tint ans,dis[N][2];",
            "\tinline void dfs(Re x,Re fa){",
            "\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);",
            "\t\t\t\tif(dis[x][0]<(tmp=dis[to][0]+a[i].w))dis[x][1]=dis[x][0],dis[x][0]=tmp;",
            "\t\t\t\telse dis[x][1]=max(dis[x][1],tmp);",
            "\t\t\t}",
            "\t\tans=max(ans,dis[x][1]+dis[x][0]);",
            "\t}",
            "\tinline int sakura(){dfs(1,0);return ans;}",
            "}T2;",
            "int main(){",
            "\tin(n),m=n-1;",
            "    while(m--)in(x),in(y),add(x,y,1),add(y,x,1);",
            "    printf(\"%d\\n\",T1.sakura());",
            "//    printf(\"%d\\n\",T2.sakura());",
            "}",
            ""
        ],
        "description": "插入 树的直径"
    }
}