Codeforces 600E - Lomsat gelral 「$Dsu \ on \ tree$模板」

With $Dsu \ on \ tree$ we can answer queries of this type:

How many vertices in the subtree of vertex $v$ has some property in $O (n \log n)$ time (for all of the queries)?

这题写的是轻重儿子(重链剖分)版本的 $Dsu \ on \ tree$

具体流程如下:

每次先递归计算轻儿子,再单独递归重儿子,计算完后轻儿子的一些信息需要删掉,但是重儿子的信息无需删除,如此出解,相当于是优化了暴力的多余部分

每个节点会作为轻儿子被计算,重链剖分上垂直有 $\log n$ 条链,故复杂度 $O (n \log n)$

代码

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4
  5 using namespace std;
  6
  7 typedef long long LL;
  8
  9 const int MAXN = 1e05 + 10;
 10 const int MAXM = 1e05 + 10;
 11 const int MAXC = 1e05 + 10;
 12
 13 struct LinkedForwardStar {
 14     int to;
 15
 16     int next;
 17 } ;
 18
 19 LinkedForwardStar Link[MAXM << 1];
 20 int Head[MAXN]= {0};
 21 int size = 0;
 22
 23 void Insert (int u, int v) {
 24     Link[++ size].to = v;
 25     Link[size].next = Head[u];
 26
 27     Head[u] = size;
 28 }
 29
 30 int N;
 31 int colour[MAXN];
 32
 33 int son[MAXN]= {0};
 34 int subsize[MAXN]= {0};
 35 void DFS (int root, int father) {
 36     son[root] = - 1;
 37     subsize[root] = 1;
 38     for (int i = Head[root]; i; i = Link[i].next) {
 39         int v = Link[i].to;
 40         if (v == father)
 41             continue;
 42         DFS (v, root);
 43         subsize[root] += subsize[v];
 44         if (son[root] == - 1 || subsize[v] > subsize[son[root]])
 45             son[root] = v;
 46     }
 47 }
 48 int vis[MAXN]= {0};
 49 int total[MAXC]= {0};
 50 int maxv = 0;
 51 LL sum = 0;
 52 void calc (int root, int father, int delta) { // 统计答案
 53     total[colour[root]] += delta;
 54     if (delta > 0 && total[colour[root]] >= maxv) {
 55         if (total[colour[root]] > maxv)
 56             sum = 0, maxv = total[colour[root]];
 57         sum += colour[root];
 58     }
 59     for (int i = Head[root]; i; i = Link[i].next) {
 60         int v = Link[i].to;
 61         if (v == father || vis[v])
 62             continue;
 63         calc (v, root, delta);
 64     }
 65 }
 66 LL answer[MAXN]= {0};
 67 void Solve (int root, int father, int type) { // type表示是不是重儿子信息
 68     for (int i = Head[root]; i; i = Link[i].next) {
 69         int v = Link[i].to;
 70         if (v == father || v == son[root])
 71             continue;
 72         Solve (v, root, 0);
 73     }
 74     if (~ son[root])
 75         Solve (son[root], root, 1), vis[son[root]] = 1;
 76     calc (root, father, 1);
 77     answer[root] = sum;
 78     if (~ son[root])
 79         vis[son[root]] = 0;
 80     if (! type) // 如果是轻儿子信息就需删除
 81         calc (root, father, - 1), maxv = sum = 0;
 82 }
 83
 84 int getnum () {
 85     int num = 0;
 86     char ch = getchar ();
 87
 88     while (! isdigit (ch))
 89         ch = getchar ();
 90     while (isdigit (ch))
 91         num = (num << 3) + (num << 1) + ch - ‘0‘, ch = getchar ();
 92
 93     return num;
 94 }
 95
 96 int main () {
 97     N = getnum ();
 98     for (int i = 1; i <= N; i ++)
 99         colour[i] = getnum ();
100     for (int i = 1; i < N; i ++) {
101         int u = getnum (), v = getnum ();
102         Insert (u, v), Insert (v, u);
103     }
104     DFS (1, 0), Solve (1, 0, 0);
105     for (int i = 1; i <= N; i ++) {
106         if (i > 1)
107             putchar (‘ ‘);
108         printf ("%lld", answer[i]);
109     }
110     puts ("");
111
112     return 0;
113 }
114
115 /*
116 4
117 1 2 3 4
118 1 2
119 2 3
120 2 4
121 */
122
123 /*
124 15
125 1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
126 1 2
127 1 3
128 1 4
129 1 14
130 1 15
131 2 5
132 2 6
133 2 7
134 3 8
135 3 9
136 3 10
137 4 11
138 4 12
139 4 13
140 */

