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

{
    "Prufer序列": {
        "prefix": "acm-4.3get_prufer",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=5e6+3;",
            "int n,op,fa[N],pf[N],son[N];LL ans;",
            "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;",
            "}",
            "inline LL get_prufer(){",
            "\tfor(Re i=1;i<=n-1;++i)in(fa[i]),++son[fa[i]];",
            "\tfor(Re i=1,j=1;i<=n-2;++i,++j){",
            "\t\twhile(son[j])++j;pf[i]=fa[j];",
            "\t\twhile(i<=n-2&&!--son[pf[i]]&&pf[i]<j)pf[i+1]=fa[pf[i]],++i;",
            "\t}",
            "//\tprintf(\"Prufer: \");for(Re i=1;i<=n-2;++i)printf(\"%d \",pf[i]);puts(\"\");",
            "\tfor(Re i=1;i<=n-2;++i)ans^=(LL)i*pf[i];",
            "\treturn ans;",
            "}",
            "inline LL get_fa(){",
            "\tfor(Re i=1;i<=n-2;++i)in(pf[i]),++son[pf[i]];pf[n-1]=n;",
            "\tfor(Re i=1,j=1;i<=n-1;++i,++j){",
            "\t\twhile(son[j])++j;fa[j]=pf[i];",
            "\t\twhile(i<=n-1&&!--son[pf[i]]&&pf[i]<j)fa[pf[i]]=pf[i+1],++i;",
            "\t}",
            "//\tprintf(\"fa: \");for(Re i=1;i<=n-1;++i)printf(\"%d \",fa[i]);puts(\"\");",
            "\tfor(Re i=1;i<=n-1;++i)ans^=(LL)i*fa[i];",
            "\treturn ans;",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(op),printf(\"%lld\\n\",op<2?get_prufer():get_fa());",
            "}",
            ""
        ],
        "description": "插入 Prufer序列"
    }
}