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

{
    "点分治  (询问是否有长度等于K的路径)": {
        "prefix": "acm-4.3分各子树求解",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e4+3,M=103,INF=1e7+3,inf=2e9;",
            "int n,m,o,x,y,z,T,maxK,K[M],Ans[M],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],vis[N],tmp[N],dis[N],maxp[N],size[N];bool judge[INF];",
            "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>maxK)return;",
            "\tdd[++dd[0]]=dis[x]=dis[fa]+w;",
            "\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){",
            "\ttmp[0]=0,judge[tmp[++tmp[0]]=0]=1;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]){",
            "\t\t\tdis[x]=dd[0]=0,getdis(to,x,a[i].w);",
            "\t\t\tfor(Re j=1;j<=dd[0];++j)",
            "\t\t\t\tfor(Re k=1;k<=T;++k)",
            "\t\t\t\t\tif(K[k]>=dd[j])Ans[k]|=judge[K[k]-dd[j]];",
            "\t\t\tfor(Re j=1;j<=dd[0];++j)judge[tmp[++tmp[0]]=dd[j]]=1;",
            "\t\t}",
            "\tfor(Re i=1;i<=tmp[0];++i)judge[tmp[i]]=0;",
            "}",
            "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(T),m=n-1;",
            "\twhile(m--)in(x),in(y),in(z),add(x,y,z),add(y,x,z);",
            "\tfor(Re i=1;i<=T;++i)in(K[i]),maxK=max(maxK,K[i]);",
            "\tsum=n,maxp[rt=0]=inf,getrt(1,0),sakura(rt);",
            "\tfor(Re i=1;i<=T;++i)puts(Ans[i]?\"AYE\":\"NAY\");",
            "}",
            ""
        ],
        "description": "插入 点分治  (询问是否有长度等于K的路径)"
    }
}