原文地址:https://www.cnblogs.com/Colythme/p/10248071.html

时间: 2024-08-02 17:37:28

Codeforces 600E - Lomsat gelral 「$Dsu \ on \ tree$模板」的相关文章

Codeforces 600E Lomsat gelral (Dsu On the Tree)

题目链接 Lomsat gelral 占坑--等深入理解了再来补题解-- 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i, a, b) for (int i(a); i <= (b); ++i) 6 7 typedef long long LL; 8 9 const int N = 600010; 10 11 int n; 12 int cc[N], col[N], sz[N], son[N];

Codeforces.600E.Lomsat gelral(dsu on tree)

题目链接 dsu on tree参见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用sum[i]表示出现次数为i的颜色的和,cnt[i]表示出现次数为i的颜色有多少个(其实有个Max表示当前最多的次数,和tm[i]就好了),然后就这样了.. 再写一遍dsu on tree大体过程:(设当前点为x) 计算轻儿子子树的答案,并删掉轻儿子的贡献(大多数时候): 计算重儿子子树的答案,保留重儿

Codeforces 600E. Lomsat gelral(Dsu on tree学习)

题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这个子树中出现得比它多.求占领每个子树的所有颜色之和. 我们都知道可以$BST$启发式合并从而完美${O(nlogn^{2})}$,这太丑陋了. 那么$Dsu~~on~~tree$是在干啥呢? 找出树中每一个节点的重儿子,统计答案的时候优先进入每一个点的所有轻儿子,之后再进入重儿子,目的是保留重儿子所

CodeForces 600E Lomsat gelral(线段树合并)

题目链接:http://codeforces.com/problemset/problem/600/E You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the

CodeForces 600E. Lomsat gelral【树上启发式合并】

传送门 好像大家都是拿这道题作为树上启发式合并的板子题. 树上启发式合并,英文是 dsu on tree,感觉还是中文的说法更准确,因为这个算法和并查集(dsu)没有任何关系.一般用来求解有根树的所有子树的统计问题. 根据轻重儿子的各种性质,可以证明这个算法的时间复杂度为 \(O(nlogn)\),虽然看起来暴力的不行,但是却是一个很高效的算法. 算法的核心其实就是对于每个节点,先计算轻儿子,再计算重儿子,把自己和轻儿子的所有贡献累计到重儿子上去,如果自己是轻儿子,就把贡献清空. 如果掌握了树链

codeforces 600E . Lomsat gelral (线段树合并)

You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it'

CF 600E. Lomsat gelral(dsu on tree)

解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<set> using namespace std; const int

600E - Lomsat gelral(找子树多颜色问题)(入门)

题:https://codeforces.com/problemset/problem/600/E 题意:一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和,对于每个结点都输出答案. 分析:考虑暴力算法,对于每个节点只是清空计数数组,再对其子树颜色进行统计,复杂度o(n^2); 接着我们发现最后一个子树的清空是必要的,所以我们把重儿子当作这个子树,就可以让复杂度降为o(nlogn)级别: #include<bits/stdc++.h> using n

CF 600 E. Lomsat gelral

E. Lomsat gelral http://codeforces.com/contest/600/problem/E 题意: 求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号求和). 分析: dsu on tree. 这个可以解决一系列不带修改的子树查询问题. 考虑暴力的思路:就是枚举每个子树,计算每个颜色出现的个数.统计答案. dsu on tree:最后一个子树枚举计算完了,它的贡献可以保留,然后用其它的子树去合并.(最后一棵子树是最大的).现在的复杂度就是nlog