-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy path1139.cc
47 lines (42 loc) · 812 Bytes
/
1139.cc
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
// https://cses.fi/problemset/task/1139
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef unordered_set<int> si;
vvi g;
vi c, d;
si s;
void dfs(int u, int p) {
si t;
t.insert(c[u]);
for (int z:g[u]) {
if (z == p) continue;
dfs(z, u);
if (s.size() > t.size()) swap(s, t);
for (int x:s) t.insert(x);
}
d[u] = t.size();
swap(s, t);
}
int main() {
int n, u, v;
cin >> n;
g = vvi(n);
c = vi(n); d = vi(n);
for (int i = 0; i < n; i++) cin >> c[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
u--; v--;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0, -1);
for (int i = 0; i < n; i++) {
cout << d[i];
if (i < n - 1) cout << " ";
}
cout << endl;
}