-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1139.cpp
More file actions
41 lines (38 loc) · 747 Bytes
/
1139.cpp
File metadata and controls
41 lines (38 loc) · 747 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, c[N], ans[N];
vector<int> g[N];
set<int> f[N];
void dfs(int u, int p) {
f[u].emplace(c[u]);
for (int v: g[u]) {
if (v != p) {
dfs(v, u);
if (f[u].size() < f[v].size()) {
f[u].swap(f[v]);
}
for (int x: f[v]) {
f[u].emplace(x);
}
}
}
ans[u] = f[u].size();
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
}
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1, -1);
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " \n"[i == n];
}
return 0;
}