//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.3Island 求基环树森林直径之和",
        "body": [
            "#pragma comment(linker, \"/STACK:102400000,102400000\")",
            "#include<cstdio>",
            "#define Re register int",
            "const int N=1e6+3;",
            "#define LL long long",
            "int n,m,x,y,o=1,h,t,st,cnt,In[N],cir[N],pan[N],head[N];LL ans,tmp,Q[N<<1],S[N<<1],dp[N<<1],dis[N];",
            "struct QAQ{int w,to,next;}a[N<<1];",
            "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 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 LL max(LL a,LL b){return a>b?a:b;}",
            "inline int find_circle(Re x,Re p){",
            "\tif(In[x]){st=x,cir[++cnt]=x,pan[x]=1;return 1;}",
            "\tIn[x]=1;",
            "\tfor(Re i=head[x];i;i=a[i].next)",
            "\t\tif(i!=(p^1)&&find_circle(a[i].to,i)){",
            "\t\t\tpan[x]=1;",
            "\t\t\tif(x==st){S[0]=S[1]-a[i].w;return 0;}",
            "\t\t\tcir[++cnt]=x,S[cnt]=S[cnt-1]+a[i].w;",
            "\t\t\treturn 1;",
            "\t\t}",
            "\treturn 0;",
            "}",
            "inline void Tree_dp(Re x){",
            "\tpan[x]=1;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!pan[to=a[i].to]){",
            "\t\t\tTree_dp(to);",
            "\t\t\ttmp=max(tmp,dis[x]+a[i].w+dis[to]);",
            "\t\t\tdis[x]=max(dis[x],dis[to]+a[i].w);",
            "\t\t}",
            "}",
            "inline LL pp(Re j){return dp[j]-S[j];}",
            "inline LL sakura(Re root){",
            "\tS[0]=cnt=st=0;find_circle(root,0);",
            "\tLL ans1=0,ans2=0;tmp=0;",
            "\tfor(Re i=1;i<=cnt;++i){",
            "\t\ttmp=0,Tree_dp(cir[i]),ans1=max(ans1,tmp);",
            "\t\tdp[i+cnt]=dp[i]=dis[cir[i]];",
            "\t\tS[i+cnt]=S[i+cnt-1]+S[i]-S[i-1];",
            "\t}",
            "\th=1,t=0,Q[++t]=1;//ans=max(dp[i]+dp[j]+S[i]-S[j]) i-j+1<=cnt i-cnt+1<=j<i",
            "\tfor(Re i=2;i<=(cnt<<1);++i){",
            "\t\twhile(h<=t&&Q[h]<i-cnt+1)++h;",
            "\t\tif(h<=t)ans2=max(ans2,dp[i]+S[i]+pp(Q[h]));",
            "\t\twhile(h<=t&&pp(Q[t])<=pp(i))--t;",
            "\t\tQ[++t]=i;",
            "\t}",
            "\treturn max(ans1,ans2);",
            "}",
            "int main(){",
            "\tin(n);",
            "\tfor(Re i=1;i<=n;++i)in(x),in(y),add(i,x,y),add(x,i,y);",
            "\tfor(Re i=1;i<=n;++i)if(!pan[i])ans+=sakura(i);",
            "\tprintf(\"%lld\",ans);",
            "}"
        ],
        "description": "插入 基环树"
    }
}