Link-Cut Tree(LCT) 模板总结 & 水题/模板题 动态树

来源:互联网 发布:怎么提高编程打字速度 编辑:程序博客网 时间:2024/04/30 18:08

伪代码解释LCT模板,下面题目有C++模板。

splay::push_up i: // 维护数据

    // 举个例子

    i.sum = i.left_child.sum + i.right_child.sum

splay::push_down i: // 下传翻转标记,make_root 需

    if i.reverse_lazy_tag:

        reverse i

        reverse i.left_child

        reverse i.right_child

        swap i.left_child i.right_child

splay::reverse i: // 标记翻转,make_root 需

    i.reverse_lazy_tag = not self

splay::left_rotate i: // 左旋

    z = i.right_child; i.right_child = z.left_child // 旋转操作

    if i = i.father.left_child: // 维护父亲节点

        i.father.left_child = z

    else if i = i.father.right_child

        i.father.right_child = z

    z.father = i.father

    i.father = z

    i.push_up // 更新数据

    z.push_up

splay::right_rotate i // 右旋, 同理

splay::splay x: // splay操作

    stack.clear

    for i = x; i != null; i = i.father: // 将splay[x]的祖先先清空标记

        stack.push i

    while not stack.empty:

        stack.pop.push_down

    while x is not root // 简单地通过旋转操作将x升到根,戏说spaly,比splay操作要慢0.5倍左右,可写splay

        if x = x.father.left_child:

            x.father.right_rotate

        else

            x.father.left_rotate

    x.push_up

link_cut_tree::access x: // 将根到x的路径设为preferred path,有点类似重链

    for t = null; x != null; t = x, x = x.father // 遍历根到x的路径,此时x始终是t的父亲

        splay splay[x]

        splay[x].right_child = t // 由于x是t的父亲,所以t的深度比x大1,即t在伸展树中在x的右子树中

        splay[x].push_up

link_cut_tree::make_root x: // 将x设为LCT的新根节点

    access x // 将原根到x的路径上的节点依次加入splay,将x设为根后,此路径将颠倒。

    splay x // 此时位于splay中x左子树的点都是x的祖先

    reverse x // 翻转以x为根的splay,此时x的左子树中的点都到x的右子树中,即表示x的祖先变为x的后代,对于原根~x路径的每个点都如此。

link_cut_tree::link x, y:

    make_root x // 如果要连接x,y,先将x设为根

    x.father = y // 此时很容易想到将x的父亲设为y,边就建立了

link_cut_tree::cut x, y 同理

1036: [ZJOI2008]树的统计Count

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 9352  Solved: 3783
[Submit][Status][Discuss]

Description

一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w。我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身

Input

输入的第一行为一个整数n,表示节点的个数。接下来n – 1行,每行2个整数a和b,表示节点a和节点b之间有一条边相连。接下来n行,每行一个整数,第i行的整数wi表示节点i的权值。接下来1行,为一个整数q,表示操作的总数。接下来q行,每行一个操作,以“CHANGE u t”或者“QMAX u v”或者“QSUM u v”的形式给出。 对于100%的数据,保证1<=n<=30000,0<=q<=200000;中途操作中保证每个节点的权值w在-30000到30000之间。

Output

对于每个“QMAX”或者“QSUM”的操作,每行输出一个整数表示要求输出的结果。

Sample Input

4

1 2

2 3

4 1

4 2 1 3

12

QMAX 3 4

QMAX 3 3

QMAX 3 2

QMAX 2 3

QSUM 3 4

QSUM 2 1

CHANGE 1 5

QMAX 3 4

CHANGE 3 6

QMAX 3 4

QMAX 2 4

QSUM 3 4

Sample Output

4

1

2

2

10

6

5

6

5

16

