//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-3.2add",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=5e5+3,M=5e5+3,logN=19,inf=2e9;",
            "int T,n,m,o,root,fa[N],head[N],Tree[M],M1[N][23],M2[N][23];LL MinTree;",
            "struct QAQ{int w,to,next;}a[M<<1];",
            "struct QWQ{int x,y,w;inline bool operator<(QWQ O)const{return w<O.w;}}A[M];",
            "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;}",
            "struct LCA{",
            "\tint deep[N],ant[N][23];",
            "\tinline void dfs(Re x,Re fa){",
            "\t\tdeep[x]=deep[fa]+1,ant[x][0]=fa;",
            "\t\tfor(Re i=1;(1<<i)<=deep[x];++i){",
            "\t\t\tant[x][i]=ant[ant[x][i-1]][i-1];",
            "\t\t\tM1[x][i]=max(M1[x][i-1],M1[ant[x][i-1]][i-1]);",
            "\t\t\tM2[x][i]=max(M2[x][i-1],M2[ant[x][i-1]][i-1]);",
            "\t\t\tif(M1[x][i-1]!=M1[ant[x][i-1]][i-1])",
            "\t\t\t\tM2[x][i]=max(M2[x][i],min(M1[x][i-1],M1[ant[x][i-1]][i-1]));",
            "\t\t}",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=fa)M1[to][0]=a[i].w,M2[to][0]=-inf,dfs(to,x);",
            "\t}",
            "\tinline int lca(Re x,Re y){",
            "\t\tif(deep[x]>deep[y])swap(x,y);",
            "\t\tfor(Re i=logN;i>=0;--i)if(deep[x]<=deep[y]-(1<<i))y=ant[y][i];",
            "\t\tif(x==y)return y;",
            "\t\tfor(Re i=logN;i>=0;--i)",
            "\t\t\tif(ant[x][i]^ant[y][i])x=ant[x][i],y=ant[y][i];",
            "\t\treturn ant[x][0];",
            "\t}",
            "\tinline int ask_max(Re x,Re Ant,Re w){",
            "\t\tRe ans=-inf;",
            "\t\tfor(Re i=logN;i>=0;--i)",
            "\t\t\tif(deep[ant[x][i]]>=deep[Ant]){",
            "\t\t\t\tif(w!=M1[x][i])ans=max(ans,M1[x][i]);",
            "\t\t\t\telse ans=max(ans,M2[x][i]);",
            "\t\t\t\tx=ant[x][i];",
            "\t\t\t}",
            "\t\treturn ans;",
            "\t}",
            "}T1;",
            "inline int find(Re x){return x==fa[x]?x:fa[x]=find(fa[x]);}",
            "inline void kruscal(){",
            "\tsort(A+1,A+m+1);",
            "\tfor(Re i=1;i<=n;++i)fa[i]=i;",
            "\tfor(Re i=1;i<=m;++i){",
            "\t\tRe x=A[i].x,y=A[i].y,w=A[i].w;",
            "\t\tif(find(x)!=find(y)){",
            "\t\t\tMinTree+=w,Tree[i]=1;",
            "\t\t\tadd(x,y,w),add(y,x,w);",
            "\t\t\tfa[find(x)]=find(y);",
            "\t\t}",
            "\t}",
            "}",
            "int main(){",
            "\tin(n),in(m),root=1;",
            "\tfor(Re i=1;i<=m;++i)in(A[i].x),in(A[i].y),in(A[i].w);",
            "\tkruscal();",
            "\tM1[root][0]=0,M2[root][0]=-inf,T1.dfs(root,0);",
            "\tLL ans=1e17;",
            "\tfor(Re i=1;i<=m;++i)",
            "\t\tif(!Tree[i]){",
            "\t\t\tRe x=A[i].x,y=A[i].y,w=A[i].w;",
            "\t\t\tRe lca=T1.lca(x,y);",
            "\t\t\tans=min(ans,MinTree+w-max(T1.ask_max(x,lca,w),T1.ask_max(y,lca,w)));",
            "\t\t}",
            "\tprintf(\"%lld\",ans);",
            "}",
            ""
        ],
        "description": "插入 严格次小生成树"
    }
}