#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector <int> g[N];
int color[N], viz[N], sz[N], team[N], newDad[N], usedColor[N], newViz[N];
vector <int> col[N];
int n, k, ans = N;
void dfsSize(int nod, int dad, int boss)
{
sz[nod] = 1;
newViz[nod] = 0;
team[nod] = boss;
newDad[nod] = dad;
usedColor[color[nod]] = 0;
for (auto son : g[nod])
{
if (son == dad || viz[son])
continue;
dfsSize(son, nod, boss);
sz[nod] += sz[son];
}
}
int findCentroid(int nod, int dad, int myN)
{
for (auto son : g[nod])
{
if (son == dad || viz[son])
continue;
if (sz[son] > myN / 2)
return findCentroid(son, nod, myN);
}
return nod;
}
void centroidDecomp(int nod)
{
dfsSize(nod, 0, nod);
int cnt = 0, mid = findCentroid(nod, 0, sz[nod]), ok = 1;
dfsSize(mid, 0, mid);
viz[mid] = 1;
queue <int> q;
q.push(mid);
while (ok && !q.empty())
{
int x = q.front();
int cx = color[x];
q.pop();
if (usedColor[cx])
continue;
cnt++;
usedColor[cx] = 1;
for (auto y : col[cx])
{
if (team[y] != mid)
{
ok = 0;
break;
}
int cy = color[newDad[y]];
if (y == mid || usedColor[cy])
continue;
q.push(newDad[y]);
}
}
if (ok)
ans = min(ans, cnt - 1);
for (auto z : g[mid])
if (!viz[z])
centroidDecomp(z);
}
int main()
{
//freopen("x.in", "r", stdin);
// freopen("x.out", "w", stdout);
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 >> color[i];
col[color[i]].push_back(i);
}
centroidDecomp(1);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9680 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
8 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9696 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9680 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
8 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9696 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
9 ms |
9804 KB |
Output is correct |
12 |
Correct |
9 ms |
9804 KB |
Output is correct |
13 |
Correct |
10 ms |
9804 KB |
Output is correct |
14 |
Correct |
10 ms |
9768 KB |
Output is correct |
15 |
Correct |
9 ms |
9836 KB |
Output is correct |
16 |
Correct |
10 ms |
9840 KB |
Output is correct |
17 |
Correct |
10 ms |
9932 KB |
Output is correct |
18 |
Correct |
10 ms |
9980 KB |
Output is correct |
19 |
Correct |
11 ms |
9904 KB |
Output is correct |
20 |
Correct |
9 ms |
9968 KB |
Output is correct |
21 |
Correct |
9 ms |
9932 KB |
Output is correct |
22 |
Correct |
11 ms |
10008 KB |
Output is correct |
23 |
Correct |
10 ms |
9932 KB |
Output is correct |
24 |
Correct |
11 ms |
9840 KB |
Output is correct |
25 |
Correct |
9 ms |
9964 KB |
Output is correct |
26 |
Correct |
10 ms |
9932 KB |
Output is correct |
27 |
Correct |
10 ms |
9932 KB |
Output is correct |
28 |
Correct |
10 ms |
9972 KB |
Output is correct |
29 |
Correct |
9 ms |
9968 KB |
Output is correct |
30 |
Correct |
10 ms |
9932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
938 ms |
39564 KB |
Output is correct |
2 |
Correct |
381 ms |
43488 KB |
Output is correct |
3 |
Correct |
868 ms |
42580 KB |
Output is correct |
4 |
Correct |
388 ms |
43456 KB |
Output is correct |
5 |
Correct |
874 ms |
39692 KB |
Output is correct |
6 |
Correct |
383 ms |
43432 KB |
Output is correct |
7 |
Correct |
885 ms |
39996 KB |
Output is correct |
8 |
Correct |
389 ms |
42952 KB |
Output is correct |
9 |
Correct |
1025 ms |
38096 KB |
Output is correct |
10 |
Correct |
978 ms |
35688 KB |
Output is correct |
11 |
Correct |
1020 ms |
38604 KB |
Output is correct |
12 |
Correct |
966 ms |
41192 KB |
Output is correct |
13 |
Correct |
936 ms |
35244 KB |
Output is correct |
14 |
Correct |
983 ms |
41508 KB |
Output is correct |
15 |
Correct |
1016 ms |
41136 KB |
Output is correct |
16 |
Correct |
936 ms |
36236 KB |
Output is correct |
17 |
Correct |
929 ms |
36820 KB |
Output is correct |
18 |
Correct |
916 ms |
36872 KB |
Output is correct |
19 |
Correct |
920 ms |
40004 KB |
Output is correct |
20 |
Correct |
923 ms |
42288 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9680 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
8 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9696 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
9 ms |
9804 KB |
Output is correct |
12 |
Correct |
9 ms |
9804 KB |
Output is correct |
13 |
Correct |
10 ms |
9804 KB |
Output is correct |
14 |
Correct |
10 ms |
9768 KB |
Output is correct |
15 |
Correct |
9 ms |
9836 KB |
Output is correct |
16 |
Correct |
10 ms |
9840 KB |
Output is correct |
17 |
Correct |
10 ms |
9932 KB |
Output is correct |
18 |
Correct |
10 ms |
9980 KB |
Output is correct |
19 |
Correct |
11 ms |
9904 KB |
Output is correct |
20 |
Correct |
9 ms |
9968 KB |
Output is correct |
21 |
Correct |
9 ms |
9932 KB |
Output is correct |
22 |
Correct |
11 ms |
10008 KB |
Output is correct |
23 |
Correct |
10 ms |
9932 KB |
Output is correct |
24 |
Correct |
11 ms |
9840 KB |
Output is correct |
25 |
Correct |
9 ms |
9964 KB |
Output is correct |
26 |
Correct |
10 ms |
9932 KB |
Output is correct |
27 |
Correct |
10 ms |
9932 KB |
Output is correct |
28 |
Correct |
10 ms |
9972 KB |
Output is correct |
29 |
Correct |
9 ms |
9968 KB |
Output is correct |
30 |
Correct |
10 ms |
9932 KB |
Output is correct |
31 |
Correct |
938 ms |
39564 KB |
Output is correct |
32 |
Correct |
381 ms |
43488 KB |
Output is correct |
33 |
Correct |
868 ms |
42580 KB |
Output is correct |
34 |
Correct |
388 ms |
43456 KB |
Output is correct |
35 |
Correct |
874 ms |
39692 KB |
Output is correct |
36 |
Correct |
383 ms |
43432 KB |
Output is correct |
37 |
Correct |
885 ms |
39996 KB |
Output is correct |
38 |
Correct |
389 ms |
42952 KB |
Output is correct |
39 |
Correct |
1025 ms |
38096 KB |
Output is correct |
40 |
Correct |
978 ms |
35688 KB |
Output is correct |
41 |
Correct |
1020 ms |
38604 KB |
Output is correct |
42 |
Correct |
966 ms |
41192 KB |
Output is correct |
43 |
Correct |
936 ms |
35244 KB |
Output is correct |
44 |
Correct |
983 ms |
41508 KB |
Output is correct |
45 |
Correct |
1016 ms |
41136 KB |
Output is correct |
46 |
Correct |
936 ms |
36236 KB |
Output is correct |
47 |
Correct |
929 ms |
36820 KB |
Output is correct |
48 |
Correct |
916 ms |
36872 KB |
Output is correct |
49 |
Correct |
920 ms |
40004 KB |
Output is correct |
50 |
Correct |
923 ms |
42288 KB |
Output is correct |
51 |
Correct |
6 ms |
9676 KB |
Output is correct |
52 |
Correct |
653 ms |
26276 KB |
Output is correct |
53 |
Correct |
707 ms |
26180 KB |
Output is correct |
54 |
Correct |
705 ms |
26252 KB |
Output is correct |
55 |
Correct |
695 ms |
26344 KB |
Output is correct |
56 |
Correct |
793 ms |
26340 KB |
Output is correct |
57 |
Correct |
671 ms |
26124 KB |
Output is correct |
58 |
Correct |
721 ms |
29888 KB |
Output is correct |
59 |
Correct |
694 ms |
30236 KB |
Output is correct |
60 |
Correct |
787 ms |
29508 KB |
Output is correct |
61 |
Correct |
862 ms |
29184 KB |
Output is correct |
62 |
Correct |
379 ms |
43460 KB |
Output is correct |
63 |
Correct |
378 ms |
43492 KB |
Output is correct |
64 |
Correct |
377 ms |
43204 KB |
Output is correct |
65 |
Correct |
373 ms |
43448 KB |
Output is correct |
66 |
Correct |
579 ms |
34876 KB |
Output is correct |
67 |
Correct |
593 ms |
34688 KB |
Output is correct |
68 |
Correct |
633 ms |
34860 KB |
Output is correct |
69 |
Correct |
600 ms |
34816 KB |
Output is correct |
70 |
Correct |
586 ms |
34720 KB |
Output is correct |
71 |
Correct |
585 ms |
34828 KB |
Output is correct |
72 |
Correct |
616 ms |
34728 KB |
Output is correct |
73 |
Correct |
586 ms |
33852 KB |
Output is correct |
74 |
Correct |
584 ms |
34748 KB |
Output is correct |
75 |
Correct |
585 ms |
34756 KB |
Output is correct |
76 |
Correct |
804 ms |
33608 KB |
Output is correct |
77 |
Correct |
785 ms |
31680 KB |
Output is correct |
78 |
Correct |
937 ms |
36812 KB |
Output is correct |
79 |
Correct |
915 ms |
34672 KB |
Output is correct |
80 |
Correct |
975 ms |
42016 KB |
Output is correct |
81 |
Correct |
922 ms |
38340 KB |
Output is correct |
82 |
Correct |
974 ms |
38436 KB |
Output is correct |
83 |
Correct |
902 ms |
35140 KB |
Output is correct |
84 |
Correct |
939 ms |
40904 KB |
Output is correct |
85 |
Correct |
938 ms |
39620 KB |
Output is correct |
86 |
Correct |
916 ms |
34632 KB |
Output is correct |
87 |
Correct |
919 ms |
36584 KB |
Output is correct |
88 |
Correct |
838 ms |
37608 KB |
Output is correct |
89 |
Correct |
817 ms |
33732 KB |
Output is correct |
90 |
Correct |
865 ms |
33436 KB |
Output is correct |
91 |
Correct |
869 ms |
35812 KB |
Output is correct |
92 |
Correct |
900 ms |
34396 KB |
Output is correct |
93 |
Correct |
861 ms |
34136 KB |
Output is correct |
94 |
Correct |
864 ms |
33416 KB |
Output is correct |
95 |
Correct |
899 ms |
34976 KB |
Output is correct |
96 |
Correct |
956 ms |
33604 KB |
Output is correct |
97 |
Correct |
872 ms |
35588 KB |
Output is correct |