#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
using pii=pair<int, int>;
const int MX = 2e5 + 5;
int N, K, C[MX];
int A, vis[MX], sz[MX];
vector<int> adj[MX], ele[MX];
int P[MX], cnt[MX], use[MX], clh[MX];
void d1(int n, int p) {
sz[n]=1; cnt[C[n]]++;
for (auto &i:adj[n])
if (i!=p&&!vis[i]) d1(i, n), sz[n]+=sz[i];
}
int d2(int n, int p, int c) {
for (auto &i:adj[n])
if (i!=p&&!vis[i]&&sz[i]>=c) return d2(i, n, c);
return n;
}
void d3(int n) {
vector<pii> V;
V.eb(n, 0);
for (int i=0; i<V.size(); i++) {
int x, y;
x=V[i].first, y=V[i].second;
P[x]=y;
for (auto &j:adj[x])
if (j!=y&&!vis[j]) V.eb(j, x);
}
}
void d4(int n) {
vector<pii> V;
V.eb(n, 0);
for (int i=0; i<V.size(); i++) {
int x, y;
x=V[i].first, y=V[i].second;
cnt[C[x]]=0;
for (auto &j:adj[x])
if (j!=y&&!vis[j]) V.eb(j, x);
}
}
void sol(int st) {
d1(st, 0);
int c=d2(st, 0, sz[st]/2+1);
d3(c);
int fl=0, d=0;
vector<int> s; s.eb(c);
for (int i=0; i<s.size(); i++) {
int x=s[i];
if (use[x]) continue;
use[x]=1;
if (ele[C[x]].size()!=cnt[C[x]]) { fl=1; break; }
if (!clh[C[x]]) {
clh[C[x]]=1;
d++;
for (auto &j:ele[C[x]]) s.eb(j);
}
if (P[x]) s.eb(P[x]);
}
if (!fl) A=min(A, d);
for (auto &i:s) use[i]=0, clh[C[i]]=0;
d4(st);
s.clear();
vis[c]=1;
for (auto &i:adj[c]) if (!vis[i]) sol(i);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin>>N>>K;
for (int i=1, u, v; i<N; i++) cin>>u>>v, adj[u].eb(v), adj[v].eb(u);
for (int i=1; i<=N; i++) cin>>C[i], ele[C[i]].eb(i);
A=MX;
sol(1);
cout<<A-1<<'\n';
return 0;
}
Compilation message
capital_city.cpp: In function 'void d3(int)':
capital_city.cpp:28:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int i=0; i<V.size(); i++) {
| ~^~~~~~~~~
capital_city.cpp: In function 'void d4(int)':
capital_city.cpp:40:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i=0; i<V.size(); i++) {
| ~^~~~~~~~~
capital_city.cpp: In function 'void sol(int)':
capital_city.cpp:56:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i=0; i<s.size(); i++) {
| ~^~~~~~~~~
capital_city.cpp:60:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | if (ele[C[x]].size()!=cnt[C[x]]) { fl=1; break; }
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9728 KB |
Output is correct |
2 |
Correct |
8 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
7 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9728 KB |
Output is correct |
2 |
Correct |
8 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
7 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9984 KB |
Output is correct |
12 |
Correct |
10 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9984 KB |
Output is correct |
14 |
Correct |
12 ms |
9984 KB |
Output is correct |
15 |
Correct |
10 ms |
9984 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
9 ms |
9984 KB |
Output is correct |
18 |
Correct |
9 ms |
9984 KB |
Output is correct |
19 |
Correct |
9 ms |
9984 KB |
Output is correct |
20 |
Correct |
9 ms |
9984 KB |
Output is correct |
21 |
Correct |
9 ms |
9984 KB |
Output is correct |
22 |
Correct |
10 ms |
9984 KB |
Output is correct |
23 |
Correct |
10 ms |
9984 KB |
Output is correct |
24 |
Correct |
11 ms |
9984 KB |
Output is correct |
25 |
Correct |
10 ms |
9984 KB |
Output is correct |
26 |
Correct |
10 ms |
9984 KB |
Output is correct |
27 |
Correct |
10 ms |
9984 KB |
Output is correct |
28 |
Correct |
10 ms |
9984 KB |
Output is correct |
29 |
Correct |
10 ms |
9984 KB |
Output is correct |
30 |
Correct |
10 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
737 ms |
34780 KB |
Output is correct |
2 |
Correct |
314 ms |
35008 KB |
Output is correct |
3 |
Correct |
737 ms |
34720 KB |
Output is correct |
4 |
Correct |
320 ms |
35096 KB |
Output is correct |
5 |
Correct |
767 ms |
38388 KB |
Output is correct |
6 |
Correct |
314 ms |
38840 KB |
Output is correct |
7 |
Correct |
778 ms |
38396 KB |
Output is correct |
8 |
Correct |
340 ms |
40264 KB |
Output is correct |
9 |
Correct |
1020 ms |
37304 KB |
Output is correct |
10 |
Correct |
958 ms |
35912 KB |
Output is correct |
11 |
Correct |
868 ms |
37688 KB |
Output is correct |
12 |
Correct |
926 ms |
39356 KB |
Output is correct |
13 |
Correct |
932 ms |
35700 KB |
Output is correct |
14 |
Correct |
891 ms |
39420 KB |
Output is correct |
15 |
Correct |
935 ms |
39352 KB |
Output is correct |
16 |
Correct |
904 ms |
36280 KB |
Output is correct |
17 |
Correct |
908 ms |
36792 KB |
Output is correct |
18 |
Correct |
969 ms |
36792 KB |
Output is correct |
19 |
Correct |
909 ms |
38620 KB |
Output is correct |
20 |
Correct |
907 ms |
40016 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9728 KB |
Output is correct |
2 |
Correct |
8 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
7 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9984 KB |
Output is correct |
12 |
Correct |
10 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9984 KB |
Output is correct |
14 |
Correct |
12 ms |
9984 KB |
Output is correct |
15 |
Correct |
10 ms |
9984 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
9 ms |
9984 KB |
Output is correct |
18 |
Correct |
9 ms |
9984 KB |
Output is correct |
19 |
Correct |
9 ms |
9984 KB |
Output is correct |
20 |
Correct |
9 ms |
9984 KB |
Output is correct |
21 |
Correct |
9 ms |
9984 KB |
Output is correct |
22 |
Correct |
10 ms |
9984 KB |
Output is correct |
23 |
Correct |
10 ms |
9984 KB |
Output is correct |
24 |
Correct |
11 ms |
9984 KB |
Output is correct |
25 |
Correct |
10 ms |
9984 KB |
Output is correct |
26 |
Correct |
10 ms |
9984 KB |
Output is correct |
27 |
Correct |
10 ms |
9984 KB |
Output is correct |
28 |
Correct |
10 ms |
9984 KB |
Output is correct |
29 |
Correct |
10 ms |
9984 KB |
Output is correct |
30 |
Correct |
10 ms |
9984 KB |
Output is correct |
31 |
Correct |
737 ms |
34780 KB |
Output is correct |
32 |
Correct |
314 ms |
35008 KB |
Output is correct |
33 |
Correct |
737 ms |
34720 KB |
Output is correct |
34 |
Correct |
320 ms |
35096 KB |
Output is correct |
35 |
Correct |
767 ms |
38388 KB |
Output is correct |
36 |
Correct |
314 ms |
38840 KB |
Output is correct |
37 |
Correct |
778 ms |
38396 KB |
Output is correct |
38 |
Correct |
340 ms |
40264 KB |
Output is correct |
39 |
Correct |
1020 ms |
37304 KB |
Output is correct |
40 |
Correct |
958 ms |
35912 KB |
Output is correct |
41 |
Correct |
868 ms |
37688 KB |
Output is correct |
42 |
Correct |
926 ms |
39356 KB |
Output is correct |
43 |
Correct |
932 ms |
35700 KB |
Output is correct |
44 |
Correct |
891 ms |
39420 KB |
Output is correct |
45 |
Correct |
935 ms |
39352 KB |
Output is correct |
46 |
Correct |
904 ms |
36280 KB |
Output is correct |
47 |
Correct |
908 ms |
36792 KB |
Output is correct |
48 |
Correct |
969 ms |
36792 KB |
Output is correct |
49 |
Correct |
909 ms |
38620 KB |
Output is correct |
50 |
Correct |
907 ms |
40016 KB |
Output is correct |
51 |
Correct |
7 ms |
9728 KB |
Output is correct |
52 |
Correct |
613 ms |
30576 KB |
Output is correct |
53 |
Correct |
657 ms |
30660 KB |
Output is correct |
54 |
Correct |
647 ms |
30648 KB |
Output is correct |
55 |
Correct |
682 ms |
30724 KB |
Output is correct |
56 |
Correct |
682 ms |
30488 KB |
Output is correct |
57 |
Correct |
658 ms |
30676 KB |
Output is correct |
58 |
Correct |
716 ms |
32352 KB |
Output is correct |
59 |
Correct |
680 ms |
30612 KB |
Output is correct |
60 |
Correct |
814 ms |
30284 KB |
Output is correct |
61 |
Correct |
820 ms |
31992 KB |
Output is correct |
62 |
Correct |
314 ms |
38784 KB |
Output is correct |
63 |
Correct |
322 ms |
38756 KB |
Output is correct |
64 |
Correct |
318 ms |
40340 KB |
Output is correct |
65 |
Correct |
318 ms |
38736 KB |
Output is correct |
66 |
Correct |
549 ms |
35788 KB |
Output is correct |
67 |
Correct |
492 ms |
35460 KB |
Output is correct |
68 |
Correct |
483 ms |
35460 KB |
Output is correct |
69 |
Correct |
498 ms |
35460 KB |
Output is correct |
70 |
Correct |
473 ms |
35584 KB |
Output is correct |
71 |
Correct |
491 ms |
35512 KB |
Output is correct |
72 |
Correct |
504 ms |
35592 KB |
Output is correct |
73 |
Correct |
515 ms |
34948 KB |
Output is correct |
74 |
Correct |
514 ms |
35460 KB |
Output is correct |
75 |
Correct |
547 ms |
35664 KB |
Output is correct |
76 |
Correct |
812 ms |
34700 KB |
Output is correct |
77 |
Correct |
753 ms |
33852 KB |
Output is correct |
78 |
Correct |
818 ms |
36544 KB |
Output is correct |
79 |
Correct |
870 ms |
35364 KB |
Output is correct |
80 |
Correct |
864 ms |
39748 KB |
Output is correct |
81 |
Correct |
852 ms |
37568 KB |
Output is correct |
82 |
Correct |
849 ms |
37688 KB |
Output is correct |
83 |
Correct |
923 ms |
35640 KB |
Output is correct |
84 |
Correct |
942 ms |
39352 KB |
Output is correct |
85 |
Correct |
924 ms |
38328 KB |
Output is correct |
86 |
Correct |
875 ms |
35336 KB |
Output is correct |
87 |
Correct |
896 ms |
36468 KB |
Output is correct |
88 |
Correct |
789 ms |
36920 KB |
Output is correct |
89 |
Correct |
760 ms |
34512 KB |
Output is correct |
90 |
Correct |
751 ms |
34500 KB |
Output is correct |
91 |
Correct |
739 ms |
35944 KB |
Output is correct |
92 |
Correct |
726 ms |
35068 KB |
Output is correct |
93 |
Correct |
738 ms |
34876 KB |
Output is correct |
94 |
Correct |
786 ms |
34616 KB |
Output is correct |
95 |
Correct |
803 ms |
35384 KB |
Output is correct |
96 |
Correct |
861 ms |
34744 KB |
Output is correct |
97 |
Correct |
812 ms |
35768 KB |
Output is correct |