CF600E-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’s possible that two or more colours will be dominating in the subtree of some vertex.
The subtree of vertex $ v $ is the vertex $ v $ and all other vertices that contains vertex $ v $ in each path to the root.
For each vertex $ v $ find the sum of all dominating colours in the subtree of vertex $ v $ .
输入格式:
The first line contains integer $ n $ ( $ 1<=n<=10^{5} $ ) — the number of vertices in the tree.
The second line contains $ n $ integers $ c_{i} $ ( $ 1<=c_{i}<=n $ ), $ c_{i} $ — the colour of the $ i $ -th vertex.
Each of the next $ n-1 $ lines contains two integers $ x_{j},y_{j} $ ( $ 1<=x_{j},y_{j}<=n $ ) — the edge of the tree. The first vertex is the root of the tree.
输出格式:
Print $ n $ integers — the sums of dominating colours for each vertex.
样例:
样例输入1:
1
2
3
4
5
| 4
1 2 3 4
1 2
2 3
2 4
|
样例输出1:
样例输入2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| 15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
|
样例输出2:
1
| 6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
|
思路:
一道dsu on tree的简单例题
用一个通桶统计一遍答案即可
实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
| // Problem: CF600E Lomsat gelral
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF600E
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// Author: Ybw051114
//
// Powered by CP Editor (https://cpeditor.org)
#include "ybwhead/ios.h"
#define int long long
const int maxn = 1e5 + 10;
struct edge
{
int v, nxt;
} e[maxn << 1];
int head[maxn], tot, ans[maxn], siz[maxn], c[maxn], wson[maxn];
int a[maxn], n;
int mx, sum;
void __ADD(int u, int v)
{
e[++tot] = {v, head[u]};
head[u] = tot;
}
void add(int u, int v)
{
__ADD(u, v), __ADD(v, u);
}
void dfs(int u, int fa)
{
siz[u] = 1;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].v;
if (v == fa)
continue;
dfs(v, u);
siz[u] += siz[v];
if (siz[wson[u]] < siz[v])
wson[u] = v;
}
}
void add(int u, int fa, int rt)
{
c[a[u]]++;
if (c[a[u]] == mx)
sum += a[u];
if (c[a[u]] > mx)
sum = a[u], mx = c[a[u]];
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].v;
if (v == fa)
continue;
add(v, u, rt);
}
}
void del(int u, int fa)
{
c[a[u]]--;
mx = sum = 0;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].v;
if (v == fa)
continue;
del(v, u);
}
}
void solve(int u, int fa)
{
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].v;
if (v == fa || v == wson[u])
continue;
solve(v, u);
del(v, u);
}
if (wson[u])
solve(wson[u], u);
// cerr << mx << " " << sum << endl;
c[a[u]]++;
if (c[a[u]] == mx)
sum += a[u];
if (c[a[u]] > mx)
sum = a[u], mx = c[a[u]];
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].v;
if (v == fa || v == wson[u])
continue;
add(v, u, u);
}
ans[u] = sum;
}
signed main()
{
yin >> n;
for (int i = 1; i <= n; ++i)
{
yin >> a[i];
}
for (int i = 1; i < n; i++)
{
int x, y;
yin >> x >> y;
add(x, y);
}
dfs(1, 0);
solve(1, 0);
for (int i = 1; i <= n; i++)
yout << ans[i] << " ";
return 0;
}
|