// In the name of God
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define lc 2 * v
#define rc 2 * v + 1
#define mid (s + e) / 2
#define _sz(e) (int)e.size()
#define _all(e) e.begin() , e.end()
#define _lcm(a , b) (1ll * a * b) / __gcd(a , b)
#define ll long long
#define int long long
#define ld long double
#define pii pair <int , int>
#define pcc pair <char , char>
#define pll pair <long long , long long>
#define piii pair <int , pair <int , int>>
#define FAST ios::sync_with_stdio(0);cin.tie(0)
#pragma GCC optimize("Ofast")
// #pragma GCC target("ve,ve2,ve3,vse3,ve4,avmini,avmini2")
const int maxn = 3e5 + 10 , N = 2e5 + 10 , SQ = 320 , base = 800287 , mod = 1e9 + 7 , INF = 1e14 + 10 , lg = 22;
const ld eps = 1e-4;
int n , k , c[maxn] , res[maxn] , sz[maxn] , par[maxn];
bool death[maxn] , sub[maxn] , mark[maxn] , visited[maxn];
vector <int> edges[maxn] , town[maxn] , tmp;
void dfs_sz(int v , int p = -1) {
sz[v] = 1;
for (auto u : edges[v]) {
if(u != p && !death[u]) {
dfs_sz(u , v);
sz[v] += sz[u];
}
}
}
void dfs_sub(int v , int p = -1) {
sub[v] = 1 , par[v] = p , tmp.PB(v);
for (auto u : edges[v]) {
if(u != p && !death[u]) {
dfs_sub(u , v);
}
}
}
int find_centroid(int v , int tot , int p = -1) {
for (auto u : edges[v]) {
if(u != p && !death[u] && sz[u] > tot / 2) {
return find_centroid(u , tot , v);
}
}
return v;
}
void calc(int x) {
dfs_sub(x);
queue <int> q; q.push(x);
while(_sz(q)) {
int v = q.front(); q.pop();
if(visited[v]) {continue;}
visited[v] = 1 , tmp.PB(v);
if(!mark[c[v]]) {
for (auto u : town[c[v]]) {
if(!sub[u] || death[u]) {
res[x] = INF;
return ;
}
if(!visited[u]) {
q.push(u);
}
}
res[x]++;
mark[c[v]] = 1 , tmp.PB(c[v]);
}
if(par[v] != -1 && !visited[par[v]]) {
q.push(par[v]);
}
}
}
void centroid_tree() {
queue <int> q; dfs_sz(0);
q.push(find_centroid(0 , sz[0]));
while(_sz(q)) {
int v = q.front(); q.pop();
calc(v);
for (auto u : tmp) {
sub[u] = mark[u] = visited[u] = 0;
}
tmp.clear();
death[v] = 1;
for (auto u : edges[v]) {
if(!death[u]) {
dfs_sz(u);
q.push(find_centroid(u , sz[u]));
}
}
}
}
int32_t main() {
FAST;
cin >> n >> k;
for (int i = 0; i < n - 1; ++i) {
int x , y; cin >> x >> y;
x-- , y--;
edges[x].PB(y);
edges[y].PB(x);
}
for (int i = 0; i < n; ++i) {
cin >> c[i]; c[i]--;
town[c[i]].PB(i);
}
centroid_tree();
int ans = k - 1;
for (int i = 0; i < n; ++i) {
ans = min(ans , res[i] - 1);
}
cout << ans << '\n';
}
// Mistakes:
// * Read the problem carefully.
// * Check maxn.
// * Overflows.
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
10 ms |
14540 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
10 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
10 ms |
14540 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
10 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14404 KB |
Output is correct |
11 |
Correct |
12 ms |
14684 KB |
Output is correct |
12 |
Correct |
10 ms |
14668 KB |
Output is correct |
13 |
Correct |
11 ms |
14628 KB |
Output is correct |
14 |
Correct |
12 ms |
14668 KB |
Output is correct |
15 |
Correct |
11 ms |
14684 KB |
Output is correct |
16 |
Correct |
11 ms |
14668 KB |
Output is correct |
17 |
Correct |
11 ms |
14644 KB |
Output is correct |
18 |
Correct |
11 ms |
14604 KB |
Output is correct |
19 |
Correct |
10 ms |
14680 KB |
Output is correct |
20 |
Correct |
11 ms |
14684 KB |
Output is correct |
21 |
Correct |
11 ms |
14684 KB |
Output is correct |
22 |
Correct |
11 ms |
14680 KB |
Output is correct |
23 |
Correct |
11 ms |
14668 KB |
Output is correct |
24 |
Correct |
11 ms |
14668 KB |
Output is correct |
25 |
Correct |
11 ms |
14668 KB |
Output is correct |
26 |
Correct |
14 ms |
14756 KB |
Output is correct |
27 |
Correct |
12 ms |
14700 KB |
Output is correct |
28 |
Correct |
12 ms |
14744 KB |
Output is correct |
29 |
Correct |
12 ms |
14668 KB |
Output is correct |
30 |
Correct |
11 ms |
14668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
811 ms |
42044 KB |
Output is correct |
2 |
Correct |
234 ms |
42192 KB |
Output is correct |
3 |
Correct |
886 ms |
41912 KB |
Output is correct |
4 |
Correct |
231 ms |
42208 KB |
Output is correct |
5 |
Correct |
846 ms |
40972 KB |
Output is correct |
6 |
Correct |
219 ms |
42200 KB |
Output is correct |
7 |
Correct |
845 ms |
41244 KB |
Output is correct |
8 |
Correct |
229 ms |
42368 KB |
Output is correct |
9 |
Correct |
1035 ms |
41052 KB |
Output is correct |
10 |
Correct |
1049 ms |
40860 KB |
Output is correct |
11 |
Correct |
1054 ms |
41260 KB |
Output is correct |
12 |
Correct |
1105 ms |
42132 KB |
Output is correct |
13 |
Correct |
1012 ms |
40792 KB |
Output is correct |
14 |
Correct |
1090 ms |
42188 KB |
Output is correct |
15 |
Correct |
1040 ms |
42124 KB |
Output is correct |
16 |
Correct |
1075 ms |
40832 KB |
Output is correct |
17 |
Correct |
1176 ms |
40956 KB |
Output is correct |
18 |
Correct |
1142 ms |
40852 KB |
Output is correct |
19 |
Correct |
1107 ms |
41836 KB |
Output is correct |
20 |
Correct |
1127 ms |
42396 KB |
Output is correct |
21 |
Correct |
10 ms |
14412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
10 ms |
14540 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
10 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14404 KB |
Output is correct |
11 |
Correct |
12 ms |
14684 KB |
Output is correct |
12 |
Correct |
10 ms |
14668 KB |
Output is correct |
13 |
Correct |
11 ms |
14628 KB |
Output is correct |
14 |
Correct |
12 ms |
14668 KB |
Output is correct |
15 |
Correct |
11 ms |
14684 KB |
Output is correct |
16 |
Correct |
11 ms |
14668 KB |
Output is correct |
17 |
Correct |
11 ms |
14644 KB |
Output is correct |
18 |
Correct |
11 ms |
14604 KB |
Output is correct |
19 |
Correct |
10 ms |
14680 KB |
Output is correct |
20 |
Correct |
11 ms |
14684 KB |
Output is correct |
21 |
Correct |
11 ms |
14684 KB |
Output is correct |
22 |
Correct |
11 ms |
14680 KB |
Output is correct |
23 |
Correct |
11 ms |
14668 KB |
Output is correct |
24 |
Correct |
11 ms |
14668 KB |
Output is correct |
25 |
Correct |
11 ms |
14668 KB |
Output is correct |
26 |
Correct |
14 ms |
14756 KB |
Output is correct |
27 |
Correct |
12 ms |
14700 KB |
Output is correct |
28 |
Correct |
12 ms |
14744 KB |
Output is correct |
29 |
Correct |
12 ms |
14668 KB |
Output is correct |
30 |
Correct |
11 ms |
14668 KB |
Output is correct |
31 |
Correct |
811 ms |
42044 KB |
Output is correct |
32 |
Correct |
234 ms |
42192 KB |
Output is correct |
33 |
Correct |
886 ms |
41912 KB |
Output is correct |
34 |
Correct |
231 ms |
42208 KB |
Output is correct |
35 |
Correct |
846 ms |
40972 KB |
Output is correct |
36 |
Correct |
219 ms |
42200 KB |
Output is correct |
37 |
Correct |
845 ms |
41244 KB |
Output is correct |
38 |
Correct |
229 ms |
42368 KB |
Output is correct |
39 |
Correct |
1035 ms |
41052 KB |
Output is correct |
40 |
Correct |
1049 ms |
40860 KB |
Output is correct |
41 |
Correct |
1054 ms |
41260 KB |
Output is correct |
42 |
Correct |
1105 ms |
42132 KB |
Output is correct |
43 |
Correct |
1012 ms |
40792 KB |
Output is correct |
44 |
Correct |
1090 ms |
42188 KB |
Output is correct |
45 |
Correct |
1040 ms |
42124 KB |
Output is correct |
46 |
Correct |
1075 ms |
40832 KB |
Output is correct |
47 |
Correct |
1176 ms |
40956 KB |
Output is correct |
48 |
Correct |
1142 ms |
40852 KB |
Output is correct |
49 |
Correct |
1107 ms |
41836 KB |
Output is correct |
50 |
Correct |
1127 ms |
42396 KB |
Output is correct |
51 |
Correct |
10 ms |
14412 KB |
Output is correct |
52 |
Correct |
777 ms |
39396 KB |
Output is correct |
53 |
Correct |
870 ms |
39508 KB |
Output is correct |
54 |
Correct |
875 ms |
39348 KB |
Output is correct |
55 |
Correct |
921 ms |
39316 KB |
Output is correct |
56 |
Correct |
920 ms |
39452 KB |
Output is correct |
57 |
Correct |
871 ms |
39344 KB |
Output is correct |
58 |
Correct |
994 ms |
39776 KB |
Output is correct |
59 |
Correct |
816 ms |
39360 KB |
Output is correct |
60 |
Correct |
982 ms |
38552 KB |
Output is correct |
61 |
Correct |
1008 ms |
38852 KB |
Output is correct |
62 |
Correct |
254 ms |
42196 KB |
Output is correct |
63 |
Correct |
238 ms |
42188 KB |
Output is correct |
64 |
Correct |
235 ms |
42332 KB |
Output is correct |
65 |
Correct |
248 ms |
42136 KB |
Output is correct |
66 |
Correct |
597 ms |
40852 KB |
Output is correct |
67 |
Correct |
638 ms |
40804 KB |
Output is correct |
68 |
Correct |
583 ms |
40812 KB |
Output is correct |
69 |
Correct |
636 ms |
40828 KB |
Output is correct |
70 |
Correct |
716 ms |
40740 KB |
Output is correct |
71 |
Correct |
710 ms |
40760 KB |
Output is correct |
72 |
Correct |
867 ms |
40784 KB |
Output is correct |
73 |
Correct |
705 ms |
40828 KB |
Output is correct |
74 |
Correct |
605 ms |
40852 KB |
Output is correct |
75 |
Correct |
691 ms |
40848 KB |
Output is correct |
76 |
Correct |
1393 ms |
40320 KB |
Output is correct |
77 |
Correct |
1225 ms |
40228 KB |
Output is correct |
78 |
Correct |
1329 ms |
40792 KB |
Output is correct |
79 |
Correct |
1339 ms |
40700 KB |
Output is correct |
80 |
Correct |
1359 ms |
42340 KB |
Output is correct |
81 |
Correct |
1290 ms |
41148 KB |
Output is correct |
82 |
Correct |
1343 ms |
41168 KB |
Output is correct |
83 |
Correct |
1399 ms |
40852 KB |
Output is correct |
84 |
Correct |
1360 ms |
42048 KB |
Output is correct |
85 |
Correct |
1328 ms |
41488 KB |
Output is correct |
86 |
Correct |
1177 ms |
40772 KB |
Output is correct |
87 |
Correct |
947 ms |
40876 KB |
Output is correct |
88 |
Correct |
838 ms |
41272 KB |
Output is correct |
89 |
Correct |
794 ms |
40244 KB |
Output is correct |
90 |
Correct |
788 ms |
40292 KB |
Output is correct |
91 |
Correct |
834 ms |
40524 KB |
Output is correct |
92 |
Correct |
968 ms |
40332 KB |
Output is correct |
93 |
Correct |
965 ms |
40232 KB |
Output is correct |
94 |
Correct |
1042 ms |
40276 KB |
Output is correct |
95 |
Correct |
1076 ms |
40456 KB |
Output is correct |
96 |
Correct |
1058 ms |
40300 KB |
Output is correct |
97 |
Correct |
1066 ms |
40540 KB |
Output is correct |