题目链接
https://www.luogu.org/problem/P4180
分析
根据Kruskal算法的思想,(非)严格次小生成树应该是来自最小生成树的;
具体来说,是将某条非树边加入到MST上,再删去产生的环里原树边中边权最大的一条;
枚举加入的非树边,再用倍增算法求要删去的树边;
但要求严格次小生成树,所以删去的树边不可以和插入的边权值相等,
这样维护树上路径最大值的同时还要维护次大值。
AC代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
int num = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9')
num = num * 10 + c - '0', c = getchar();
return num;
}
const int maxn = 1e5 + 5, maxm = 3e5 + 5;
int head[maxn], eid;
struct Edge {
int u, v, w, flag, next;
bool operator < (const Edge& rhs) const {
return w < rhs.w;
}
} edge0[maxm], edge[2 * maxn];
inline void insert(int u, int v, int w) {
edge[++eid].v = v;
edge[eid].w = w;
edge[eid].next = head[u];
head[u] = eid;
}
int fa[maxn], depth[maxn], f[maxn]