#include <cstdio>#include <cstring>#include <algorithm>#define N 200005int sz[N], l[N], r[N], fa[N], sum[N], val[N], max[N], rev[N], sk[N];void update(int x) {max[x] = std::max(std::max(max[l[x]], max[r[x]]), val[x]);sum[x] = sum[l[x]] + sum[r[x]] + val[x];}void pushdown(int x) {if (rev[x]) { rev[x] ^= 1, rev[l[x]] ^= 1, rev[r[x]] ^= 1, std::swap(l[x], r[x]); }}bool isRoot(int x) {return l[fa[x]] != x && r[fa[x]] != x;}void lrotate(int i) {int z = r[i]; r[i] = l[z]; fa[r[i]] = i; l[z] = i;if (i == l[fa[i]]) l[fa[i]] = z;else if (i == r[fa[i]]) r[fa[i]] = z;fa[z] = fa[i]; fa[i] = z;update(i); update(z);}void rrotate(int i) {int z = l[i]; l[i] = r[z]; fa[l[i]] = i; r[z] = i;if (i == l[fa[i]]) l[fa[i]] = z;else if (i == r[fa[i]]) r[fa[i]] = z;fa[z] = fa[i]; fa[i] = z;update(i); update(z);}void spaly(int x) {int top = 0;sk[++top] = x;for (int i = x; !isRoot(i); i = fa[i]) sk[++top] = fa[i];while (top) pushdown(sk[top--]);while (!isRoot(x))if (x == l[fa[x]]) rrotate(fa[x]);else lrotate(fa[x]);update(x);}void access(int x) { for(int t = 0; x; t = x, x = fa[x]) { spaly(x); r[x] = t; update(x); } }void makeroot(int x) { access(x); spaly(x); rev[x] ^= 1; }int query_sum(int x, int y) { makeroot(x); access(y); spaly(y); return sum[y]; }int query_max(int x, int y) { makeroot(x); access(y); spaly(y); return max[y]; }void modify(int x, int v) { access(x); val[x] = v; update(x); }int h[N], p[N], to[N], cnt = 0;void add(int a, int b) { p[cnt] = h[a], to[cnt] = b, h[a] = cnt++; }void dfs(int x) {for (int i = h[x]; i != -1; i = p[i])if (to[i] != fa[x])fa[to[i]] = x, dfs(to[i]);}int main() {int n, u, v, i, q;char ch[8];scanf("%d", &n);std::fill(h,h+n+2,-1);std::fill(rev,rev+n+2,0);max[0] = -2147483647;for (i = 1; i < n; i++) {scanf("%d%d", &u, &v);add(u, v); add(v, u);}dfs(1);for (i = 1; i <= n; i++) {scanf("%d", &u);val[i] = max[i] = sum[i] = u;}scanf("%d", &q);for(; q>0; q--) {scanf("%s%d%d", ch, &u, &v);if (ch[1] == 'M') printf("%d\n", query_max(u, v));else if (ch[1] == 'S') printf("%d\n", query_sum(u, v));else modify(u, v);}return 0;}

