//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.3Kingdom and its Cities",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3,logN=17;",
            "int n,m,x,y,o,T,dfn_o,dfn[N],deep[N],head[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;",
            "}",
            "struct LCA{",
            "\tint ant[N][20];",
            "\tinline void dfs(Re x,Re fa){",
            "\t\tdeep[x]=deep[ant[x][0]=fa]+1,dfn[x]=++dfn_o;",
            "\t\tfor(Re j=1;(1<<j)<=deep[x];++j)ant[x][j]=ant[ant[x][j-1]][j-1];",
            "\t\tfor(Re i=head[x];i;i=a[i].next)if(a[i].to!=fa)dfs(a[i].to,x);",
            "\t}",
            "\tinline int lca(Re x,Re y){",
            "\t\tif(deep[x]<deep[y])swap(x,y);",
            "\t\tfor(Re j=logN;j>=0;--j)if(deep[ant[x][j]]>=deep[y])x=ant[x][j];",
            "\t\tif(x==y)return x;",
            "\t\tfor(Re j=logN;j>=0;--j)if(ant[x][j]!=ant[y][j])x=ant[x][j],y=ant[y][j];",
            "\t\treturn ant[x][0];",
            "\t}",
            "}T1;",
            "inline bool cmp(Re x,Re y){return dfn[x]<dfn[y];}",
            "struct Virtual_Tree{",
            "\tint t,o,ans,A[N],Q[N],key[N],head[N];",
            "\tstruct QAQ{int to,next;}a[N<<1];",
            "\tinline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}",
            "\tinline void insert(Re x){// 撤销操作",
            "\t\tRe lca=T1.lca(Q[t],x);",
            "\t\twhile(t>1&&deep[Q[t-1]]>deep[lca])add(Q[t-1],Q[t]),--t;",
            "\t\tif(t&&deep[Q[t]]>deep[lca])add(lca,Q[t--]);",
            "\t\tif(!t||Q[t]!=lca)Q[++t]=lca;",
            "\t\tQ[++t]=x;",
            "\t}",
            "\tinline void dfs(Re x){// 撤销操作",
            "\t\tif(key[x])",
            "\t\t\tfor(Re i=head[x],to;i;i=a[i].next){",
            "\t\t\t\tdfs(to=a[i].to);",
            "\t\t\t\tif(key[to])key[to]=0,++ans;",
            "\t\t\t}",
            "\t\telse{",
            "\t\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\t\tdfs(to=a[i].to),key[x]+=key[to],key[to]=0;",
            "\t\t\tif(key[x]>1)key[x]=0,++ans;",
            "\t\t}",
            "\t\thead[x]=0;",
            "\t}",
            "\tinline void sakura(){",
            "\t\tin(m);Re flag=1;ans=o=t=0;",
            "\t\tfor(Re i=1;i<=m;++i)in(A[i]),key[A[i]]=1;",
            "\t\tfor(Re i=1;i<=m&&flag;++i)flag&=(!key[T1.ant[A[i]][0]]);",
            "\t\tif(!flag){",
            "\t\t\tfor(Re i=1;i<=m;++i)key[A[i]]=0;",
            "\t\t\tputs(\"-1\");return;",
            "\t\t}",
            "\t\tsort(A+1,A+m+1,cmp),Q[++t]=1;",
            "\t\tfor(Re i=1+(A[1]==1);i<=m;++i)insert(A[i]);",
            "\t\twhile(t>1)add(Q[t-1],Q[t]),--t;",
            "\t\tdfs(1),key[1]=0;",
            "\t\tprintf(\"%d\\n\",ans);",
            "\t}",
            "}T2;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),m=n-1;",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tT1.dfs(1,0),in(T);",
            "\twhile(T--)T2.sakura();",
            "}",
            ""
        ],
        "description": "插入 虚树"
    }
}