#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
--x; --y;
g[x].push_back(y);
g[y].push_back(x);
}
vector<int> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
--c[i];
}
auto Bfs = [&](int v) {
vector<int> d(n, -1);
vector<int> que(1, v);
d[v] = 0;
for (int b = 0; b < (int) que.size(); b++) {
int i = que[b];
for (int j : g[i]) {
if (d[j] == -1) {
d[j] = d[i] + 1;
que.push_back(j);
}
}
}
return d;
};
int x, y;
{
vector<int> d = Bfs(0);
x = 0;
for (int i = 1; i < n; i++) {
if (d[i] > d[x]) {
x = i;
}
}
}
{
vector<int> d = Bfs(x);
y = 0;
for (int i = 1; i < n; i++) {
if (d[i] > d[y]) {
y = i;
}
}
}
vector<vector<int>> d(2);
d[0] = Bfs(x);
d[1] = Bfs(y);
vector<int> res(n);
for (int foo = 0; foo < 2; foo++) {
int root = (foo == 0 ? x : y);
vector<int> mx(n);
vector<int> pr(n);
vector<int> dep(n);
function<void(int, int)> Dfs = [&](int v, int pv) {
pr[v] = pv;
mx[v] = 1;
for (int u : g[v]) {
if (u == pv) {
continue;
}
dep[u] = dep[v] + 1;
Dfs(u, v);
mx[v] = max(mx[v], mx[u] + 1);
}
};
Dfs(root, root);
vector<int> f(n, 1);
auto Add = [&](int v, int d, int p) {
while (d--) {
f[v] += p;
v = pr[v];
}
};
vector<set<int>> cols(n);
function<void(int, int)> Solve = [&](int v, int pv) {
if (d[foo][v] >= d[foo ^ 1][v]) {
Add(v, mx[v], -1);
vector<bool> was(m);
res[v] = 0;
for (int u = pr[v]; ; u = pr[u]) {
if (f[u] == 1) {
res[v] = (int) cols[u].size();
// res[v] += !was[c[u]];
// was[c[u]] = true;
break;
}
if (u == root) {
break;
}
}
Add(v, mx[v], +1);
}
multiset<int> st;
for (int u : g[v]) {
if (u == pv) {
continue;
}
st.insert(mx[u]);
}
for (int u : g[v]) {
if (u == pv) {
continue;
}
st.erase(st.find(mx[u]));
if (!st.empty()) {
Add(pr[v], *prev(st.end()), -1);
}
cols[v].clear();
for (int x = pr[v]; ; x = pr[x]) {
if (f[x] == 1) {
cols[v] = cols[x];
break;
}
if (x == root) {
break;
}
}
cols[v].insert(c[v]);
Solve(u, v);
if (!st.empty()) {
Add(pr[v], *prev(st.end()), +1);
}
st.insert(mx[u]);
}
};
Solve(root, root);
}
for (int i = 0; i < n; i++) {
cout << res[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
860 KB |
Output is correct |
3 |
Correct |
25 ms |
16408 KB |
Output is correct |
4 |
Correct |
24 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
7 ms |
1540 KB |
Output is correct |
7 |
Correct |
6 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
3 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
1532 KB |
Output is correct |
14 |
Correct |
5 ms |
860 KB |
Output is correct |
15 |
Correct |
6 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
9 ms |
1116 KB |
Output is correct |
18 |
Correct |
7 ms |
1116 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
147 ms |
65844 KB |
Output is correct |
21 |
Correct |
38 ms |
16220 KB |
Output is correct |
22 |
Correct |
3 ms |
856 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1096 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
76 ms |
33592 KB |
Output is correct |
28 |
Correct |
54 ms |
23856 KB |
Output is correct |
29 |
Correct |
22 ms |
9308 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
30 ms |
14428 KB |
Output is correct |
32 |
Correct |
37 ms |
16220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
312 ms |
20892 KB |
Output is correct |
2 |
Execution timed out |
2057 ms |
32912 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
650 ms |
37500 KB |
Output is correct |
2 |
Runtime error |
268 ms |
274432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
860 KB |
Output is correct |
3 |
Correct |
25 ms |
16408 KB |
Output is correct |
4 |
Correct |
24 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
7 ms |
1540 KB |
Output is correct |
7 |
Correct |
6 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
3 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
1532 KB |
Output is correct |
14 |
Correct |
5 ms |
860 KB |
Output is correct |
15 |
Correct |
6 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
9 ms |
1116 KB |
Output is correct |
18 |
Correct |
7 ms |
1116 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
147 ms |
65844 KB |
Output is correct |
21 |
Correct |
38 ms |
16220 KB |
Output is correct |
22 |
Correct |
3 ms |
856 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1096 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
76 ms |
33592 KB |
Output is correct |
28 |
Correct |
54 ms |
23856 KB |
Output is correct |
29 |
Correct |
22 ms |
9308 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
30 ms |
14428 KB |
Output is correct |
32 |
Correct |
37 ms |
16220 KB |
Output is correct |
33 |
Correct |
312 ms |
20892 KB |
Output is correct |
34 |
Execution timed out |
2057 ms |
32912 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |