[BJOI 2010]次小生成树Tree

时间:2023-03-09 00:33:16
[BJOI 2010]次小生成树Tree

Description

小 C 最近学了很多最小生成树的算法,Prim 算法、Kurskal 算法、消圈算法等等。 正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了。小 P 说,让小 C 求出一个无向图的次小生成树,而且这个次小生成树还得是严格次小的,也就是说: 如果最小生成树选择的边集是 EM,严格次小生成树选择的边集是 ES,那么需要满足:(value(e) 表示边 e的权值) [BJOI 2010]次小生成树Tree 这下小 C 蒙了,他找到了你,希望你帮他解决这个问题。

Input

第一行包含两个整数N 和M,表示无向图的点数与边数。 接下来 M行,每行 3个数x y z 表示,点 x 和点y之间有一条边,边的权值为z。

Output

包含一行,仅一个数,表示严格次小生成树的边权和。(数据保证必定存在严格次小生成树)

Sample Input

5 6
1 2 1
1 3 2
2 4 3
3 5 4
3 4 3
4 5 6

Sample Output

11

Hint

数据中无向图无自环; 50% 的数据N≤2 000 M≤3 000; 80% 的数据N≤50 000 M≤100 000; 100% 的数据N≤100 000 M≤300 000 ,边权值非负且不超过 10^9 。

题解

次小生成树模板。简便地直接用$LCA$做。唯一注意的是由于它要求严格的次小生成树,所以我们$LCA$时还要记得保存次大值。(防止边权相等)

 #include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const LL N=; LL n,m,op,x,y,p,q,d=2e15;
struct aa
{
LL u,v,c;
}lin[N*+];
bool comp(aa a,aa b); LL mst,cnt;
struct bb
{
LL to,next,cost;
}edge[N*+];
LL path[N+],top;
bool vis[N*+];
void Add(LL u,LL v,LL c); LL set[N+];
LL Find(LL x); LL f[N+][],maxn[N+][],sub[N+][];
LL dep[N+];
void Dfs(LL x,LL depth);
void Lca(LL x,LL y,LL c); int main()
{
scanf("%lld%lld",&n,&m);
op=log2(n);
for (LL i=;i<=m;i++) scanf("%lld%lld%lld",&lin[i].u,&lin[i].v,&lin[i].c);
sort(lin+,lin+m+,comp);
for (LL i=;i<=m;i++)
{
p=Find(lin[i].u);
q=Find(lin[i].v);
if (p!=q)
{
set[p]=q;
cnt++;
mst+=lin[i].c;
vis[i]=;
Add(lin[i].u,lin[i].v,lin[i].c);
Add(lin[i].v,lin[i].u,lin[i].c);
if (cnt==n-) break;
}
}
if (cnt<n-)
{
printf("No MST!\n");
return ;
}
Dfs(,);
for (LL t=;t<=op;t++)
for (LL i=;i<=n;i++)
if (f[i][t-])
{
f[i][t]=f[f[i][t-]][t-];
x=maxn[i][t-];y=sub[i][t-];
p=maxn[f[i][t-]][t-];q=sub[f[i][t-]][t-];
if (x==p){maxn[i][t]=x;sub[i][t]=max(y,q);}
else if (x>p){maxn[i][t]=x;sub[i][t]=max(p,y);}
else if (x<p){maxn[i][t]=p;sub[i][t]=max(x,q);}
}
for (LL i=;i<=m;i++) if (!vis[i]) Lca(lin[i].u,lin[i].v,lin[i].c);
if (d==2e15) printf("No SST!");
else printf("%lld\n",mst+d);
return ;
} bool comp(aa a,aa b){return a.c<b.c;}
void Add(LL u,LL v,LL c)
{
edge[++top].to=v;
edge[top].next=path[u];
edge[top].cost=c;
path[u]=top;
}
LL Find(LL x){return set[x] ? set[x]=Find(set[x]):x;}
void Dfs(LL x,LL depth)
{
dep[x]=depth;
for (LL i=path[x];i;i=edge[i].next) if (!dep[edge[i].to])
{
f[edge[i].to][]=x;
maxn[edge[i].to][]=edge[i].cost;
Dfs(edge[i].to,depth+);
}
}
void Lca(LL x,LL y,LL c)
{
LL m1=,m2=;
if (dep[x]<dep[y]) swap(x,y);
for (LL i=op;i>=;i--) if (dep[x]-(<<i)>=dep[y])
{
if (sub[x][i]>m1){m2=m1;m1=sub[x][i];}
else if (sub[x][i]>m2&&sub[x][i]!=m1) m2=sub[x][i];
if (maxn[x][i]>m1){m2=m1;m1=maxn[x][i];}
else if (maxn[x][i]>m2&&maxn[x][i]!=m1) m2=maxn[x][i];
x=f[x][i];
}
if (x!=y)
{
for (LL i=op;i>=;i--) if (f[x][i]!=f[y][i])
{
if (sub[x][i]>m1){m2=m1;m1=sub[x][i];}
else if (sub[x][i]>m2&&sub[x][i]!=m1) m2=sub[x][i];
if (maxn[x][i]>m1){m2=m1;m1=maxn[x][i];}
else if (maxn[x][i]>m2&&maxn[x][i]!=m1) m2=maxn[x][i];
if (sub[y][i]>m1){m2=m1;m1=sub[y][i];}
else if (sub[y][i]>m2&&sub[y][i]!=m1) m2=sub[y][i];
if (maxn[y][i]>m1){m2=m1;m1=maxn[y][i];}
else if (maxn[y][i]>m2&&maxn[y][i]!=m1) m2=maxn[y][i];
x=f[x][i];
y=f[y][i];
}
if (sub[x][]>m1){m2=m1;m1=sub[x][];}
else if (sub[x][]>m2&&sub[x][]!=m1) m2=sub[x][];
if (maxn[x][]>m1){m2=m1;m1=maxn[x][];}
else if (maxn[x][]>m2&&maxn[x][]!=m1) m2=maxn[x][];
if (sub[y][]>m1){m2=m1;m1=sub[y][];}
else if (sub[y][]>m2&&sub[y][]!=m1) m2=sub[y][];
if (maxn[y][]>m1){m2=m1;m1=maxn[y][];}
else if (maxn[y][]>m2&&maxn[y][]!=m1) m2=maxn[y][];
}
if (m1==) return;
if (c==m1)
{
if (m2==) return;
d=min(d,c-m2);
}
else d=min(d,c-m1);
}