// author : thembululquaUwU
// 3.9.2024
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int LG = 20;
void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}
struct LCA{
vi t_in, t_out, _lg, high;
vector <vi> euler, adj;
int cnt = 0, n, root;
#define rz resize
void rz(int _n, int _root){
n = _n, root = _root;
t_in.rz(n + 1), t_out.rz(n + 1);
euler.rz(3 * n + 1), _lg.rz(3 * n + 1);
adj.rz(n + 3), high.rz(n + 3, 1);
for (int i = 1; i <= 3 * n; ++ i) _lg[i] = log2(i);
}
void add(int u, int v){adj[u].pb(v); adj[v].pb(u);}
void dfs(int u, int p = 0){
t_in[u] = ++ cnt; euler[cnt].pb(u);
for (int v : adj[u]) if(v ^ p) {
high[v] = high[u] + 1;
dfs(v, u); euler[++ cnt].pb(u);
}
t_out[u] = cnt;
}
int check(int u, int v) {return t_in[u] <= t_in[v] && t_out[v] <= t_out[u];}
int min_by_time(int u, int v) {return (t_in[u] < t_in[v] ? u : v);}
void init(){
dfs(root);
for (int lg = 1; lg < LG; ++lg) {
for (int i = 1; i + (1 << lg) - 1 <= cnt; ++i)
euler[i].pb(min_by_time(euler[i][lg - 1], euler[i + (1 << lg - 1)][lg - 1]));
}
}
int get(int u, int v) {
int L = min(t_in[u], t_in[v]);
int R = max(t_out[u], t_out[v]);
int lg = _lg[R - L + 1];
return min_by_time(euler[L][lg], euler[R - (1 << lg) + 1][lg]);
}
int dist(int u, int v){
int lc = get(u, v);
return high[u] + high[v] - 2 * high[lc];
}
} pqh;
int n, k, last[N], par[N];
vi badj[N], nadj[N * LG];
int to[N][LG], idx[N][LG], nvex;
void dfs(int u = 1, int p = 0){
par[u] = p;
to[u][0] = u, idx[u][0] = ++ nvex;
//cout << u << ' ' << 0 << ' ' << nvex << endl;
for (int j = 1; j < LG; ++ j){
int nxt = par[to[u][j - 1]];
if (!nxt || !idx[nxt][j - 1]) break;
to[u][j] = to[nxt][j - 1], idx[u][j] = ++ nvex;
//cout << u << ' ' << j << ' ' << nvex << endl;
nadj[nvex].pb(idx[u][j - 1]); nadj[nvex].pb(idx[nxt][j - 1]);
}
for (int v : badj[u]) if (v ^ p) dfs(v, u);
}
void add(int c, int u, int p){
for (int l = LG - 1; l >= 0; -- l){
int t = to[u][l]; if (!t || (par[p] && pqh.check(t, par[p]))) continue;
//if (c == 2) cout << idx[u][l] << endl;
nadj[c].pb(idx[u][l]); u = par[t];
}
}
int num[N * LG], low[N * LG], ti = 0, sz[N * LG], ncom = 0, lab[N * LG];
vector <int> stk;
void vis(int u){
num[u] = low[u] = ++ ti;
stk.pb(u);
for (int v : nadj[u]){
if (num[v]) minl(low[u], num[v]);
else vis(v), minl(low[u], low[v]);
}
if (low[u] == num[u]){
++ ncom; int v;
do {
v = stk.back(); stk.pop_back();
low[v] = num[v] = 1e9;
sz[ncom] += (v <= k);
lab[v] = ncom;
} while (v ^ u);
}
}
void solve(){
cin >> n >> k; pqh.rz(n, 1); nvex = k;
for (int i = 1, u, v; i < n; ++ i){
cin >> u >> v;
badj[u].pb(v); badj[v].pb(u);
pqh.add(u, v);
}
pqh.init(); dfs();
for (int i = 1; i <= n; ++ i){
int c; cin >> c;
nadj[idx[i][0]].pb(c);
if (last[c]) {
int p = pqh.get(i, last[c]);;
add(c, i, p); add(c, last[c], p);
}
last[c] = i;
}
for (int i = 1; i <= nvex; ++ i) if (!num[i]) vis(i);
int ans = k; //cout << lab[1] << ' ' << lab[2] << ' ' << lab[3] << endl;
vector <bool> ck(ncom + 1, true);
for (int i = 1; i <= nvex; ++ i){
for (int j : nadj[i]) {
//cout << i << ' ' << j << endl;
if (lab[i] ^ lab[j]) ck[lab[i]] = false;
}
}
for (int i = 1; i <= ncom; ++ i) if (ck[i]) {
minl(ans, sz[i]); //cout << i << ' ' << sz[i] << endl;
}
cout << ans - 1;
}
int main(){
if (fopen("pqh.inp", "r")){
freopen("pqh.inp", "r", stdin);
freopen("pqh.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; // cin >> t;
while (t --) solve();
return 0;
}
Compilation message
capital_city.cpp:19:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void maxl(auto &a, auto b) {a = max(a, b);}
| ^~~~
capital_city.cpp:19:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void maxl(auto &a, auto b) {a = max(a, b);}
| ^~~~
capital_city.cpp:20:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void minl(auto &a, auto b) {a = min(a, b);}
| ^~~~
capital_city.cpp:20:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void minl(auto &a, auto b) {a = min(a, b);}
| ^~~~
capital_city.cpp: In member function 'void LCA::init()':
capital_city.cpp:56:78: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
56 | euler[i].pb(min_by_time(euler[i][lg - 1], euler[i + (1 << lg - 1)][lg - 1]));
| ~~~^~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:157:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
157 | freopen("pqh.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
158 | freopen("pqh.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
99136 KB |
Output is correct |
2 |
Correct |
47 ms |
99140 KB |
Output is correct |
3 |
Correct |
40 ms |
98908 KB |
Output is correct |
4 |
Correct |
44 ms |
98896 KB |
Output is correct |
5 |
Correct |
40 ms |
98896 KB |
Output is correct |
6 |
Correct |
40 ms |
98900 KB |
Output is correct |
7 |
Correct |
40 ms |
99048 KB |
Output is correct |
8 |
Correct |
39 ms |
99116 KB |
Output is correct |
9 |
Correct |
39 ms |
98908 KB |
Output is correct |
10 |
Correct |
39 ms |
98908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
99136 KB |
Output is correct |
2 |
Correct |
47 ms |
99140 KB |
Output is correct |
3 |
Correct |
40 ms |
98908 KB |
Output is correct |
4 |
Correct |
44 ms |
98896 KB |
Output is correct |
5 |
Correct |
40 ms |
98896 KB |
Output is correct |
6 |
Correct |
40 ms |
98900 KB |
Output is correct |
7 |
Correct |
40 ms |
99048 KB |
Output is correct |
8 |
Correct |
39 ms |
99116 KB |
Output is correct |
9 |
Correct |
39 ms |
98908 KB |
Output is correct |
10 |
Correct |
39 ms |
98908 KB |
Output is correct |
11 |
Correct |
43 ms |
100496 KB |
Output is correct |
12 |
Correct |
45 ms |
100440 KB |
Output is correct |
13 |
Correct |
42 ms |
100432 KB |
Output is correct |
14 |
Correct |
42 ms |
100440 KB |
Output is correct |
15 |
Correct |
43 ms |
100944 KB |
Output is correct |
16 |
Correct |
44 ms |
100692 KB |
Output is correct |
17 |
Correct |
43 ms |
100884 KB |
Output is correct |
18 |
Correct |
48 ms |
100944 KB |
Output is correct |
19 |
Correct |
43 ms |
100948 KB |
Output is correct |
20 |
Correct |
44 ms |
100948 KB |
Output is correct |
21 |
Correct |
43 ms |
100968 KB |
Output is correct |
22 |
Correct |
44 ms |
101152 KB |
Output is correct |
23 |
Correct |
51 ms |
101200 KB |
Output is correct |
24 |
Correct |
52 ms |
100948 KB |
Output is correct |
25 |
Correct |
50 ms |
101200 KB |
Output is correct |
26 |
Correct |
49 ms |
101240 KB |
Output is correct |
27 |
Correct |
50 ms |
101200 KB |
Output is correct |
28 |
Correct |
50 ms |
100944 KB |
Output is correct |
29 |
Correct |
48 ms |
101000 KB |
Output is correct |
30 |
Correct |
53 ms |
100944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1442 ms |
437692 KB |
Output is correct |
2 |
Correct |
781 ms |
396628 KB |
Output is correct |
3 |
Correct |
1212 ms |
404940 KB |
Output is correct |
4 |
Correct |
765 ms |
396624 KB |
Output is correct |
5 |
Correct |
1252 ms |
427832 KB |
Output is correct |
6 |
Correct |
822 ms |
396628 KB |
Output is correct |
7 |
Correct |
1248 ms |
417856 KB |
Output is correct |
8 |
Correct |
745 ms |
396624 KB |
Output is correct |
9 |
Correct |
1242 ms |
384484 KB |
Output is correct |
10 |
Correct |
1351 ms |
380816 KB |
Output is correct |
11 |
Correct |
1346 ms |
385396 KB |
Output is correct |
12 |
Correct |
1380 ms |
391896 KB |
Output is correct |
13 |
Correct |
1345 ms |
380272 KB |
Output is correct |
14 |
Correct |
1293 ms |
392608 KB |
Output is correct |
15 |
Correct |
1285 ms |
391872 KB |
Output is correct |
16 |
Correct |
1325 ms |
381120 KB |
Output is correct |
17 |
Correct |
1326 ms |
382232 KB |
Output is correct |
18 |
Correct |
1291 ms |
381828 KB |
Output is correct |
19 |
Correct |
1266 ms |
389232 KB |
Output is correct |
20 |
Correct |
1284 ms |
395572 KB |
Output is correct |
21 |
Correct |
43 ms |
98876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
99136 KB |
Output is correct |
2 |
Correct |
47 ms |
99140 KB |
Output is correct |
3 |
Correct |
40 ms |
98908 KB |
Output is correct |
4 |
Correct |
44 ms |
98896 KB |
Output is correct |
5 |
Correct |
40 ms |
98896 KB |
Output is correct |
6 |
Correct |
40 ms |
98900 KB |
Output is correct |
7 |
Correct |
40 ms |
99048 KB |
Output is correct |
8 |
Correct |
39 ms |
99116 KB |
Output is correct |
9 |
Correct |
39 ms |
98908 KB |
Output is correct |
10 |
Correct |
39 ms |
98908 KB |
Output is correct |
11 |
Correct |
43 ms |
100496 KB |
Output is correct |
12 |
Correct |
45 ms |
100440 KB |
Output is correct |
13 |
Correct |
42 ms |
100432 KB |
Output is correct |
14 |
Correct |
42 ms |
100440 KB |
Output is correct |
15 |
Correct |
43 ms |
100944 KB |
Output is correct |
16 |
Correct |
44 ms |
100692 KB |
Output is correct |
17 |
Correct |
43 ms |
100884 KB |
Output is correct |
18 |
Correct |
48 ms |
100944 KB |
Output is correct |
19 |
Correct |
43 ms |
100948 KB |
Output is correct |
20 |
Correct |
44 ms |
100948 KB |
Output is correct |
21 |
Correct |
43 ms |
100968 KB |
Output is correct |
22 |
Correct |
44 ms |
101152 KB |
Output is correct |
23 |
Correct |
51 ms |
101200 KB |
Output is correct |
24 |
Correct |
52 ms |
100948 KB |
Output is correct |
25 |
Correct |
50 ms |
101200 KB |
Output is correct |
26 |
Correct |
49 ms |
101240 KB |
Output is correct |
27 |
Correct |
50 ms |
101200 KB |
Output is correct |
28 |
Correct |
50 ms |
100944 KB |
Output is correct |
29 |
Correct |
48 ms |
101000 KB |
Output is correct |
30 |
Correct |
53 ms |
100944 KB |
Output is correct |
31 |
Correct |
1442 ms |
437692 KB |
Output is correct |
32 |
Correct |
781 ms |
396628 KB |
Output is correct |
33 |
Correct |
1212 ms |
404940 KB |
Output is correct |
34 |
Correct |
765 ms |
396624 KB |
Output is correct |
35 |
Correct |
1252 ms |
427832 KB |
Output is correct |
36 |
Correct |
822 ms |
396628 KB |
Output is correct |
37 |
Correct |
1248 ms |
417856 KB |
Output is correct |
38 |
Correct |
745 ms |
396624 KB |
Output is correct |
39 |
Correct |
1242 ms |
384484 KB |
Output is correct |
40 |
Correct |
1351 ms |
380816 KB |
Output is correct |
41 |
Correct |
1346 ms |
385396 KB |
Output is correct |
42 |
Correct |
1380 ms |
391896 KB |
Output is correct |
43 |
Correct |
1345 ms |
380272 KB |
Output is correct |
44 |
Correct |
1293 ms |
392608 KB |
Output is correct |
45 |
Correct |
1285 ms |
391872 KB |
Output is correct |
46 |
Correct |
1325 ms |
381120 KB |
Output is correct |
47 |
Correct |
1326 ms |
382232 KB |
Output is correct |
48 |
Correct |
1291 ms |
381828 KB |
Output is correct |
49 |
Correct |
1266 ms |
389232 KB |
Output is correct |
50 |
Correct |
1284 ms |
395572 KB |
Output is correct |
51 |
Correct |
43 ms |
98876 KB |
Output is correct |
52 |
Correct |
705 ms |
271900 KB |
Output is correct |
53 |
Correct |
711 ms |
273320 KB |
Output is correct |
54 |
Correct |
709 ms |
273336 KB |
Output is correct |
55 |
Correct |
684 ms |
273952 KB |
Output is correct |
56 |
Correct |
638 ms |
272552 KB |
Output is correct |
57 |
Correct |
626 ms |
273340 KB |
Output is correct |
58 |
Correct |
1076 ms |
369156 KB |
Output is correct |
59 |
Correct |
1036 ms |
371832 KB |
Output is correct |
60 |
Correct |
1091 ms |
355028 KB |
Output is correct |
61 |
Correct |
1128 ms |
355092 KB |
Output is correct |
62 |
Correct |
819 ms |
396504 KB |
Output is correct |
63 |
Correct |
773 ms |
396624 KB |
Output is correct |
64 |
Correct |
767 ms |
396748 KB |
Output is correct |
65 |
Correct |
781 ms |
396628 KB |
Output is correct |
66 |
Correct |
1084 ms |
336580 KB |
Output is correct |
67 |
Correct |
1103 ms |
367740 KB |
Output is correct |
68 |
Correct |
1016 ms |
336756 KB |
Output is correct |
69 |
Correct |
1034 ms |
372076 KB |
Output is correct |
70 |
Correct |
1028 ms |
371940 KB |
Output is correct |
71 |
Correct |
931 ms |
336544 KB |
Output is correct |
72 |
Correct |
1063 ms |
336552 KB |
Output is correct |
73 |
Correct |
1145 ms |
382732 KB |
Output is correct |
74 |
Correct |
1012 ms |
336552 KB |
Output is correct |
75 |
Correct |
1051 ms |
371984 KB |
Output is correct |
76 |
Correct |
935 ms |
376848 KB |
Output is correct |
77 |
Correct |
873 ms |
367052 KB |
Output is correct |
78 |
Correct |
1308 ms |
381796 KB |
Output is correct |
79 |
Correct |
1368 ms |
379232 KB |
Output is correct |
80 |
Correct |
1344 ms |
394212 KB |
Output is correct |
81 |
Correct |
1254 ms |
385680 KB |
Output is correct |
82 |
Correct |
1277 ms |
385220 KB |
Output is correct |
83 |
Correct |
1383 ms |
379932 KB |
Output is correct |
84 |
Correct |
1430 ms |
391524 KB |
Output is correct |
85 |
Correct |
1333 ms |
387544 KB |
Output is correct |
86 |
Correct |
1283 ms |
379072 KB |
Output is correct |
87 |
Correct |
1426 ms |
381556 KB |
Output is correct |
88 |
Correct |
1453 ms |
385128 KB |
Output is correct |
89 |
Correct |
1227 ms |
375616 KB |
Output is correct |
90 |
Correct |
1158 ms |
375916 KB |
Output is correct |
91 |
Correct |
1223 ms |
379596 KB |
Output is correct |
92 |
Correct |
1252 ms |
376600 KB |
Output is correct |
93 |
Correct |
1206 ms |
376596 KB |
Output is correct |
94 |
Correct |
1244 ms |
375196 KB |
Output is correct |
95 |
Correct |
1154 ms |
379044 KB |
Output is correct |
96 |
Correct |
1174 ms |
375900 KB |
Output is correct |
97 |
Correct |
1234 ms |
379240 KB |
Output is correct |