#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 2e5 + 7;
const int INF = (int) 1e9;
int c[N], timestamp[N], added[N], p[N], subarb[N];
vector<int> g[N], colors[N];
bool visited[N];
void getsize(int u, int par = 0)
{
subarb[u] = 1;
for (auto it : g[u])
{
if (visited[it] == false && it != par)
{
getsize(it, u);
subarb[u] += subarb[it];
}
}
}
int getcentroid(int u, int total, int par = 0)
{
for (auto it : g[u])
{
if (visited[it] == false && it != par && subarb[it] > total / 2)
{
return getcentroid(it, total, u);
}
}
return u;
}
int Time = 0;
void dfs(int u, int par = 0)
{
p[u] = par;
timestamp[u] = Time;
for (auto it : g[u])
{
if (visited[it] == false && it != par)
{
dfs(it, u);
}
}
}
int ret = INF;
void solve(int u)
{
getsize(u);
int centroid = getcentroid(u, subarb[u], 0);
visited[centroid] = 1;
Time++;
timestamp[centroid] = Time;
for (auto it : g[centroid])
{
if (visited[it] == false)
{
dfs(it, centroid);
}
}
queue<int> q;
bool ok = 1;
q.push(c[centroid]);
added[c[centroid]] = Time;
int cnt = 1;
while (!q.empty())
{
int color = q.front();
q.pop();
for (auto it : colors[color])
{
if (timestamp[it] != Time)
{
ok = 0;
break;
}
if (it != centroid && added[c[p[it]]] != Time)
{
q.push(c[p[it]]);
added[c[p[it]]] = Time;
cnt++;
}
}
if (ok == 0)
{
break;
}
}
if (ok)
{
ret = min(ret, cnt - 1);
}
for (auto it : g[centroid])
{
if (visited[it] == false)
{
solve(it);
}
}
}
int main()
{
#ifdef ONPC
freopen("input.txt", "r", stdin);
#endif // ONPC
#ifndef ONPC
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC
int n, k;
cin >> n >> k;
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1; i <= n; i++)
{
cin >> c[i];
colors[c[i]].push_back(i);
}
solve(1);
cout << ret;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9732 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9724 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9724 KB |
Output is correct |
8 |
Correct |
7 ms |
9724 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9732 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9724 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9724 KB |
Output is correct |
8 |
Correct |
7 ms |
9724 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9796 KB |
Output is correct |
12 |
Correct |
6 ms |
9868 KB |
Output is correct |
13 |
Correct |
7 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9872 KB |
Output is correct |
15 |
Correct |
7 ms |
9856 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9872 KB |
Output is correct |
19 |
Correct |
6 ms |
9872 KB |
Output is correct |
20 |
Correct |
8 ms |
9940 KB |
Output is correct |
21 |
Correct |
6 ms |
9916 KB |
Output is correct |
22 |
Correct |
6 ms |
10004 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
6 ms |
9940 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
10004 KB |
Output is correct |
28 |
Correct |
9 ms |
9940 KB |
Output is correct |
29 |
Correct |
7 ms |
9976 KB |
Output is correct |
30 |
Correct |
7 ms |
9868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
479 ms |
41720 KB |
Output is correct |
2 |
Correct |
166 ms |
42116 KB |
Output is correct |
3 |
Correct |
438 ms |
41332 KB |
Output is correct |
4 |
Correct |
164 ms |
42156 KB |
Output is correct |
5 |
Correct |
417 ms |
38316 KB |
Output is correct |
6 |
Correct |
183 ms |
42152 KB |
Output is correct |
7 |
Correct |
427 ms |
38580 KB |
Output is correct |
8 |
Correct |
176 ms |
41660 KB |
Output is correct |
9 |
Correct |
465 ms |
36720 KB |
Output is correct |
10 |
Correct |
485 ms |
34284 KB |
Output is correct |
11 |
Correct |
486 ms |
37140 KB |
Output is correct |
12 |
Correct |
494 ms |
39784 KB |
Output is correct |
13 |
Correct |
550 ms |
33916 KB |
Output is correct |
14 |
Correct |
500 ms |
40120 KB |
Output is correct |
15 |
Correct |
531 ms |
39708 KB |
Output is correct |
16 |
Correct |
482 ms |
34808 KB |
Output is correct |
17 |
Correct |
530 ms |
35428 KB |
Output is correct |
18 |
Correct |
542 ms |
35588 KB |
Output is correct |
19 |
Correct |
533 ms |
38824 KB |
Output is correct |
20 |
Correct |
514 ms |
40988 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9732 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9724 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9724 KB |
Output is correct |
8 |
Correct |
7 ms |
9724 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9796 KB |
Output is correct |
12 |
Correct |
6 ms |
9868 KB |
Output is correct |
13 |
Correct |
7 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9872 KB |
Output is correct |
15 |
Correct |
7 ms |
9856 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9872 KB |
Output is correct |
19 |
Correct |
6 ms |
9872 KB |
Output is correct |
20 |
Correct |
8 ms |
9940 KB |
Output is correct |
21 |
Correct |
6 ms |
9916 KB |
Output is correct |
22 |
Correct |
6 ms |
10004 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
6 ms |
9940 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
10004 KB |
Output is correct |
28 |
Correct |
9 ms |
9940 KB |
Output is correct |
29 |
Correct |
7 ms |
9976 KB |
Output is correct |
30 |
Correct |
7 ms |
9868 KB |
Output is correct |
31 |
Correct |
479 ms |
41720 KB |
Output is correct |
32 |
Correct |
166 ms |
42116 KB |
Output is correct |
33 |
Correct |
438 ms |
41332 KB |
Output is correct |
34 |
Correct |
164 ms |
42156 KB |
Output is correct |
35 |
Correct |
417 ms |
38316 KB |
Output is correct |
36 |
Correct |
183 ms |
42152 KB |
Output is correct |
37 |
Correct |
427 ms |
38580 KB |
Output is correct |
38 |
Correct |
176 ms |
41660 KB |
Output is correct |
39 |
Correct |
465 ms |
36720 KB |
Output is correct |
40 |
Correct |
485 ms |
34284 KB |
Output is correct |
41 |
Correct |
486 ms |
37140 KB |
Output is correct |
42 |
Correct |
494 ms |
39784 KB |
Output is correct |
43 |
Correct |
550 ms |
33916 KB |
Output is correct |
44 |
Correct |
500 ms |
40120 KB |
Output is correct |
45 |
Correct |
531 ms |
39708 KB |
Output is correct |
46 |
Correct |
482 ms |
34808 KB |
Output is correct |
47 |
Correct |
530 ms |
35428 KB |
Output is correct |
48 |
Correct |
542 ms |
35588 KB |
Output is correct |
49 |
Correct |
533 ms |
38824 KB |
Output is correct |
50 |
Correct |
514 ms |
40988 KB |
Output is correct |
51 |
Correct |
6 ms |
9684 KB |
Output is correct |
52 |
Correct |
356 ms |
24784 KB |
Output is correct |
53 |
Correct |
421 ms |
24656 KB |
Output is correct |
54 |
Correct |
367 ms |
24660 KB |
Output is correct |
55 |
Correct |
375 ms |
24664 KB |
Output is correct |
56 |
Correct |
459 ms |
24660 KB |
Output is correct |
57 |
Correct |
406 ms |
24592 KB |
Output is correct |
58 |
Correct |
352 ms |
28460 KB |
Output is correct |
59 |
Correct |
444 ms |
28896 KB |
Output is correct |
60 |
Correct |
485 ms |
28180 KB |
Output is correct |
61 |
Correct |
467 ms |
27960 KB |
Output is correct |
62 |
Correct |
168 ms |
42076 KB |
Output is correct |
63 |
Correct |
160 ms |
42056 KB |
Output is correct |
64 |
Correct |
171 ms |
41924 KB |
Output is correct |
65 |
Correct |
186 ms |
42016 KB |
Output is correct |
66 |
Correct |
293 ms |
33480 KB |
Output is correct |
67 |
Correct |
313 ms |
33352 KB |
Output is correct |
68 |
Correct |
352 ms |
33464 KB |
Output is correct |
69 |
Correct |
284 ms |
33480 KB |
Output is correct |
70 |
Correct |
306 ms |
33384 KB |
Output is correct |
71 |
Correct |
317 ms |
33296 KB |
Output is correct |
72 |
Correct |
278 ms |
33288 KB |
Output is correct |
73 |
Correct |
330 ms |
32548 KB |
Output is correct |
74 |
Correct |
268 ms |
33492 KB |
Output is correct |
75 |
Correct |
299 ms |
33432 KB |
Output is correct |
76 |
Correct |
414 ms |
32168 KB |
Output is correct |
77 |
Correct |
452 ms |
30308 KB |
Output is correct |
78 |
Correct |
602 ms |
35304 KB |
Output is correct |
79 |
Correct |
501 ms |
33272 KB |
Output is correct |
80 |
Correct |
513 ms |
40508 KB |
Output is correct |
81 |
Correct |
538 ms |
36884 KB |
Output is correct |
82 |
Correct |
504 ms |
37032 KB |
Output is correct |
83 |
Correct |
530 ms |
33740 KB |
Output is correct |
84 |
Correct |
571 ms |
39528 KB |
Output is correct |
85 |
Correct |
510 ms |
38056 KB |
Output is correct |
86 |
Correct |
470 ms |
33228 KB |
Output is correct |
87 |
Correct |
551 ms |
35024 KB |
Output is correct |
88 |
Correct |
450 ms |
36148 KB |
Output is correct |
89 |
Correct |
482 ms |
32136 KB |
Output is correct |
90 |
Correct |
427 ms |
31992 KB |
Output is correct |
91 |
Correct |
457 ms |
34444 KB |
Output is correct |
92 |
Correct |
425 ms |
32956 KB |
Output is correct |
93 |
Correct |
425 ms |
32696 KB |
Output is correct |
94 |
Correct |
427 ms |
31984 KB |
Output is correct |
95 |
Correct |
399 ms |
33572 KB |
Output is correct |
96 |
Correct |
442 ms |
32388 KB |
Output is correct |
97 |
Correct |
435 ms |
34352 KB |
Output is correct |