// Grzegorz Ryn
// V LO Kraków
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define st first
#define nd second
#define FOR(__VAR, __START, __END) for(int __VAR=__START; __VAR<=__END; __VAR++)
#define FORB(__VAR, __START, __END) for(int __VAR=__START; __VAR>=__END; __VAR--)
#define FORK(__VAR, __START, __END, __DIFF) for(int __VAR=__START; __VAR<=END; __VAR+=__DIFF)
#define all(__VAR) (__VAR).begin(), (__VAR).end()
#define rall(__VAR) (__VAR).rbegin(), (__VAR).rend()
#define DEBUG(__VAR) cout << #__VAR << ": " << __VAR << endl;
template<typename __T1, typename __T2>
ostream & operator<<(ostream &out, pair<__T1, __T2> &__VAR) {
cout << "[" << __VAR.st << ", " << __VAR.nd << "]";
return out;
}
template<typename __T>
ostream & operator<<(ostream &out, vector<__T> &__VAR) {
cout << "[";
FOR(i, 0, (int)__VAR.size()-2)
cout << __VAR[i] << ", ";
if(__VAR.size()>0)
cout << __VAR[__VAR.size()-1];
cout << "]" << endl;
return out;
}
const int INF=1e9;
int N, K, n = 1<<20;
vector<vector<int>> g, inv_g, tree;
vector<int> pre_order, c, siz, heavy, depth, head, parent;
vector<bool> vis;
void prep_hld(int v) {
vis[v] = true;
for(int u:tree[v]) {
if(vis[u]) {
continue;
}
depth[u] = depth[v]+1;
parent[u] = v;
prep_hld(u);
siz[v] += siz[u];
if(heavy[v] == -1 || siz[heavy[v]] < siz[u]) {
heavy[v] = u;
}
}
}
void get_ind(int v, int h) {
static int next_it = 0;
vis[v] = true;
pre_order[v] = next_it++;
head[v] = h;
if(heavy[v] != -1) {
get_ind(heavy[v], h);
}
for(int u:tree[v]) {
if(vis[u]) {
continue;
}
get_ind(u, u);
}
}
void get_input() {
cin >> N >> K;
tree.resize(N);
c.resize(N);
pre_order.resize(N);
parent.resize(N);
for(int i=0; i<N-1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
tree[a].pb(b);
tree[b].pb(a);
}
for(int i=0; i<N; i++) {
cin >> c[i];
c[i]--;
}
vis.resize(N, false);
siz.resize(N, 1);
heavy.resize(N, -1);
head.resize(N, 0);
depth.resize(N, 0);
prep_hld(0);
fill(all(vis), false);
get_ind(0, 0);
g.resize(2*n + K);
for(int i=n-1; i>0; i--) {
g[i].pb(2*i);
g[i].pb(2*i+1);
}
for(int i=0; i<N; i++) {
g[pre_order[i] + n].pb(2*n + c[i]);
g[2*n + c[i]].pb(pre_order[i] + n);
}
}
void add(int l, int r, int v, int p, int q, int u) {
if(r<p || q<l) {
return;
}
if(p<=l && r<=q) {
g[u+n].pb(v);
return;
}
int mid = (l+r)/2;
add(l, mid, 2*v, p, q, u);
add(mid+1, r, 2*v+1, p, q, u);
}
int lca(int v, int u) {
while(head[u] != head[v]) {
if(depth[head[u]] < depth[head[v]]) {
swap(u, v);
}
u = parent[head[u]];
}
if(depth[u] > depth[v]) {
swap(u, v);
}
return u;
}
void update(int u, int v, int source) {
//cout << u << " -> " << v << endl;
while(head[u] != head[v]) {
//cout << pre_order[source] << " => [" << pre_order[head[u]] << ' ' << pre_order[u] << "]" << endl;
add(0, n-1, 1, pre_order[head[u]], pre_order[u], pre_order[source]);
u = parent[head[u]];
}
//cout << pre_order[source] << " => [" << pre_order[v] << ' ' << pre_order[u] << "]" << endl;
add(0, n-1, 1, pre_order[v], pre_order[u], pre_order[source]);
}
stack<int> s;
void dfs1(int v) {
vis[v] = true;
for(int u:g[v]) {
if(vis[u]) {
continue;
}
dfs1(u);
}
s.push(v);
}
vector<int> comp;
int next_comp = 0;
void dfs2(int v) {
vis[v] = true;
comp[v] = next_comp;
for(int u:inv_g[v]) {
if(vis[u]) {
continue;
}
dfs2(u);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
get_input();
vector<int> color_lca(K);
for(int i=0; i<N; i++) {
color_lca[c[i]] = i;
}
for(int i=0; i<N; i++) {
color_lca[c[i]] = lca(color_lca[c[i]], i);
}
for(int i=0; i<N; i++) {
update(i, color_lca[c[i]], i);
}
inv_g.resize(g.size());
for(int i=0; i<g.size(); i++) {
for(int u:g[i]) {
inv_g[u].pb(i);
}
}
vis.resize(g.size());
fill(all(vis), false);
for(int i=0; i<g.size(); i++) {
if(vis[i]) {
continue;
}
dfs1(i);
}
fill(all(vis), false);
comp.resize(g.size());
while(!s.empty()) {
int v=s.top();
s.pop();
if(vis[v]) {
continue;
}
dfs2(v);
next_comp++;
}
vector<int> out(next_comp, 0), siz(next_comp, 0);
for(int i=0; i<g.size(); i++) {
for(int j:g[i]) {
if(comp[i] != comp[j]) {
out[comp[i]]++;
}
}
}
for(int i=2*n; i<g.size(); i++) {
siz[comp[i]]++;
}
int result = INF;
for(int i=0; i<N; i++) {
int cmp = comp[pre_order[i]+n];
if(out[cmp] > 0) {
continue;
}
result = min(result, siz[cmp]-1);
}
cout << result << '\n';
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:212:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
212 | for(int i=0; i<g.size(); i++) {
| ~^~~~~~~~~
capital_city.cpp:220:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
220 | for(int i=0; i<g.size(); i++) {
| ~^~~~~~~~~
capital_city.cpp:240:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
240 | for(int i=0; i<g.size(); i++) {
| ~^~~~~~~~~
capital_city.cpp:247:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
247 | for(int i=2*n; i<g.size(); i++) {
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
270 ms |
222572 KB |
Output is correct |
2 |
Correct |
289 ms |
222572 KB |
Output is correct |
3 |
Correct |
268 ms |
222572 KB |
Output is correct |
4 |
Correct |
272 ms |
222548 KB |
Output is correct |
5 |
Correct |
277 ms |
222564 KB |
Output is correct |
6 |
Correct |
271 ms |
222544 KB |
Output is correct |
7 |
Correct |
273 ms |
222528 KB |
Output is correct |
8 |
Correct |
279 ms |
222472 KB |
Output is correct |
9 |
Correct |
279 ms |
222544 KB |
Output is correct |
10 |
Correct |
280 ms |
222576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
270 ms |
222572 KB |
Output is correct |
2 |
Correct |
289 ms |
222572 KB |
Output is correct |
3 |
Correct |
268 ms |
222572 KB |
Output is correct |
4 |
Correct |
272 ms |
222548 KB |
Output is correct |
5 |
Correct |
277 ms |
222564 KB |
Output is correct |
6 |
Correct |
271 ms |
222544 KB |
Output is correct |
7 |
Correct |
273 ms |
222528 KB |
Output is correct |
8 |
Correct |
279 ms |
222472 KB |
Output is correct |
9 |
Correct |
279 ms |
222544 KB |
Output is correct |
10 |
Correct |
280 ms |
222576 KB |
Output is correct |
11 |
Correct |
277 ms |
222984 KB |
Output is correct |
12 |
Correct |
273 ms |
222948 KB |
Output is correct |
13 |
Correct |
281 ms |
222940 KB |
Output is correct |
14 |
Correct |
273 ms |
222952 KB |
Output is correct |
15 |
Correct |
288 ms |
223136 KB |
Output is correct |
16 |
Correct |
281 ms |
223008 KB |
Output is correct |
17 |
Correct |
276 ms |
222960 KB |
Output is correct |
18 |
Correct |
289 ms |
223204 KB |
Output is correct |
19 |
Correct |
271 ms |
222992 KB |
Output is correct |
20 |
Correct |
275 ms |
223148 KB |
Output is correct |
21 |
Correct |
278 ms |
223160 KB |
Output is correct |
22 |
Correct |
328 ms |
223204 KB |
Output is correct |
23 |
Correct |
282 ms |
223212 KB |
Output is correct |
24 |
Correct |
315 ms |
222916 KB |
Output is correct |
25 |
Correct |
278 ms |
223052 KB |
Output is correct |
26 |
Correct |
320 ms |
223036 KB |
Output is correct |
27 |
Correct |
272 ms |
222956 KB |
Output is correct |
28 |
Correct |
272 ms |
223060 KB |
Output is correct |
29 |
Correct |
283 ms |
223004 KB |
Output is correct |
30 |
Correct |
272 ms |
222980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
810 ms |
290636 KB |
Output is correct |
2 |
Correct |
523 ms |
294676 KB |
Output is correct |
3 |
Correct |
821 ms |
293944 KB |
Output is correct |
4 |
Correct |
509 ms |
294628 KB |
Output is correct |
5 |
Correct |
861 ms |
292760 KB |
Output is correct |
6 |
Correct |
547 ms |
294460 KB |
Output is correct |
7 |
Correct |
811 ms |
290904 KB |
Output is correct |
8 |
Correct |
514 ms |
291648 KB |
Output is correct |
9 |
Correct |
799 ms |
278208 KB |
Output is correct |
10 |
Correct |
787 ms |
275016 KB |
Output is correct |
11 |
Correct |
793 ms |
279476 KB |
Output is correct |
12 |
Correct |
779 ms |
282048 KB |
Output is correct |
13 |
Correct |
797 ms |
276776 KB |
Output is correct |
14 |
Correct |
776 ms |
278468 KB |
Output is correct |
15 |
Correct |
769 ms |
279308 KB |
Output is correct |
16 |
Correct |
796 ms |
279156 KB |
Output is correct |
17 |
Correct |
875 ms |
280124 KB |
Output is correct |
18 |
Correct |
794 ms |
275388 KB |
Output is correct |
19 |
Correct |
791 ms |
281124 KB |
Output is correct |
20 |
Correct |
784 ms |
282680 KB |
Output is correct |
21 |
Correct |
303 ms |
222580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
270 ms |
222572 KB |
Output is correct |
2 |
Correct |
289 ms |
222572 KB |
Output is correct |
3 |
Correct |
268 ms |
222572 KB |
Output is correct |
4 |
Correct |
272 ms |
222548 KB |
Output is correct |
5 |
Correct |
277 ms |
222564 KB |
Output is correct |
6 |
Correct |
271 ms |
222544 KB |
Output is correct |
7 |
Correct |
273 ms |
222528 KB |
Output is correct |
8 |
Correct |
279 ms |
222472 KB |
Output is correct |
9 |
Correct |
279 ms |
222544 KB |
Output is correct |
10 |
Correct |
280 ms |
222576 KB |
Output is correct |
11 |
Correct |
277 ms |
222984 KB |
Output is correct |
12 |
Correct |
273 ms |
222948 KB |
Output is correct |
13 |
Correct |
281 ms |
222940 KB |
Output is correct |
14 |
Correct |
273 ms |
222952 KB |
Output is correct |
15 |
Correct |
288 ms |
223136 KB |
Output is correct |
16 |
Correct |
281 ms |
223008 KB |
Output is correct |
17 |
Correct |
276 ms |
222960 KB |
Output is correct |
18 |
Correct |
289 ms |
223204 KB |
Output is correct |
19 |
Correct |
271 ms |
222992 KB |
Output is correct |
20 |
Correct |
275 ms |
223148 KB |
Output is correct |
21 |
Correct |
278 ms |
223160 KB |
Output is correct |
22 |
Correct |
328 ms |
223204 KB |
Output is correct |
23 |
Correct |
282 ms |
223212 KB |
Output is correct |
24 |
Correct |
315 ms |
222916 KB |
Output is correct |
25 |
Correct |
278 ms |
223052 KB |
Output is correct |
26 |
Correct |
320 ms |
223036 KB |
Output is correct |
27 |
Correct |
272 ms |
222956 KB |
Output is correct |
28 |
Correct |
272 ms |
223060 KB |
Output is correct |
29 |
Correct |
283 ms |
223004 KB |
Output is correct |
30 |
Correct |
272 ms |
222980 KB |
Output is correct |
31 |
Correct |
810 ms |
290636 KB |
Output is correct |
32 |
Correct |
523 ms |
294676 KB |
Output is correct |
33 |
Correct |
821 ms |
293944 KB |
Output is correct |
34 |
Correct |
509 ms |
294628 KB |
Output is correct |
35 |
Correct |
861 ms |
292760 KB |
Output is correct |
36 |
Correct |
547 ms |
294460 KB |
Output is correct |
37 |
Correct |
811 ms |
290904 KB |
Output is correct |
38 |
Correct |
514 ms |
291648 KB |
Output is correct |
39 |
Correct |
799 ms |
278208 KB |
Output is correct |
40 |
Correct |
787 ms |
275016 KB |
Output is correct |
41 |
Correct |
793 ms |
279476 KB |
Output is correct |
42 |
Correct |
779 ms |
282048 KB |
Output is correct |
43 |
Correct |
797 ms |
276776 KB |
Output is correct |
44 |
Correct |
776 ms |
278468 KB |
Output is correct |
45 |
Correct |
769 ms |
279308 KB |
Output is correct |
46 |
Correct |
796 ms |
279156 KB |
Output is correct |
47 |
Correct |
875 ms |
280124 KB |
Output is correct |
48 |
Correct |
794 ms |
275388 KB |
Output is correct |
49 |
Correct |
791 ms |
281124 KB |
Output is correct |
50 |
Correct |
784 ms |
282680 KB |
Output is correct |
51 |
Correct |
303 ms |
222580 KB |
Output is correct |
52 |
Correct |
1079 ms |
269180 KB |
Output is correct |
53 |
Correct |
993 ms |
270364 KB |
Output is correct |
54 |
Correct |
1004 ms |
270328 KB |
Output is correct |
55 |
Correct |
987 ms |
270080 KB |
Output is correct |
56 |
Correct |
954 ms |
269696 KB |
Output is correct |
57 |
Correct |
961 ms |
269920 KB |
Output is correct |
58 |
Correct |
814 ms |
273292 KB |
Output is correct |
59 |
Correct |
812 ms |
273228 KB |
Output is correct |
60 |
Correct |
923 ms |
280344 KB |
Output is correct |
61 |
Correct |
980 ms |
281860 KB |
Output is correct |
62 |
Correct |
513 ms |
294588 KB |
Output is correct |
63 |
Correct |
521 ms |
294588 KB |
Output is correct |
64 |
Correct |
528 ms |
293196 KB |
Output is correct |
65 |
Correct |
560 ms |
294384 KB |
Output is correct |
66 |
Correct |
790 ms |
296728 KB |
Output is correct |
67 |
Correct |
686 ms |
275712 KB |
Output is correct |
68 |
Correct |
807 ms |
296752 KB |
Output is correct |
69 |
Correct |
784 ms |
296636 KB |
Output is correct |
70 |
Correct |
790 ms |
296488 KB |
Output is correct |
71 |
Correct |
808 ms |
296760 KB |
Output is correct |
72 |
Correct |
809 ms |
296552 KB |
Output is correct |
73 |
Correct |
713 ms |
276076 KB |
Output is correct |
74 |
Correct |
815 ms |
296664 KB |
Output is correct |
75 |
Correct |
774 ms |
296552 KB |
Output is correct |
76 |
Correct |
678 ms |
268324 KB |
Output is correct |
77 |
Correct |
662 ms |
264268 KB |
Output is correct |
78 |
Correct |
801 ms |
276420 KB |
Output is correct |
79 |
Correct |
777 ms |
273424 KB |
Output is correct |
80 |
Correct |
792 ms |
278956 KB |
Output is correct |
81 |
Correct |
781 ms |
278084 KB |
Output is correct |
82 |
Correct |
784 ms |
277396 KB |
Output is correct |
83 |
Correct |
805 ms |
275448 KB |
Output is correct |
84 |
Correct |
784 ms |
279892 KB |
Output is correct |
85 |
Correct |
781 ms |
279340 KB |
Output is correct |
86 |
Correct |
825 ms |
274524 KB |
Output is correct |
87 |
Correct |
809 ms |
278448 KB |
Output is correct |
88 |
Correct |
841 ms |
277220 KB |
Output is correct |
89 |
Correct |
817 ms |
277780 KB |
Output is correct |
90 |
Correct |
855 ms |
275908 KB |
Output is correct |
91 |
Correct |
828 ms |
277196 KB |
Output is correct |
92 |
Correct |
821 ms |
277692 KB |
Output is correct |
93 |
Correct |
838 ms |
277656 KB |
Output is correct |
94 |
Correct |
839 ms |
275492 KB |
Output is correct |
95 |
Correct |
857 ms |
275924 KB |
Output is correct |
96 |
Correct |
843 ms |
277152 KB |
Output is correct |
97 |
Correct |
837 ms |
282260 KB |
Output is correct |