#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5, LG = 18, MAX = 3600005;
int n, k, m;
int lst[N], c[N], dep[N], pr[LG][N], id[LG][N], low[MAX], num[MAX], cnt[MAX], lab[MAX];
vector<int> tree[N], g[MAX];
void dfs(int u) {
for (int v : tree[u]) {
if (v != pr[0][u]) {
pr[0][v] = u;
id[0][v] = c[u];
for (int i = 1; i < LG; ++i) {
id[i][v] = ++m;
pr[i][v] = pr[i - 1][pr[i - 1][v]];
g[m].push_back(id[i - 1][v]);
g[m].push_back(id[i - 1][pr[i - 1][v]]);
}
dep[v] = dep[u] + 1;
dfs(v);
}
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) {
swap(u, v);
}
for (int j = LG - 1; ~j; --j) {
if (dep[u] - (1 << j) >= dep[v]) {
u = pr[j][u];
}
}
if (u == v) {
return u;
}
for (int j = LG - 1; ~j; --j) {
if (pr[j][u] != pr[j][v]) {
u = pr[j][u], v = pr[j][v];
}
}
return pr[0][u];
}
void add(int u, int p) {
int col = c[u];
for (int i = LG - 1; ~i; --i) {
if (dep[u] - (1 << i) >= dep[p]) {
g[col].push_back(id[i][u]);
u = pr[i][u];
}
}
}
int top, timer, scc;
int st[MAX];
void DFS(int u) {
st[++top] = u;
low[u] = num[u] = ++timer;
for (int v : g[u]) {
if (!num[v]) {
DFS(v);
low[u] = min(low[u], low[v]);
} else {
low[u] = min(low[u], num[v]);
}
}
if (low[u] == num[u]) {
++scc;
while (1) {
int v = st[top--];
num[v] = MAX;
lab[v] = scc;
if (v == u) {
break;
}
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> k; m = k;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
cin >> c[i];
}
dfs(1);
for (int i = 1; i <= n; ++i) {
if (lst[c[i]]) {
int x = lca(i, lst[c[i]]);
add(i, x);
add(lst[c[i]], x);
}
lst[c[i]] = i;
}
for (int i = 1; i <= m; ++i) {
if (!num[i]) {
DFS(i);
}
}
for (int i = 1; i <= k; ++i) {
++cnt[lab[i]];
}
vector<int> ver(m);
iota(ver.begin(), ver.end(), 1);
sort(ver.begin(), ver.end(), [&](int i, int j) {
return lab[i] < lab[j];
});
int res = MAX;
for (int i = 1, j = 0; i <= scc; ++i) {
bool out = 0;
while (j < m && lab[ver[j]] == i) {
for (int k : g[ver[j]]) {
if (lab[k] != i && cnt[lab[k]]) {
out = 1;
}
}
j++;
}
if (out) {
cnt[i] = 1;
} else if (cnt[i]) {
res = min(res, cnt[i] - 1);
}
}
cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
89684 KB |
Output is correct |
2 |
Correct |
42 ms |
89944 KB |
Output is correct |
3 |
Correct |
38 ms |
89948 KB |
Output is correct |
4 |
Correct |
36 ms |
89944 KB |
Output is correct |
5 |
Correct |
38 ms |
89940 KB |
Output is correct |
6 |
Correct |
38 ms |
89940 KB |
Output is correct |
7 |
Correct |
35 ms |
89940 KB |
Output is correct |
8 |
Correct |
38 ms |
89940 KB |
Output is correct |
9 |
Correct |
39 ms |
90200 KB |
Output is correct |
10 |
Correct |
46 ms |
89684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
89684 KB |
Output is correct |
2 |
Correct |
42 ms |
89944 KB |
Output is correct |
3 |
Correct |
38 ms |
89948 KB |
Output is correct |
4 |
Correct |
36 ms |
89944 KB |
Output is correct |
5 |
Correct |
38 ms |
89940 KB |
Output is correct |
6 |
Correct |
38 ms |
89940 KB |
Output is correct |
7 |
Correct |
35 ms |
89940 KB |
Output is correct |
8 |
Correct |
38 ms |
89940 KB |
Output is correct |
9 |
Correct |
39 ms |
90200 KB |
Output is correct |
10 |
Correct |
46 ms |
89684 KB |
Output is correct |
11 |
Correct |
42 ms |
92060 KB |
Output is correct |
12 |
Correct |
41 ms |
91984 KB |
Output is correct |
13 |
Correct |
43 ms |
91988 KB |
Output is correct |
14 |
Correct |
45 ms |
91984 KB |
Output is correct |
15 |
Correct |
42 ms |
91988 KB |
Output is correct |
16 |
Correct |
42 ms |
91988 KB |
Output is correct |
17 |
Correct |
43 ms |
91988 KB |
Output is correct |
18 |
Correct |
41 ms |
92252 KB |
Output is correct |
19 |
Correct |
41 ms |
92104 KB |
Output is correct |
20 |
Correct |
42 ms |
92244 KB |
Output is correct |
21 |
Correct |
42 ms |
92240 KB |
Output is correct |
22 |
Correct |
43 ms |
92240 KB |
Output is correct |
23 |
Correct |
42 ms |
92500 KB |
Output is correct |
24 |
Correct |
43 ms |
91992 KB |
Output is correct |
25 |
Correct |
42 ms |
92200 KB |
Output is correct |
26 |
Correct |
41 ms |
92244 KB |
Output is correct |
27 |
Correct |
42 ms |
92260 KB |
Output is correct |
28 |
Correct |
43 ms |
92244 KB |
Output is correct |
29 |
Correct |
42 ms |
91988 KB |
Output is correct |
30 |
Correct |
41 ms |
91988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1106 ms |
380100 KB |
Output is correct |
2 |
Correct |
862 ms |
336468 KB |
Output is correct |
3 |
Correct |
1127 ms |
342808 KB |
Output is correct |
4 |
Correct |
847 ms |
336496 KB |
Output is correct |
5 |
Correct |
1084 ms |
374844 KB |
Output is correct |
6 |
Correct |
840 ms |
336208 KB |
Output is correct |
7 |
Correct |
1133 ms |
361776 KB |
Output is correct |
8 |
Correct |
908 ms |
334708 KB |
Output is correct |
9 |
Correct |
1031 ms |
322120 KB |
Output is correct |
10 |
Correct |
1042 ms |
318280 KB |
Output is correct |
11 |
Correct |
1018 ms |
322804 KB |
Output is correct |
12 |
Correct |
988 ms |
326764 KB |
Output is correct |
13 |
Correct |
992 ms |
317500 KB |
Output is correct |
14 |
Correct |
1045 ms |
326856 KB |
Output is correct |
15 |
Correct |
1039 ms |
326820 KB |
Output is correct |
16 |
Correct |
1039 ms |
318912 KB |
Output is correct |
17 |
Correct |
1161 ms |
319920 KB |
Output is correct |
18 |
Correct |
1221 ms |
319700 KB |
Output is correct |
19 |
Correct |
1157 ms |
325316 KB |
Output is correct |
20 |
Correct |
1126 ms |
327896 KB |
Output is correct |
21 |
Correct |
38 ms |
89680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
89684 KB |
Output is correct |
2 |
Correct |
42 ms |
89944 KB |
Output is correct |
3 |
Correct |
38 ms |
89948 KB |
Output is correct |
4 |
Correct |
36 ms |
89944 KB |
Output is correct |
5 |
Correct |
38 ms |
89940 KB |
Output is correct |
6 |
Correct |
38 ms |
89940 KB |
Output is correct |
7 |
Correct |
35 ms |
89940 KB |
Output is correct |
8 |
Correct |
38 ms |
89940 KB |
Output is correct |
9 |
Correct |
39 ms |
90200 KB |
Output is correct |
10 |
Correct |
46 ms |
89684 KB |
Output is correct |
11 |
Correct |
42 ms |
92060 KB |
Output is correct |
12 |
Correct |
41 ms |
91984 KB |
Output is correct |
13 |
Correct |
43 ms |
91988 KB |
Output is correct |
14 |
Correct |
45 ms |
91984 KB |
Output is correct |
15 |
Correct |
42 ms |
91988 KB |
Output is correct |
16 |
Correct |
42 ms |
91988 KB |
Output is correct |
17 |
Correct |
43 ms |
91988 KB |
Output is correct |
18 |
Correct |
41 ms |
92252 KB |
Output is correct |
19 |
Correct |
41 ms |
92104 KB |
Output is correct |
20 |
Correct |
42 ms |
92244 KB |
Output is correct |
21 |
Correct |
42 ms |
92240 KB |
Output is correct |
22 |
Correct |
43 ms |
92240 KB |
Output is correct |
23 |
Correct |
42 ms |
92500 KB |
Output is correct |
24 |
Correct |
43 ms |
91992 KB |
Output is correct |
25 |
Correct |
42 ms |
92200 KB |
Output is correct |
26 |
Correct |
41 ms |
92244 KB |
Output is correct |
27 |
Correct |
42 ms |
92260 KB |
Output is correct |
28 |
Correct |
43 ms |
92244 KB |
Output is correct |
29 |
Correct |
42 ms |
91988 KB |
Output is correct |
30 |
Correct |
41 ms |
91988 KB |
Output is correct |
31 |
Correct |
1106 ms |
380100 KB |
Output is correct |
32 |
Correct |
862 ms |
336468 KB |
Output is correct |
33 |
Correct |
1127 ms |
342808 KB |
Output is correct |
34 |
Correct |
847 ms |
336496 KB |
Output is correct |
35 |
Correct |
1084 ms |
374844 KB |
Output is correct |
36 |
Correct |
840 ms |
336208 KB |
Output is correct |
37 |
Correct |
1133 ms |
361776 KB |
Output is correct |
38 |
Correct |
908 ms |
334708 KB |
Output is correct |
39 |
Correct |
1031 ms |
322120 KB |
Output is correct |
40 |
Correct |
1042 ms |
318280 KB |
Output is correct |
41 |
Correct |
1018 ms |
322804 KB |
Output is correct |
42 |
Correct |
988 ms |
326764 KB |
Output is correct |
43 |
Correct |
992 ms |
317500 KB |
Output is correct |
44 |
Correct |
1045 ms |
326856 KB |
Output is correct |
45 |
Correct |
1039 ms |
326820 KB |
Output is correct |
46 |
Correct |
1039 ms |
318912 KB |
Output is correct |
47 |
Correct |
1161 ms |
319920 KB |
Output is correct |
48 |
Correct |
1221 ms |
319700 KB |
Output is correct |
49 |
Correct |
1157 ms |
325316 KB |
Output is correct |
50 |
Correct |
1126 ms |
327896 KB |
Output is correct |
51 |
Correct |
38 ms |
89680 KB |
Output is correct |
52 |
Correct |
809 ms |
312572 KB |
Output is correct |
53 |
Correct |
816 ms |
312788 KB |
Output is correct |
54 |
Correct |
867 ms |
312800 KB |
Output is correct |
55 |
Correct |
827 ms |
312792 KB |
Output is correct |
56 |
Correct |
850 ms |
312640 KB |
Output is correct |
57 |
Correct |
816 ms |
312820 KB |
Output is correct |
58 |
Correct |
934 ms |
319316 KB |
Output is correct |
59 |
Correct |
916 ms |
319060 KB |
Output is correct |
60 |
Correct |
1038 ms |
314712 KB |
Output is correct |
61 |
Correct |
998 ms |
315220 KB |
Output is correct |
62 |
Correct |
875 ms |
336228 KB |
Output is correct |
63 |
Correct |
886 ms |
336212 KB |
Output is correct |
64 |
Correct |
849 ms |
335260 KB |
Output is correct |
65 |
Correct |
877 ms |
336136 KB |
Output is correct |
66 |
Correct |
840 ms |
321348 KB |
Output is correct |
67 |
Correct |
910 ms |
321956 KB |
Output is correct |
68 |
Correct |
908 ms |
321420 KB |
Output is correct |
69 |
Correct |
841 ms |
321352 KB |
Output is correct |
70 |
Correct |
884 ms |
321360 KB |
Output is correct |
71 |
Correct |
870 ms |
321240 KB |
Output is correct |
72 |
Correct |
842 ms |
321364 KB |
Output is correct |
73 |
Correct |
994 ms |
321340 KB |
Output is correct |
74 |
Correct |
875 ms |
321352 KB |
Output is correct |
75 |
Correct |
956 ms |
321552 KB |
Output is correct |
76 |
Correct |
841 ms |
316756 KB |
Output is correct |
77 |
Correct |
713 ms |
313940 KB |
Output is correct |
78 |
Correct |
1073 ms |
319544 KB |
Output is correct |
79 |
Correct |
1027 ms |
315960 KB |
Output is correct |
80 |
Correct |
1014 ms |
327740 KB |
Output is correct |
81 |
Correct |
1067 ms |
323008 KB |
Output is correct |
82 |
Correct |
1014 ms |
322628 KB |
Output is correct |
83 |
Correct |
1047 ms |
316876 KB |
Output is correct |
84 |
Correct |
1081 ms |
326364 KB |
Output is correct |
85 |
Correct |
1018 ms |
324488 KB |
Output is correct |
86 |
Correct |
1004 ms |
315812 KB |
Output is correct |
87 |
Correct |
966 ms |
319428 KB |
Output is correct |
88 |
Correct |
1069 ms |
322748 KB |
Output is correct |
89 |
Correct |
987 ms |
316412 KB |
Output is correct |
90 |
Correct |
972 ms |
316972 KB |
Output is correct |
91 |
Correct |
1014 ms |
319992 KB |
Output is correct |
92 |
Correct |
1117 ms |
317468 KB |
Output is correct |
93 |
Correct |
953 ms |
317292 KB |
Output is correct |
94 |
Correct |
967 ms |
315984 KB |
Output is correct |
95 |
Correct |
999 ms |
319620 KB |
Output is correct |
96 |
Correct |
977 ms |
316540 KB |
Output is correct |
97 |
Correct |
1097 ms |
320068 KB |
Output is correct |