将边按权值从小到大排序
一开始想二分差值,枚举左端点确定右端点,仅用区间内的边判断构成生成树,这个可以用 LCT + 滑块在 时间内完成。
既然要用滑动窗口才能把复杂度降下来,直接考虑双指针,复杂度为 ,每次移动右指针的边加入到 LCT,然后不停的删左指针的边使得刚好形成一棵生成树。。。
稍微一想就会发现双指针的做法更加SB,右端点的边加入可能会形成环,此时必须要替换掉一条边,替换掉路径上边权最小的边是最优的。
综合一下,可以想到实际上只需要不断的移动右指针,将边加入到 LCT 中,如果形成环,则替换掉路径上最小的边,不需要双指针也不需要移动左指针来删边,当能构成生成树时就更新答案。
总结一下这个做法就是枚举最大值,如果形成环替换掉路径上的最小值,这是最优的选择。
最后要维护整颗辅助树的边权最大值和最小值,显然最大值就是当前枚举的边,最小值的下标单调递增,可以用一个指针在原数组中扫,我用了无论时间还是空间都更劣的multiset来维护整棵树的边权最大值和最小值,注意删除时如果 erase 传的值是一个数值而不是迭代器,会将 multiset 内所有此类数值全部删除,效果就等价于 set,因此还要很 SB 的调用 二分查找迭代器然后删除。
关于LCT维护边权就是转边为点老套路。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
typedef long long ll;
#define pii pair<int,int>
#define fir first
#define sec second
int n,m,ans;
struct node {
int u,v,w;
bool operator < (const node &rhs) const {
return w < rhs.w;
}
}E[maxn];
multiset<int> st;
inline int read(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
struct LCT { //用splay维护原森林的连通,用到了splay的操作以及数组
int ch[maxn][2]; //ch[u][0] 表示 左二子,ch[u][1] 表示右儿子
int f[maxn]; //当前节点的父节点
int tag[maxn]; //翻转标记,乘标记,加标记
int top,sta[maxn],sz[maxn];
int val[maxn],mi[maxn],len[maxn],sum[maxn];
inline bool get(int x) {
return ch[f[x]][1] == x;
}
void init() {
for (int i = 1; i <= n; i++)
sz[i] = 1, mi[i] = i, val[i] = 2147483647;
}
inline void pushup(int rt) {
if (rt) {
sz[rt] = 1; mi[rt] = rt; sum[rt] = len[rt];
int ls = ch[rt][0], rs = ch[rt][1];
if (ls) {
sz[rt] += sz[ls];
sum[rt] += sum[ls];
if (val[mi[ls]] < val[mi[rt]])
mi[rt] = mi[ls];
}
if (rs) {
sz[rt] += sz[rs];
sum[rt] += sum[rs];
if (val[mi[rs]] < val[mi[rt]])
mi[rt] = mi[rs];
}
}
}
inline void pushdown(int rt) {
if (tag[rt]) {
int ls = ch[rt][0], rs = ch[rt][1];
if (ls) swap(ch[ls][0],ch[ls][1]), tag[ls] ^= 1;
if (rs) swap(ch[rs][0],ch[rs][1]), tag[rs] ^= 1;
tag[rt] = 0;
}
}
inline bool isroot(int x) {
return (ch[f[x]][0] != x) && (ch[f[x]][1] != x);
}
inline void rotate(int x) { //旋转操作,根据 x 在 f[x] 的哪一侧进行左旋和右旋
int old = f[x], oldf = f[old];
int whichx = get(x);
if(!isroot(old)) ch[oldf][ch[oldf][1] == old] = x; //如果 old 不是根节点,就要修改 oldf 的子节点信息
ch[old][whichx] = ch[x][whichx ^ 1];
ch[x][whichx ^ 1] = old;
f[ch[old][whichx]] = old;
f[old] = x; f[x] = oldf;
pushup(old); pushup(x);
}
inline void splay(int x) { //将 x 旋到所在 splay 的根
top = 0; sta[++top] = x;
for (int i = x; !isroot(i); i = f[i]) sta[++top] = f[i]; //在 splay 中维护 下推标记
while(top) pushdown(sta[top--]);
for(int fa = f[x]; !isroot(x); rotate(x), fa = f[x]) { //再把x翻上来
if(!isroot(fa)) //如果fa非根,且x 和 fa是同一侧,那么先翻转fa,否则先翻转x
rotate((get(x) == get(fa)) ? fa : x);
}
}
inline void access(int x) { //access操作将x 到 根路径上的边修改为重边
int lst = 0;
while(x > 0) {
splay(x);
ch[x][1] = lst;
pushup(x);
lst = x; x = f[x];
}
}
inline void move_to_root(int x) { //将 x 移到 x 所在树的根(不是所在splay的根,所在splay只是一条重链)
access(x); splay(x); tag[x] ^= 1; swap(ch[x][0],ch[x][1]);
//将 x 移到 根之后 x 是深度最低的点,这条重链、这棵splay上所有点的深度颠倒,
//所有的点的左子树的点应该到右子树,因此要翻转这棵splay的左右子树
}
inline int findroot(int x) {
access(x);
splay(x);
int rt = x;
while(ch[rt][0]) rt = ch[rt][0];
return rt;
}
inline void split(int x,int y) {
move_to_root(x); access(y); splay(y);
}
inline void link(int x,int y) {
move_to_root(x); f[x] = y; splay(x);
}
inline void cut(int x,int y) {
split(x,y);
ch[y][0] = f[x] = 0;
pushup(y);
}
}tree;
int p[maxn];
int find(int x) {
return x == p[x] ? x : p[x] = find(p[x]);
}
int id,x,y,u,v,k[maxn],tot,vis[maxn];
char op[10];
int main() {
n = read(); m = read();
tree.init();
tot = n;
ans = 2147483647;
for (int i = 1; i <= m; i++) {
scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);
}
for (int i = 1; i <= n; i++) {
p[i] = i;
}
sort(E + 1,E + m + 1);
for (int i = 1; i <= m; i++) {
int u = E[i].u, v = E[i].v, w = E[i].w;
tree.sum[i + n] = tree.val[i + n] = w;
tree.mi[i + n] = i + n;
tree.sz[i + n] = 1;
if (u == v) continue;
if (tree.findroot(u) == tree.findroot(v)) {
tree.split(u,v);
int p = tree.mi[v];
tree.cut(E[p - n].u,p);
tree.cut(E[p - n].v,p);
auto it = st.lower_bound(E[p - n].w);
st.erase(it);
tree.link(u,i + n);
tree.link(v,i + n);
st.insert(E[i].w);
} else {
tree.link(u,i + n);
tree.link(v,i + n);
st.insert(E[i].w);
tot--;
}
if (tot == 1)
ans = min(ans,*st.rbegin() - *st.begin());
}
printf("%d\n",ans);
return 0;
}