#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
const int MAXN = 2e5;
int n, k, color[MAXN], par[MAXN], s[MAXN], ans = MAXN;
vector<int> g[MAXN], h[MAXN];
bitset<MAXN> r, vis;
void calcSize(int u){
s[u] = 1;
for(int v : g[u]) if(!s[v]) calcSize(v), s[u] += s[v];
}
int find(int u, int p, int sz){
while(1){
bool f = 0;
s[p] -= s[u], s[u] = sz;
for(int v : g[u]){
if(!r[v] && s[v] > sz/2){
p = u, u = v;
f = 1;
break;
}
}
if(!f) return u;
}
}
void dfs(int u, int p, bool val){
par[u] = val ? p : -1;
if(val) vis[color[u]] = 0;
for(int v : g[u]) if(v != p && !r[v]) dfs(v, u, val);
}
void decompose(int c){
r[c = find(c, c, s[c])] = 1;
dfs(c, c, 1);
int res = 0;
vis[color[c]] = 1;
vector<int> st;
for(int i : h[color[c]]){
if(res == MAXN) break;
if(par[i] < 0) res = MAXN;
else st.push_back(i);
}
while(!st.empty() && res < MAXN){
int u = st.back(); st.pop_back();
if(!vis[color[par[u]]]){
++res;
for(int v : h[color[par[u]]]){
if(par[v] >= 0 && res < MAXN) st.push_back(v);
else res = MAXN;
}
if(res < MAXN) vis[color[par[u]]] = 1;
}
}
ans = min(ans, res);
dfs(c, c, 0);
for(int v : g[c]) if(!r[v]) decompose(v);
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int x, y; cin >> n >> k;
for(int i=1; i<n; ++i){
cin >> x >> y; --x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i=0; i<n; ++i){
cin >> color[i]; --color[i];
h[color[i]].push_back(i);
}
calcSize(0);
decompose(0);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9720 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9700 KB |
Output is correct |
6 |
Correct |
7 ms |
9680 KB |
Output is correct |
7 |
Correct |
8 ms |
9704 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9716 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9720 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9700 KB |
Output is correct |
6 |
Correct |
7 ms |
9680 KB |
Output is correct |
7 |
Correct |
8 ms |
9704 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9716 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
11 |
Correct |
9 ms |
9932 KB |
Output is correct |
12 |
Correct |
8 ms |
9948 KB |
Output is correct |
13 |
Correct |
8 ms |
9860 KB |
Output is correct |
14 |
Correct |
10 ms |
9920 KB |
Output is correct |
15 |
Correct |
8 ms |
9864 KB |
Output is correct |
16 |
Correct |
10 ms |
9904 KB |
Output is correct |
17 |
Correct |
9 ms |
9932 KB |
Output is correct |
18 |
Correct |
10 ms |
9864 KB |
Output is correct |
19 |
Correct |
9 ms |
9952 KB |
Output is correct |
20 |
Correct |
10 ms |
9932 KB |
Output is correct |
21 |
Correct |
7 ms |
9932 KB |
Output is correct |
22 |
Correct |
7 ms |
9916 KB |
Output is correct |
23 |
Correct |
7 ms |
9932 KB |
Output is correct |
24 |
Correct |
7 ms |
9864 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
12 ms |
9992 KB |
Output is correct |
27 |
Correct |
10 ms |
9932 KB |
Output is correct |
28 |
Correct |
9 ms |
9932 KB |
Output is correct |
29 |
Correct |
11 ms |
9916 KB |
Output is correct |
30 |
Correct |
10 ms |
9932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
616 ms |
34684 KB |
Output is correct |
2 |
Correct |
235 ms |
34956 KB |
Output is correct |
3 |
Correct |
638 ms |
34548 KB |
Output is correct |
4 |
Correct |
212 ms |
34928 KB |
Output is correct |
5 |
Correct |
586 ms |
32804 KB |
Output is correct |
6 |
Correct |
236 ms |
35004 KB |
Output is correct |
7 |
Correct |
571 ms |
33184 KB |
Output is correct |
8 |
Correct |
202 ms |
35684 KB |
Output is correct |
9 |
Correct |
1508 ms |
33308 KB |
Output is correct |
10 |
Correct |
1579 ms |
31704 KB |
Output is correct |
11 |
Correct |
1438 ms |
32824 KB |
Output is correct |
12 |
Correct |
1298 ms |
34316 KB |
Output is correct |
13 |
Correct |
1186 ms |
31604 KB |
Output is correct |
14 |
Correct |
1353 ms |
34712 KB |
Output is correct |
15 |
Correct |
1126 ms |
34476 KB |
Output is correct |
16 |
Correct |
1315 ms |
31972 KB |
Output is correct |
17 |
Correct |
1275 ms |
32076 KB |
Output is correct |
18 |
Correct |
1267 ms |
32036 KB |
Output is correct |
19 |
Correct |
1027 ms |
33724 KB |
Output is correct |
20 |
Correct |
1710 ms |
34988 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9720 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9700 KB |
Output is correct |
6 |
Correct |
7 ms |
9680 KB |
Output is correct |
7 |
Correct |
8 ms |
9704 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9716 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
11 |
Correct |
9 ms |
9932 KB |
Output is correct |
12 |
Correct |
8 ms |
9948 KB |
Output is correct |
13 |
Correct |
8 ms |
9860 KB |
Output is correct |
14 |
Correct |
10 ms |
9920 KB |
Output is correct |
15 |
Correct |
8 ms |
9864 KB |
Output is correct |
16 |
Correct |
10 ms |
9904 KB |
Output is correct |
17 |
Correct |
9 ms |
9932 KB |
Output is correct |
18 |
Correct |
10 ms |
9864 KB |
Output is correct |
19 |
Correct |
9 ms |
9952 KB |
Output is correct |
20 |
Correct |
10 ms |
9932 KB |
Output is correct |
21 |
Correct |
7 ms |
9932 KB |
Output is correct |
22 |
Correct |
7 ms |
9916 KB |
Output is correct |
23 |
Correct |
7 ms |
9932 KB |
Output is correct |
24 |
Correct |
7 ms |
9864 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
12 ms |
9992 KB |
Output is correct |
27 |
Correct |
10 ms |
9932 KB |
Output is correct |
28 |
Correct |
9 ms |
9932 KB |
Output is correct |
29 |
Correct |
11 ms |
9916 KB |
Output is correct |
30 |
Correct |
10 ms |
9932 KB |
Output is correct |
31 |
Correct |
616 ms |
34684 KB |
Output is correct |
32 |
Correct |
235 ms |
34956 KB |
Output is correct |
33 |
Correct |
638 ms |
34548 KB |
Output is correct |
34 |
Correct |
212 ms |
34928 KB |
Output is correct |
35 |
Correct |
586 ms |
32804 KB |
Output is correct |
36 |
Correct |
236 ms |
35004 KB |
Output is correct |
37 |
Correct |
571 ms |
33184 KB |
Output is correct |
38 |
Correct |
202 ms |
35684 KB |
Output is correct |
39 |
Correct |
1508 ms |
33308 KB |
Output is correct |
40 |
Correct |
1579 ms |
31704 KB |
Output is correct |
41 |
Correct |
1438 ms |
32824 KB |
Output is correct |
42 |
Correct |
1298 ms |
34316 KB |
Output is correct |
43 |
Correct |
1186 ms |
31604 KB |
Output is correct |
44 |
Correct |
1353 ms |
34712 KB |
Output is correct |
45 |
Correct |
1126 ms |
34476 KB |
Output is correct |
46 |
Correct |
1315 ms |
31972 KB |
Output is correct |
47 |
Correct |
1275 ms |
32076 KB |
Output is correct |
48 |
Correct |
1267 ms |
32036 KB |
Output is correct |
49 |
Correct |
1027 ms |
33724 KB |
Output is correct |
50 |
Correct |
1710 ms |
34988 KB |
Output is correct |
51 |
Correct |
7 ms |
9676 KB |
Output is correct |
52 |
Correct |
556 ms |
27996 KB |
Output is correct |
53 |
Correct |
541 ms |
27924 KB |
Output is correct |
54 |
Correct |
595 ms |
27972 KB |
Output is correct |
55 |
Correct |
561 ms |
27988 KB |
Output is correct |
56 |
Correct |
560 ms |
28028 KB |
Output is correct |
57 |
Correct |
564 ms |
27996 KB |
Output is correct |
58 |
Correct |
496 ms |
29156 KB |
Output is correct |
59 |
Correct |
487 ms |
28676 KB |
Output is correct |
60 |
Correct |
602 ms |
27432 KB |
Output is correct |
61 |
Correct |
702 ms |
27632 KB |
Output is correct |
62 |
Correct |
228 ms |
34300 KB |
Output is correct |
63 |
Correct |
212 ms |
34356 KB |
Output is correct |
64 |
Correct |
194 ms |
34320 KB |
Output is correct |
65 |
Correct |
215 ms |
34516 KB |
Output is correct |
66 |
Correct |
358 ms |
31560 KB |
Output is correct |
67 |
Correct |
365 ms |
31544 KB |
Output is correct |
68 |
Correct |
409 ms |
31564 KB |
Output is correct |
69 |
Correct |
398 ms |
31568 KB |
Output is correct |
70 |
Correct |
363 ms |
31540 KB |
Output is correct |
71 |
Correct |
347 ms |
31608 KB |
Output is correct |
72 |
Correct |
374 ms |
31560 KB |
Output is correct |
73 |
Correct |
374 ms |
31556 KB |
Output is correct |
74 |
Correct |
385 ms |
31464 KB |
Output is correct |
75 |
Correct |
399 ms |
31560 KB |
Output is correct |
76 |
Correct |
575 ms |
30248 KB |
Output is correct |
77 |
Correct |
574 ms |
29780 KB |
Output is correct |
78 |
Correct |
1082 ms |
31800 KB |
Output is correct |
79 |
Correct |
1099 ms |
31772 KB |
Output is correct |
80 |
Correct |
1694 ms |
34484 KB |
Output is correct |
81 |
Correct |
1400 ms |
32444 KB |
Output is correct |
82 |
Correct |
1101 ms |
32552 KB |
Output is correct |
83 |
Correct |
1241 ms |
32012 KB |
Output is correct |
84 |
Correct |
974 ms |
34120 KB |
Output is correct |
85 |
Correct |
1260 ms |
33248 KB |
Output is correct |
86 |
Correct |
1160 ms |
31704 KB |
Output is correct |
87 |
Correct |
1346 ms |
31676 KB |
Output is correct |
88 |
Correct |
752 ms |
32656 KB |
Output is correct |
89 |
Correct |
926 ms |
30856 KB |
Output is correct |
90 |
Correct |
1077 ms |
30940 KB |
Output is correct |
91 |
Correct |
1079 ms |
31316 KB |
Output is correct |
92 |
Correct |
715 ms |
30996 KB |
Output is correct |
93 |
Correct |
937 ms |
30740 KB |
Output is correct |
94 |
Correct |
930 ms |
30652 KB |
Output is correct |
95 |
Correct |
941 ms |
30948 KB |
Output is correct |
96 |
Correct |
701 ms |
30768 KB |
Output is correct |
97 |
Correct |
927 ms |
31620 KB |
Output is correct |