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

{
    "Race": {
        "prefix": "acm-4.3长度等于K的路径最小边数",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=2e5+3,INF=1e6+3,inf=2e9;",
            "int n,m,o,x,y,z,K,Ans,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){",
            "    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;",
            "}",
            "int rt,sum,dd[N],ddd[N],dep[N],Min[INF],tmp[N],dis[N],vis[N],maxp[N],size[N];",
            "inline void getrt(Re x,Re fa){",
            "\tsize[x]=1,maxp[x]=0;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]&&to!=fa)",
            "\t\t\tgetrt(to,x),size[x]+=size[to],maxp[x]=max(maxp[x],size[to]);",
            "\tmaxp[x]=max(maxp[x],sum-size[x]);",
            "\tif(maxp[x]<maxp[rt])rt=x;",
            "}",
            "inline void getdis(Re x,Re fa,Re w){",
            "\tif(dis[fa]+w>K)return;",
            "\tdd[++dd[0]]=dis[x]=dis[fa]+w,ddd[dd[0]]=dep[x]=dep[fa]+1;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]&&to!=fa)getdis(to,x,a[i].w);",
            "}",
            "inline void calc(Re x){",
            "\tdis[x]=dep[x]=tmp[0]=0,Min[tmp[++tmp[0]]=0]=0;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]){",
            "\t\t\tdd[0]=0,getdis(to,x,a[i].w);",
            "\t\t\tfor(Re i=1;i<=dd[0];++i)Ans=min(Ans,ddd[i]+Min[K-dd[i]]);",
            "\t\t\tfor(Re i=1;i<=dd[0];++i)Min[dd[i]]=min(Min[dd[i]],ddd[i]),tmp[++tmp[0]]=dd[i];",
            "\t\t}",
            "\tfor(Re i=1;i<=tmp[0];++i)Min[tmp[i]]=inf;",
            "}",
            "inline void sakura(Re x){",
            "\tRe now=sum;vis[x]=1,calc(x);",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to])",
            "\t\t\tsum=size[to]>size[x]?now-size[x]:size[to],maxp[rt=0]=inf,getrt(to,x),sakura(rt);",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(K),m=n-1;",
            "\tfor(Re i=0;i<=K;++i)Min[i]=inf;",
            "\twhile(m--)in(x),in(y),in(z),++x,++y,add(x,y,z),add(y,x,z);",
            "\tAns=inf,sum=n,maxp[rt=0]=inf,getrt(1,0),sakura(rt);",
            "\tprintf(\"%d\\n\",(Ans==inf)?-1:Ans);",
            "}",
            ""
        ],
        "description": "插入 Race"
    }
}