TOP > データ構造
Link-Cut木(Link-Cut-Tree)
説明
動的木のひとつ。こわい。
計算量
- クエリ $O(\log N)$
実装例
- LinkCutTree($f$, $t$, $M1$):= コンストラクタ。ここで $f$ は2つの区間の要素をマージする二項演算, $t$ は要素を反転する演算, $M1$ はモノイドの単位元である。
- LinkCutTree($f$, $g$, $h$, $t$, $M1$, $OM0$):= コンストラクタ。ここで $f$ は2つの区間の要素をマージする二項演算, $g$ は要素と作用素をマージする二項演算(第三引数は対応する区間の長さ), $h$ は作用素同士をマージする二項演算, $t$ は要素の向きを反転する演算, $M1$ はモノイドの単位元, $OM0$ は作用素の単位元である。
- make_node($idx$, $v$) : ID が $idx$, 値に $v$ を入れたノードを新しく生成する。
- expose($t$) : $t$ と根との間を Heavy-edge でつなげて, $t$ を Heavy-edge の Splay 木の根にする。戻り値は $t$ ではないので注意。
- link($child$, $parent$) : child の親を parent にする(もともと parent の連結成分に child がつながってたらこわれるので注意すること)。もともとの $child$ は木の根である必要がある(根ではない場合は事前に expose($child$) を呼び出すこと)。
- cut($child$) : child の親と child を切り離す。
- evert($t$) : $t$ をもともとの木の根にする。
- lca($u$, $v$) : $u$ と $v$ のLCAを返す。
- get_path($t$) : $t$ から根までのパスに出現する頂点を返す($O(n)$)。
- set_propagate($t$, $x$) : 根からノード $t$ までのパスに作用素 $x$ を適用する。
- get_kth($x$, $k$) := $x$ から根までのパスに出現する頂点を並べたときの $k$ 番目の頂点を返す(0-indexed, つまり $x$ が $0$ 番目)。
- get_root($x$):= $x$ の根を返す。
template< typename Monoid = int, typename OperatorMonoid = Monoid >
struct LinkCutTree {
using F = function< Monoid(Monoid, Monoid) >;
using G = function< Monoid(Monoid, OperatorMonoid, int) >;
using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;
using S = function< Monoid(Monoid) >;
struct Node {
Node *l, *r, *p;
int idx;
Monoid key, sum;
OperatorMonoid lazy;
bool rev;
int sz;
bool is_root() {
return !p || (p->l != this && p->r != this);
}
Node(int idx, const Monoid &key, const OperatorMonoid &om) :
idx(idx), key(key), sum(key), lazy(om), sz(1),
l(nullptr), r(nullptr), p(nullptr), rev(false) {}
};
const Monoid M1;
const OperatorMonoid OM0;
const F f;
const G g;
const H h;
const S s;
LinkCutTree() : LinkCutTree([](Monoid a, Monoid b) { return a + b; }, [](Monoid a) { return a; }, Monoid()) {}
LinkCutTree(const F &f, const S &s, const Monoid &M1) :
LinkCutTree(f, G(), H(), s, M1, OperatorMonoid()) {}
LinkCutTree(const F &f, const G &g, const H &h, const S &s,
const Monoid &M1, const OperatorMonoid &OM0) :
f(f), g(g), h(h), s(s), M1(M1), OM0(OM0) {}
Node *make_node(int idx, const Monoid &v = Monoid()) {
return new Node(idx, v, OM0);
}
void propagate(Node *t, const OperatorMonoid &x) {
t->lazy = h(t->lazy, x);
t->key = g(t->key, x, 1);
t->sum = g(t->sum, x, t->sz);
}
void toggle(Node *t) {
assert(t);
swap(t->l, t->r);
t->sum = s(t->sum);
t->rev ^= true;
}
void push(Node *t) {
if(t->lazy != OM0) {
if(t->l) propagate(t->l, t->lazy);
if(t->r) propagate(t->r, t->lazy);
t->lazy = OM0;
}
if(t->rev) {
if(t->l) toggle(t->l);
if(t->r) toggle(t->r);
t->rev = false;
}
}
void update(Node *t) {
t->sz = 1;
t->sum = t->key;
if(t->l) t->sz += t->l->sz, t->sum = f(t->l->sum, t->sum);
if(t->r) t->sz += t->r->sz, t->sum = f(t->sum, t->r->sum);
}
void rotr(Node *t) {
auto *x = t->p, *y = x->p;
if((x->l = t->r)) t->r->p = x;
t->r = x, x->p = t;
update(x), update(t);
if((t->p = y)) {
if(y->l == x) y->l = t;
if(y->r == x) y->r = t;
update(y);
}
}
void rotl(Node *t) {
auto *x = t->p, *y = x->p;
if((x->r = t->l)) t->l->p = x;
t->l = x, x->p = t;
update(x), update(t);
if((t->p = y)) {
if(y->l == x) y->l = t;
if(y->r == x) y->r = t;
update(y);
}
}
void splay(Node *t) {
push(t);
while(!t->is_root()) {
auto *q = t->p;
if(q->is_root()) {
push(q), push(t);
if(q->l == t) rotr(t);
else rotl(t);
} else {
auto *r = q->p;
push(r), push(q), push(t);
if(r->l == q) {
if(q->l == t) rotr(q), rotr(t);
else rotl(t), rotr(t);
} else {
if(q->r == t) rotl(q), rotl(t);
else rotr(t), rotl(t);
}
}
}
}
Node *expose(Node *t) {
Node *rp = nullptr;
for(Node *cur = t; cur; cur = cur->p) {
splay(cur);
cur->r = rp;
update(cur);
rp = cur;
}
splay(t);
return rp;
}
void link(Node *child, Node *parent) {
expose(child);
expose(parent);
child->p = parent;
parent->r = child;
update(parent);
}
void cut(Node *child) {
expose(child);
auto *parent = child->l;
child->l = nullptr;
parent->p = nullptr;
update(child);
}
void evert(Node *t) {
expose(t);
toggle(t);
push(t);
}
Node *lca(Node *u, Node *v) {
if(get_root(u) != get_root(v)) return nullptr;
expose(u);
return expose(v);
}
vector< int > get_path(Node *x) {
vector< int > vs;
function< void(Node *) > dfs = [&](Node *cur) {
if(!cur) return;
push(cur);
dfs(cur->r);
vs.push_back(cur->idx);
dfs(cur->l);
};
expose(x);
dfs(x);
return vs;
}
void set_propagate(Node *t, const OperatorMonoid &x) {
expose(t);
propagate(t, x);
push(t);
}
Node *get_kth(Node *x, int k) {
expose(x);
while(x) {
push(x);
if(x->r && x->r->sz > k) {
x = x->r;
} else {
if(x->r) k -= x->r->sz;
if(k == 0) return x;
k -= 1;
x = x->l;
}
}
return nullptr;
}
Node *get_root(Node *x) {
expose(x);
while(x->l) {
push(x);
x = x->l;
}
return x;
}
};
検証
int main() {
int N, Q;
cin >> N >> Q;
struct submax {
int64 lsum, rsum, ans, all;
} e{-infll, -infll, -infll, 0};
auto f = [](const submax &a, const submax &b) {
submax c;
c.ans = max({a.ans, b.ans, a.rsum + b.lsum});
c.all = a.all + b.all;
c.lsum = max(a.lsum, a.all + b.lsum);
c.rsum = max(b.rsum, b.all + a.rsum);
return c;
};
auto g = [](submax a, int64 b, int p) {
a.ans = max(b, b * p);
a.all = b * p;
a.lsum = a.ans;
a.rsum = a.ans;
return a;
};
auto h = [](int64 a, int64 b) {
return b;
};
auto s = [](submax a) {
swap(a.lsum, a.rsum);
return a;
};
using LCT = LinkCutTree< submax, int64 >;
LCT lct(f, g, h, s, e, infll);
vector< LCT::Node * > nodes;
for(int i = 0; i < N; i++) {
int x;
cin >> x;
nodes.emplace_back(lct.make_node(i, g(e, x, 1)));
}
for(int i = 1; i < N; i++) {
int x, y;
cin >> x >> y;
--x, --y;
lct.evert(nodes[y]); //忘れずに
lct.link(nodes[y], nodes[x]);
}
while(Q--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--b, --c;
lct.evert(nodes[b]);
lct.expose(nodes[c]);
if(a == 1) {
lct.set_propagate(nodes[c], d);
} else {
cout << nodes[c]->sum.ans << "\n";
}
}
}
JOI春合宿2015 G - 道路整備 辺属性のクエリを処理したいときは、辺にも頂点を生やしてあげると実装が楽になる。
struct UnionFind {
vector< int > data;
UnionFind(int sz) {
data.assign(sz, -1);
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return (false);
if(data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if(data[k] < 0) return (k);
return (data[k] = find(data[k]));
}
int size(int k) {
return (-data[find(k)]);
}
};
int main() {
int N, Q;
cin >> N >> Q;
auto f = [](int a, int b) { return a + b; };
auto g = [](int a, int b, int l) { return 0; };
auto h = [](int a, int b) { return (a | b); };
auto s = [](int a) { return a; };
using LCT=LinkCutTree< int >;
LCT lct(f, g, h, s, 0, 0);
vector< LCT::Node * > nodev, nodee;
for(int i = 0; i < N; i++) {
nodev.emplace_back(lct.make_node(i, 0));
nodee.emplace_back(lct.make_node(i, 1));
}
UnionFind uf(N);
int cnt = 0;
for(int i = 0; i < Q; i++) {
int T, A, B;
cin >> T >> A >> B;
--A, --B;
if(T == 1) {
if(uf.find(A) != uf.find(B)) {
lct.evert(nodev[B]);
lct.link(nodee[cnt], nodev[A]);
lct.link(nodev[B], nodee[cnt]);
uf.unite(A, B);
++cnt;
} else {
lct.evert(nodev[A]);
lct.expose(nodev[B]);
lct.set_propagate(nodev[B], 1);
}
} else {
if(uf.find(A) != uf.find(B)) {
cout << -1 << "\n";
} else {
lct.evert(nodev[A]);
lct.expose(nodev[B]);
cout << nodev[B]->sum << "\n";
}
}
}
}