A Class of LCT Problems
A Brief Overview of LCT
LCT (Link Cut Tree) is a flexible data structure for maintaining tree paths. Unlike a segment tree, an LCT usually uses Splay, a very elegant and flexible data structure, to maintain the tree paths. Because an LCT supports adding and deleting edges dynamically, many problems can be solved by breaking through some of their restrictions.
The main operations of an LCT:
- Pushup, Pushdown operations: the basic data-structure operations
- Rotate operation: this is the Splay's rotation operation
- Splay operation: this is the Splay's splaying operation
- Access operation: opens up the path from point \(p\) to the root of the Splay
- MakeRoot operation: makes a given point the root of the tree
- Find operation: finds the root of the current Splay
- Link operation: adds an edge
- Cut operation: deletes an edge
- Split operation: extracts a path
The Classic LCT Model for Minimum Spanning Trees
The models behind many LCT problems basically all come from the way an LCT does minimum spanning trees. The LCT method for minimum spanning trees is very similar to Kruskal (although it has many unnecessary steps, they are part of the classic model), and I will introduce it here.
After initializing an LCT, we sort the edges from smallest to largest, and then we treat each edge as a point plus two edges, where the point weight is the edge weight:
Consider the case where we later find a cycle (when they are in the same connected component), and do a check: if the current LCT contains an edge larger than the current one (of course, after sorting this never happens), just Cut away the two edges of that point.
Minimum-Difference Spanning Tree
This actually also uses the idea of finding a minimum spanning tree with an LCT. After sorting the edges from smallest to largest, enumerate the upper bound on the edges, and do a Link operation when there is no cycle; if there is a cycle, delete the smallest edge in the current connected component, greedily keeping the difference as small as possible. When the whole tree is complete (when there are n - 1 edges), just update the answer.
// P4234.cpp
#include <bits/stdc++.h>
#define lson (ch[p][0])
#define rson (ch[p][1])
using namespace std;
const int MAX_N = 50000 + 5, MAX_E = 200005;
const long long INF = (1LL << 62);
struct edge
{
int src, dst, weight, id;
bool operator<(const edge &target) const { return weight < target.weight; }
} edges[MAX_E << 1];
int n, m, fa[MAX_E << 1], idx[MAX_E << 1], ch[MAX_E << 1][2];
bool tag[MAX_E << 1], book[MAX_E << 1];
int check(int x) { return ch[fa[x]][1] == x; }
bool isRoot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline void update(int p)
{
idx[p] = p;
if (idx[lson] > n && (idx[p] <= n || idx[p] > idx[lson]))
idx[p] = idx[lson];
if (idx[rson] > n && (idx[p] <= n || idx[p] > idx[rson]))
idx[p] = idx[rson];
}
inline void pushdown(int p)
{
if (tag[p])
{
if (lson)
tag[lson] ^= 1, swap(ch[lson][0], ch[lson][1]);
if (rson)
tag[rson] ^= 1, swap(ch[rson][0], ch[rson][1]);
tag[p] = 0;
}
}
inline void rotate(int x)
{
int y = fa[x], z = fa[y], dir = check(x), w = ch[x][dir ^ 1];
fa[x] = z;
if (!isRoot(y))
ch[z][check(y)] = x;
ch[y][dir] = w, fa[w] = y;
ch[x][dir ^ 1] = y, fa[y] = x;
update(y), update(x), update(z);
}
inline void jump(int x)
{
if (!isRoot(x))
jump(fa[x]);
pushdown(x);
}
inline void splay(int p)
{
jump(p);
for (int fat = fa[p]; fat = fa[p], !isRoot(p); rotate(p))
if (!isRoot(fat))
rotate(check(fat) == check(p) ? fat : p);
}
inline void access(int p)
{
for (int fat = 0; p != 0; fat = p, p = fa[p])
splay(p), ch[p][1] = fat, update(p);
}
inline void makeRoot(int p)
{
access(p), splay(p);
swap(lson, rson), tag[p] ^= 1;
}
inline int find(int p)
{
access(p), splay(p), pushdown(p);
while (lson)
pushdown(p = lson);
splay(p);
return p;
}
inline void link(int x, int y)
{
makeRoot(x);
fa[x] = y;
}
inline void split(int x, int y) { makeRoot(x), access(y), splay(y); }
inline bool judge(int x, int y)
{
makeRoot(x);
return (find(y) == x);
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1, x, y, w; i <= m; i++)
scanf("%d%d%d", &x, &y, &w), edges[i].src = x, edges[i].dst = y, edges[i].weight = w;
sort(edges + 1, edges + 1 + m);
for (int i = 1; i <= m; i++)
edges[i].id = n + i;
long long answer = INF, lbound = 1, block_num = 0;
for (int i = 1; i <= m; i++)
{
if (edges[i].src == edges[i].dst)
{
book[i] = true;
continue;
}
if (!judge(edges[i].src, edges[i].dst))
link(edges[i].src, edges[i].id), link(edges[i].id, edges[i].dst), block_num++;
else
{
split(edges[i].src, edges[i].dst);
int now = idx[edges[i].dst];
book[now - n] = true, splay(now);
fa[ch[now][0]] = fa[ch[now][1]] = 0;
link(edges[i].src, edges[i].id), link(edges[i].id, edges[i].dst);
}
while (book[lbound] && lbound <= i)
lbound++;
if (block_num >= n - 1)
answer = min(answer, 1LL * edges[i].weight - 1LL * edges[lbound].weight);
}
printf("%lld", answer);
return 0;
}
Example Problem: P2387 [NOI2014] Magic Forest
This problem is also solved with the idea of maintaining a minimum spanning tree with an LCT. Consider first sorting by \(a_i\), then fixing the upper bound \(a_i\) and finding the minimum spanning tree on \(b_i\).
// P2387.cpp
#include <bits/stdc++.h>
#define lson ch[p][0]
#define rson ch[p][1]
using namespace std;
const int MAX_N = 2e5 + 200;
struct edge
{
int src, dst, ai, bi;
bool operator<(const edge &edg) const { return ai < edg.ai || (ai == edg.ai && bi < edg.bi); }
} edges[MAX_N];
int n, m, ch[MAX_N][2], fa[MAX_N], idx[MAX_N], val[MAX_N], tag[MAX_N];
inline bool isRoot(int p) { return ch[fa[p]][0] != p && ch[fa[p]][1] != p; }
inline int check(int p) { return ch[fa[p]][1] == p; }
inline void pushup(int p)
{
idx[p] = p;
if (lson && val[idx[lson]] > val[idx[p]])
idx[p] = idx[lson];
if (rson && val[idx[rson]] > val[idx[p]])
idx[p] = idx[rson];
}
inline void pushdown(int p)
{
if (tag[p])
{
tag[p] = 0;
if (lson)
tag[lson] ^= 1, swap(ch[lson][0], ch[lson][1]);
if (rson)
tag[rson] ^= 1, swap(ch[rson][0], ch[rson][1]);
}
}
inline void rotate(int p)
{
int y = fa[p], z = fa[y], dir = check(p), w = ch[p][dir ^ 1];
fa[p] = z;
if (!isRoot(y))
ch[z][check(y)] = p;
ch[y][dir] = w, fa[w] = y;
ch[p][dir ^ 1] = y, fa[y] = p;
pushup(y), pushup(p), pushup(z);
}
inline void jump(int p)
{
if (!isRoot(p))
jump(fa[p]);
pushdown(p);
}
inline void splay(int p)
{
jump(p);
for (int fat = fa[p]; fat = fa[p], !isRoot(p); rotate(p))
if (!isRoot(fat))
rotate(check(fat) == check(p) ? fat : p);
}
inline void access(int p)
{
for (int fat = 0; p != 0; fat = p, p = fa[p])
splay(p), ch[p][1] = fat, pushup(p);
}
inline void makeRoot(int p)
{
access(p), splay(p);
swap(lson, rson), tag[p] ^= 1;
}
inline int find(int p)
{
access(p), splay(p);
while (lson)
p = lson;
splay(p);
return p;
}
inline void link(int x, int y)
{
makeRoot(x);
fa[x] = y;
}
inline void split(int x, int y)
{
makeRoot(x);
access(y), splay(y);
}
inline bool check(int x, int y)
{
makeRoot(x);
return find(y) == x;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
scanf("%d%d%d%d", &edges[i].src, &edges[i].dst, &edges[i].ai, &edges[i].bi);
int ans = 2e9;
sort(edges + 1, edges + 1 + m);
for (int i = 1; i <= m; i++)
{
int id = i + n;
val[id] = edges[i].bi;
if (edges[i].src == edges[i].dst)
continue;
if (!check(edges[i].src, edges[i].dst))
link(edges[i].src, id), link(id, edges[i].dst);
else
{
split(edges[i].src, edges[i].dst);
int now = idx[edges[i].dst], maxb = val[now];
if (maxb <= edges[i].bi)
continue;
splay(now), fa[ch[now][0]] = fa[ch[now][1]] = 0;
link(edges[i].src, id), link(id, edges[i].dst);
}
if (check(1, n))
split(1, n), ans = min(ans, edges[i].ai + val[idx[n]]);
}
if (ans < 2e9)
printf("%d", ans);
else
puts("-1");
return 0;
} // P2387.cpp