无聊写了个数据生成器。。很蛋疼的写法。。。
struct { int a, b, w; } e[2000000], k[30000];int ec = 0, p[2000000];int find(int i) { return i == p[i] ? i : p[i] = find(p[i]); }int main() {srand(time(0));int n = rand()%30000+1;printf("%d\n", n);begin:int tot = 0;for(int i = 1; i <= n; i++) {p[i] = i;int nei = n/2;while(nei--) {e[ec].a = i;e[ec].b = rand()%n+1;ec++;}}random_shuffle(e, e+ec); // 使选出的边随机化for(int i = 0; i < ec; i++) {int fa = find(e[i].a), fb = find(e[i].b);if(fa != fb) {k[++tot]=e[i];p[fa]=fb;}if(tot == n-1) break;}if(tot != n-1) goto begin; // 若图不连通,重新生成一次for (int i=1;i<n;i++) printf("%d %d\n", k[i].a, k[i].b);for(int i = 1; i <= n; i++) printf("%d ", rand()%30000);printf("\n");tot = rand()%200000+1;printf("%d\n", tot);while(tot--) {int x = random()%3;if(x==2) printf("QSUM %d %d\n", rand()%n+1, rand()%n+1);else if (x==1) printf("QMAX %d %d\n", rand()%n+1, rand()%n+1);else printf("CHANGE %d %d\n", rand()%n+1, rand()%30000);}return 0;}


3282: Tree

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 895  Solved: 375
[Submit][Status][Discuss]

Description

给定N个点以及每个点的权值,要你处理接下来的M个操作。操作有4种。操作从0到3编号。点从1到N编号。

0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。

1:后接两个整数(x,y),代表连接x到y,若x到Y已经联通则无需连接。

2:后接两个整数(x,y),代表删除边(x,y),保证边(x,y)存在。

3:后接两个整数(x,y),代表将点X上的权值变成Y。

 

Input

第1行两个整数,分别为N和M,代表点数和操作数。

第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。

第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。

 

Output

对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。

Sample Input

3 3

1

2

3

1 1 2

0 1 2

0 1 1

Sample Output

3

1

HINT

1<=N,M<=300000

#include <cstdio>#include <cstring>#include <algorithm>#define N 300005int l[N], r[N], fa[N], sum[N], val[N], rev[N]={0}, sk[N], n;void update(int x) { sum[x] = sum[l[x]] ^ sum[r[x]] ^ val[x]; }void pushdown(int x) { if (rev[x]) rev[x] ^= 1, rev[l[x]] ^= 1, rev[r[x]] ^= 1, std::swap(l[x], r[x]); }bool isRoot(int x) { return l[fa[x]]!=x&&r[fa[x]]!=x; }void lrotate(int i) {int z = r[i]; r[i] = l[z]; fa[r[i]] = i; l[z] = i;if (i == l[fa[i]]) l[fa[i]] = z; else if (i == r[fa[i]]) r[fa[i]] = z;fa[z] = fa[i]; fa[i] = z; update(i); update(z);}void rrotate(int i) {int z = l[i]; l[i] = r[z]; fa[l[i]] = i; r[z] = i;if (i == l[fa[i]]) l[fa[i]] = z; else if (i == r[fa[i]]) r[fa[i]] = z;fa[z] = fa[i]; fa[i] = z; update(i); update(z);}void splay(int x) {int top = 0; sk[++top] = x;for (int i = x; !isRoot(i); i = fa[i]) sk[++top] = fa[i];while (top) pushdown(sk[top--]);while (!isRoot(x)) if (x == l[fa[x]]) rrotate(fa[x]); else lrotate(fa[x]);update(x);}void access(int x) { for(int t = 0; x; t = x, x = fa[x]) splay(x), r[x] = t, update(x); }void makeroot(int x) { access(x); splay(x); rev[x] ^= 1; }int find(int x) { access(x); splay(x); while (l[x]) x = l[x]; return x; }void link(int x, int y) { makeroot(x); fa[x] = y; }void cut(int x, int y) { makeroot(x); access(y); splay(y); if(l[y]==x) l[y]=fa[x]=0; }int main() {int x, y, i, q, op;scanf("%d%d", &n, &q);for (i = 1; i <= n; i++) scanf("%d", &val[i]);for(; q>0; q--) {scanf("%d%d%d", &op, &x, &y);switch(op) {case 0: makeroot(x); access(y); splay(y); printf("%d\n", sum[y]); break;case 1: if (find(x) != find(y)) link(x, y); break;case 2: if (find(x) == find(y)) cut(x, y); break;case 3: access(x); splay(x); val[x] = y; update(x); break;}}return 0;}


2157: 旅游

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 548  Solved: 301
[Submit][Status][Discuss]

Description

Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。

Input

输入的第一行包含一个整数N,表示T 城中的景点个数。景点编号为 0...N − 1。接下来N − 1 行,每行三个整数u、v 和w,表示有一条u 到v,使 Ray 愉悦度增加w 的桥。桥的编号为1...N − 1。|w| <= 1000。输入的第N + 1 行包含一个整数M,表示Ray 的操作数目。接下来有M 行,每行描述了一个操作,操作有如下五种形式: C i w,表示Ray 对于经过第i 座桥的愉悦度变成了w。 N u v,表示Ray 对于经过景点u 到v 的路径上的每一座桥的愉悦度都变成原来的相反数。 SUM u v,表示询问从景点u 到v 所获得的总愉悦度。 MAX u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最大愉悦度。 MIN u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最小愉悦度。测试数据保证,任意时刻,Ray 对于经过每一座桥的愉悦度的绝对值小于等于1000。

Output

对于每一个询问(操作S、MAX 和MIN),输出答案。

神TM样例大数据。。。

测样例还得上fc。。。

#include <cstdio>#include <cstring>#include <algorithm>#define N 200005int sz[N], l[N], r[N], fa[N], sum[N], val[N], max[N], rev[N], sk[N], min[N], neg[N], ed[N], n;void update(int x) {max[x] = std::max(max[l[x]], max[r[x]]), min[x] = std::min(min[r[x]], min[l[x]]);if(x > n) max[x] = std::max(max[x], val[x]), min[x] = std::min(min[x], val[x]);sum[x] = sum[l[x]] + sum[r[x]] + val[x];}void reverse(int y) {sum[y] = -sum[y]; std::swap(max[y], min[y]);max[y] = -max[y]; min[y] = -min[y];neg[y] ^= 1; val[y] = -val[y];}void pushdown(int x) {if (neg[x]) {neg[x] = 0;if(l[x]) reverse(l[x]);if(r[x]) reverse(r[x]);}if (rev[x]) {rev[x] ^= 1, rev[l[x]] ^= 1, rev[r[x]] ^= 1, std::swap(l[x], r[x]);}}bool isRoot(int x) {return l[fa[x]]!=x&&r[fa[x]]!=x;}void lrotate(int i) {int z = r[i]; r[i] = l[z]; fa[r[i]] = i; l[z] = i;if (i == l[fa[i]]) l[fa[i]] = z;else if (i == r[fa[i]]) r[fa[i]] = z;fa[z] = fa[i]; fa[i] = z;update(i); update(z);}void rrotate(int i) {int z = l[i]; l[i] = r[z]; fa[l[i]] = i; r[z] = i;if (i == l[fa[i]]) l[fa[i]] = z;else if (i == r[fa[i]]) r[fa[i]] = z;fa[z] = fa[i]; fa[i] = z;update(i); update(z);}void splay(int x) {int top = 0;sk[++top] = x;for (int i = x; !isRoot(i); i = fa[i]) sk[++top] = fa[i];while (top) pushdown(sk[top--]);while (!isRoot(x))if (x == l[fa[x]]) rrotate(fa[x]);else lrotate(fa[x]);update(x);}void access(int x) {for(int t = 0; x; t = x, x = fa[x]) { splay(x); r[x] = t; update(x); }}void makeroot(int x) {access(x); splay(x); rev[x] ^= 1;}void get_path(int x, int y) {makeroot(x); access(y); splay(y);}void link(int x, int y) {makeroot(x); fa[x] = y;} int main() {int u, v, i, q;char ch[8];scanf("%d", &n);int id = n;std::fill(rev,rev+n+2,0);std::fill(neg,neg+n+2,0);max[0] = -2147483647;min[0] = 2147483647;sum[0] = 0;for (i = 1; i < n; i++) {scanf("%d%d%d", &u, &v, &q);u++, v++;ed[i] = ++id;link(u, id); link(v, id);val[id] = sum[id] = max[id] = min[id] = q;}scanf("%d", &q);for(; q>0; q--) {scanf("%s%d%d", ch, &u, &v);if (ch[0] == 'C') access(ed[u]), val[ed[u]] = v, update(ed[u]);else {u++, v++;if (ch[0] == 'N') get_path(u, v), reverse(v);else if (ch[1] == 'U') get_path(u, v), printf("%d\n", sum[v]);else if (ch[1] == 'A') get_path(u, v), printf("%d\n", max[v]);else if (ch[1] == 'I') get_path(u, v), printf("%d\n", min[v]);}}return 0;}

1 0