#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, int p) {
P[n]=p;
for (auto &i:adj[n])
if (i!=p&&!vis[i]) d3(i, n);
}
void d4(int n, int p) {
cnt[C[n]]=0;
for (auto &i:adj[n])
if (i!=p&&!vis[i]) d4(i, n);
}
void sol(int st) {
d1(st, 0);
int c=d2(st, 0, sz[st]/2+1);
d3(c, 0);
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, 0);
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 sol(int)':
capital_city.cpp:44:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i=0; i<s.size(); i++) {
| ~^~~~~~~~~
capital_city.cpp:48:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
48 | if (ele[C[x]].size()!=cnt[C[x]]) { fl=1; break; }
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
6 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
6 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
6 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
6 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
11 ms |
9984 KB |
Output is correct |
12 |
Correct |
9 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9984 KB |
Output is correct |
14 |
Correct |
11 ms |
9984 KB |
Output is correct |
15 |
Correct |
10 ms |
9984 KB |
Output is correct |
16 |
Correct |
9 ms |
9856 KB |
Output is correct |
17 |
Correct |
8 ms |
9984 KB |
Output is correct |
18 |
Correct |
8 ms |
10112 KB |
Output is correct |
19 |
Correct |
8 ms |
9984 KB |
Output is correct |
20 |
Correct |
8 ms |
10112 KB |
Output is correct |
21 |
Correct |
8 ms |
9984 KB |
Output is correct |
22 |
Correct |
8 ms |
9984 KB |
Output is correct |
23 |
Correct |
8 ms |
9984 KB |
Output is correct |
24 |
Correct |
9 ms |
9984 KB |
Output is correct |
25 |
Correct |
9 ms |
9984 KB |
Output is correct |
26 |
Correct |
9 ms |
9984 KB |
Output is correct |
27 |
Correct |
9 ms |
9984 KB |
Output is correct |
28 |
Correct |
9 ms |
9984 KB |
Output is correct |
29 |
Correct |
9 ms |
9984 KB |
Output is correct |
30 |
Correct |
9 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
694 ms |
33056 KB |
Output is correct |
2 |
Correct |
229 ms |
33148 KB |
Output is correct |
3 |
Correct |
681 ms |
32760 KB |
Output is correct |
4 |
Correct |
233 ms |
33144 KB |
Output is correct |
5 |
Correct |
674 ms |
31096 KB |
Output is correct |
6 |
Correct |
239 ms |
33272 KB |
Output is correct |
7 |
Correct |
662 ms |
31476 KB |
Output is correct |
8 |
Correct |
268 ms |
33392 KB |
Output is correct |
9 |
Correct |
909 ms |
31016 KB |
Output is correct |
10 |
Correct |
910 ms |
29576 KB |
Output is correct |
11 |
Correct |
872 ms |
32236 KB |
Output is correct |
12 |
Correct |
845 ms |
33516 KB |
Output is correct |
13 |
Correct |
843 ms |
29168 KB |
Output is correct |
14 |
Correct |
857 ms |
33008 KB |
Output is correct |
15 |
Correct |
841 ms |
32808 KB |
Output is correct |
16 |
Correct |
831 ms |
29864 KB |
Output is correct |
17 |
Correct |
855 ms |
30312 KB |
Output is correct |
18 |
Correct |
831 ms |
30952 KB |
Output is correct |
19 |
Correct |
842 ms |
32268 KB |
Output is correct |
20 |
Correct |
833 ms |
34216 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
6 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
6 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
11 ms |
9984 KB |
Output is correct |
12 |
Correct |
9 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9984 KB |
Output is correct |
14 |
Correct |
11 ms |
9984 KB |
Output is correct |
15 |
Correct |
10 ms |
9984 KB |
Output is correct |
16 |
Correct |
9 ms |
9856 KB |
Output is correct |
17 |
Correct |
8 ms |
9984 KB |
Output is correct |
18 |
Correct |
8 ms |
10112 KB |
Output is correct |
19 |
Correct |
8 ms |
9984 KB |
Output is correct |
20 |
Correct |
8 ms |
10112 KB |
Output is correct |
21 |
Correct |
8 ms |
9984 KB |
Output is correct |
22 |
Correct |
8 ms |
9984 KB |
Output is correct |
23 |
Correct |
8 ms |
9984 KB |
Output is correct |
24 |
Correct |
9 ms |
9984 KB |
Output is correct |
25 |
Correct |
9 ms |
9984 KB |
Output is correct |
26 |
Correct |
9 ms |
9984 KB |
Output is correct |
27 |
Correct |
9 ms |
9984 KB |
Output is correct |
28 |
Correct |
9 ms |
9984 KB |
Output is correct |
29 |
Correct |
9 ms |
9984 KB |
Output is correct |
30 |
Correct |
9 ms |
9984 KB |
Output is correct |
31 |
Correct |
694 ms |
33056 KB |
Output is correct |
32 |
Correct |
229 ms |
33148 KB |
Output is correct |
33 |
Correct |
681 ms |
32760 KB |
Output is correct |
34 |
Correct |
233 ms |
33144 KB |
Output is correct |
35 |
Correct |
674 ms |
31096 KB |
Output is correct |
36 |
Correct |
239 ms |
33272 KB |
Output is correct |
37 |
Correct |
662 ms |
31476 KB |
Output is correct |
38 |
Correct |
268 ms |
33392 KB |
Output is correct |
39 |
Correct |
909 ms |
31016 KB |
Output is correct |
40 |
Correct |
910 ms |
29576 KB |
Output is correct |
41 |
Correct |
872 ms |
32236 KB |
Output is correct |
42 |
Correct |
845 ms |
33516 KB |
Output is correct |
43 |
Correct |
843 ms |
29168 KB |
Output is correct |
44 |
Correct |
857 ms |
33008 KB |
Output is correct |
45 |
Correct |
841 ms |
32808 KB |
Output is correct |
46 |
Correct |
831 ms |
29864 KB |
Output is correct |
47 |
Correct |
855 ms |
30312 KB |
Output is correct |
48 |
Correct |
831 ms |
30952 KB |
Output is correct |
49 |
Correct |
842 ms |
32268 KB |
Output is correct |
50 |
Correct |
833 ms |
34216 KB |
Output is correct |
51 |
Correct |
7 ms |
9728 KB |
Output is correct |
52 |
Correct |
597 ms |
23528 KB |
Output is correct |
53 |
Correct |
705 ms |
23524 KB |
Output is correct |
54 |
Correct |
658 ms |
23652 KB |
Output is correct |
55 |
Correct |
715 ms |
23656 KB |
Output is correct |
56 |
Correct |
639 ms |
23484 KB |
Output is correct |
57 |
Correct |
629 ms |
23580 KB |
Output is correct |
58 |
Correct |
590 ms |
25612 KB |
Output is correct |
59 |
Correct |
555 ms |
25116 KB |
Output is correct |
60 |
Correct |
673 ms |
25180 KB |
Output is correct |
61 |
Correct |
747 ms |
25332 KB |
Output is correct |
62 |
Correct |
226 ms |
33272 KB |
Output is correct |
63 |
Correct |
228 ms |
33144 KB |
Output is correct |
64 |
Correct |
227 ms |
33212 KB |
Output is correct |
65 |
Correct |
239 ms |
33272 KB |
Output is correct |
66 |
Correct |
426 ms |
28332 KB |
Output is correct |
67 |
Correct |
488 ms |
28332 KB |
Output is correct |
68 |
Correct |
479 ms |
28332 KB |
Output is correct |
69 |
Correct |
541 ms |
28332 KB |
Output is correct |
70 |
Correct |
500 ms |
28292 KB |
Output is correct |
71 |
Correct |
478 ms |
28460 KB |
Output is correct |
72 |
Correct |
431 ms |
28332 KB |
Output is correct |
73 |
Correct |
451 ms |
27856 KB |
Output is correct |
74 |
Correct |
455 ms |
28332 KB |
Output is correct |
75 |
Correct |
485 ms |
28332 KB |
Output is correct |
76 |
Correct |
750 ms |
28484 KB |
Output is correct |
77 |
Correct |
709 ms |
27380 KB |
Output is correct |
78 |
Correct |
877 ms |
30832 KB |
Output is correct |
79 |
Correct |
834 ms |
29676 KB |
Output is correct |
80 |
Correct |
891 ms |
34028 KB |
Output is correct |
81 |
Correct |
922 ms |
31168 KB |
Output is correct |
82 |
Correct |
898 ms |
31208 KB |
Output is correct |
83 |
Correct |
812 ms |
29352 KB |
Output is correct |
84 |
Correct |
856 ms |
32852 KB |
Output is correct |
85 |
Correct |
834 ms |
32364 KB |
Output is correct |
86 |
Correct |
876 ms |
29548 KB |
Output is correct |
87 |
Correct |
829 ms |
30064 KB |
Output is correct |
88 |
Correct |
698 ms |
30320 KB |
Output is correct |
89 |
Correct |
690 ms |
27888 KB |
Output is correct |
90 |
Correct |
729 ms |
28012 KB |
Output is correct |
91 |
Correct |
784 ms |
29348 KB |
Output is correct |
92 |
Correct |
762 ms |
28720 KB |
Output is correct |
93 |
Correct |
722 ms |
28272 KB |
Output is correct |
94 |
Correct |
697 ms |
27884 KB |
Output is correct |
95 |
Correct |
740 ms |
28776 KB |
Output is correct |
96 |
Correct |
736 ms |
28300 KB |
Output is correct |
97 |
Correct |
704 ms |
30056 KB |
Output is correct |