#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=2e5;
int n, k, c[mxN], d[mxN], anc[mxN][18], tin[mxN], t, who[mxN], cnt[mxN];
vector<int> oc[mxN], adj1[mxN], adj2[mxN], adj3[mxN], st;
bool vis[mxN], out[mxN];
void dfs1(int u=0) {
tin[u]=t++;
for (int i=1; i<18; ++i)
anc[u][i]=anc[anc[u][i-1]][i-1];
for (int v : adj1[u])
if (v!=anc[u][0]) {
d[v]=d[u]+1, anc[v][0]=u;
dfs1(v);
}
}
int lca(int u, int v) {
if (d[u]>d[v])
swap(u, v);
for (int i=17; ~i; --i)
if (d[v]-(1<<i)>=d[u])
v=anc[v][i];
if (u==v)
return u;
for (int i=17; ~i; --i)
if (anc[u][i]!=anc[v][i])
u=anc[u][i], v=anc[v][i];
return anc[u][0];
}
void dfs2(int u) {
vis[u]=1;
for (int v : adj2[u])
if (!vis[v])
dfs2(v);
st.push_back(u);
}
void dfs3(int u, int rt) {
vis[u]=0, who[u]=rt;
++cnt[rt];
for (int v : adj3[u])
if (vis[v])
dfs3(v, rt);
}
void ae(int u, int v) {
adj2[u].push_back(v);
adj3[v].push_back(u);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i=1; i<n; ++i) {
int u, v;
cin >> u >> v, --u, --v;
adj1[u].push_back(v);
adj1[v].push_back(u);
}
for (int i=0; i<n; ++i) {
cin >> c[i], --c[i];
oc[c[i]].push_back(i);
}
dfs1();
for (int i=0; i<k; ++i) {
int l=oc[i][0];
for (int j=1; j<oc[i].size(); ++j)
l=lca(l, oc[i][j]);
for (int j : oc[i])
if (j!=l&&c[j]!=c[anc[j][0]])
ae(c[j], c[anc[j][0]]);
}
for (int i=0; i<k; ++i)
if (!vis[i])
dfs2(i);
reverse(st.begin(), st.end());
for (int i : st)
if (vis[i])
dfs3(i, i);
for (int i : st)
for (int j : adj2[i])
if (who[i]!=who[j])
out[who[i]]=1;
int ans=69696969;
for (int i=0; i<k; ++i)
if (!out[who[i]])
ans=min(ans, cnt[who[i]]-1);
cout << ans;
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:75:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int j=1; j<oc[i].size(); ++j)
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19124 KB |
Output is correct |
2 |
Correct |
10 ms |
19148 KB |
Output is correct |
3 |
Correct |
10 ms |
19028 KB |
Output is correct |
4 |
Correct |
10 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19092 KB |
Output is correct |
6 |
Correct |
10 ms |
19148 KB |
Output is correct |
7 |
Correct |
10 ms |
19052 KB |
Output is correct |
8 |
Correct |
11 ms |
19120 KB |
Output is correct |
9 |
Correct |
13 ms |
19156 KB |
Output is correct |
10 |
Correct |
11 ms |
19148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19124 KB |
Output is correct |
2 |
Correct |
10 ms |
19148 KB |
Output is correct |
3 |
Correct |
10 ms |
19028 KB |
Output is correct |
4 |
Correct |
10 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19092 KB |
Output is correct |
6 |
Correct |
10 ms |
19148 KB |
Output is correct |
7 |
Correct |
10 ms |
19052 KB |
Output is correct |
8 |
Correct |
11 ms |
19120 KB |
Output is correct |
9 |
Correct |
13 ms |
19156 KB |
Output is correct |
10 |
Correct |
11 ms |
19148 KB |
Output is correct |
11 |
Correct |
11 ms |
19444 KB |
Output is correct |
12 |
Correct |
11 ms |
19464 KB |
Output is correct |
13 |
Correct |
12 ms |
19412 KB |
Output is correct |
14 |
Correct |
11 ms |
19380 KB |
Output is correct |
15 |
Correct |
11 ms |
19528 KB |
Output is correct |
16 |
Correct |
12 ms |
19428 KB |
Output is correct |
17 |
Correct |
11 ms |
19412 KB |
Output is correct |
18 |
Correct |
11 ms |
19516 KB |
Output is correct |
19 |
Correct |
11 ms |
19524 KB |
Output is correct |
20 |
Correct |
13 ms |
19488 KB |
Output is correct |
21 |
Correct |
12 ms |
19412 KB |
Output is correct |
22 |
Correct |
12 ms |
19616 KB |
Output is correct |
23 |
Correct |
14 ms |
19540 KB |
Output is correct |
24 |
Correct |
12 ms |
19540 KB |
Output is correct |
25 |
Correct |
11 ms |
19540 KB |
Output is correct |
26 |
Correct |
11 ms |
19540 KB |
Output is correct |
27 |
Correct |
11 ms |
19516 KB |
Output is correct |
28 |
Correct |
11 ms |
19556 KB |
Output is correct |
29 |
Correct |
11 ms |
19412 KB |
Output is correct |
30 |
Correct |
12 ms |
19520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
68404 KB |
Output is correct |
2 |
Correct |
125 ms |
68808 KB |
Output is correct |
3 |
Correct |
323 ms |
68236 KB |
Output is correct |
4 |
Correct |
138 ms |
68896 KB |
Output is correct |
5 |
Correct |
355 ms |
65144 KB |
Output is correct |
6 |
Correct |
123 ms |
68772 KB |
Output is correct |
7 |
Correct |
355 ms |
64988 KB |
Output is correct |
8 |
Correct |
120 ms |
67148 KB |
Output is correct |
9 |
Correct |
267 ms |
61232 KB |
Output is correct |
10 |
Correct |
329 ms |
59228 KB |
Output is correct |
11 |
Correct |
267 ms |
61580 KB |
Output is correct |
12 |
Correct |
298 ms |
63648 KB |
Output is correct |
13 |
Correct |
266 ms |
58960 KB |
Output is correct |
14 |
Correct |
297 ms |
63988 KB |
Output is correct |
15 |
Correct |
281 ms |
63848 KB |
Output is correct |
16 |
Correct |
297 ms |
59684 KB |
Output is correct |
17 |
Correct |
271 ms |
60200 KB |
Output is correct |
18 |
Correct |
291 ms |
60360 KB |
Output is correct |
19 |
Correct |
292 ms |
62912 KB |
Output is correct |
20 |
Correct |
292 ms |
64552 KB |
Output is correct |
21 |
Correct |
12 ms |
19032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19124 KB |
Output is correct |
2 |
Correct |
10 ms |
19148 KB |
Output is correct |
3 |
Correct |
10 ms |
19028 KB |
Output is correct |
4 |
Correct |
10 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19092 KB |
Output is correct |
6 |
Correct |
10 ms |
19148 KB |
Output is correct |
7 |
Correct |
10 ms |
19052 KB |
Output is correct |
8 |
Correct |
11 ms |
19120 KB |
Output is correct |
9 |
Correct |
13 ms |
19156 KB |
Output is correct |
10 |
Correct |
11 ms |
19148 KB |
Output is correct |
11 |
Correct |
11 ms |
19444 KB |
Output is correct |
12 |
Correct |
11 ms |
19464 KB |
Output is correct |
13 |
Correct |
12 ms |
19412 KB |
Output is correct |
14 |
Correct |
11 ms |
19380 KB |
Output is correct |
15 |
Correct |
11 ms |
19528 KB |
Output is correct |
16 |
Correct |
12 ms |
19428 KB |
Output is correct |
17 |
Correct |
11 ms |
19412 KB |
Output is correct |
18 |
Correct |
11 ms |
19516 KB |
Output is correct |
19 |
Correct |
11 ms |
19524 KB |
Output is correct |
20 |
Correct |
13 ms |
19488 KB |
Output is correct |
21 |
Correct |
12 ms |
19412 KB |
Output is correct |
22 |
Correct |
12 ms |
19616 KB |
Output is correct |
23 |
Correct |
14 ms |
19540 KB |
Output is correct |
24 |
Correct |
12 ms |
19540 KB |
Output is correct |
25 |
Correct |
11 ms |
19540 KB |
Output is correct |
26 |
Correct |
11 ms |
19540 KB |
Output is correct |
27 |
Correct |
11 ms |
19516 KB |
Output is correct |
28 |
Correct |
11 ms |
19556 KB |
Output is correct |
29 |
Correct |
11 ms |
19412 KB |
Output is correct |
30 |
Correct |
12 ms |
19520 KB |
Output is correct |
31 |
Correct |
337 ms |
68404 KB |
Output is correct |
32 |
Correct |
125 ms |
68808 KB |
Output is correct |
33 |
Correct |
323 ms |
68236 KB |
Output is correct |
34 |
Correct |
138 ms |
68896 KB |
Output is correct |
35 |
Correct |
355 ms |
65144 KB |
Output is correct |
36 |
Correct |
123 ms |
68772 KB |
Output is correct |
37 |
Correct |
355 ms |
64988 KB |
Output is correct |
38 |
Correct |
120 ms |
67148 KB |
Output is correct |
39 |
Correct |
267 ms |
61232 KB |
Output is correct |
40 |
Correct |
329 ms |
59228 KB |
Output is correct |
41 |
Correct |
267 ms |
61580 KB |
Output is correct |
42 |
Correct |
298 ms |
63648 KB |
Output is correct |
43 |
Correct |
266 ms |
58960 KB |
Output is correct |
44 |
Correct |
297 ms |
63988 KB |
Output is correct |
45 |
Correct |
281 ms |
63848 KB |
Output is correct |
46 |
Correct |
297 ms |
59684 KB |
Output is correct |
47 |
Correct |
271 ms |
60200 KB |
Output is correct |
48 |
Correct |
291 ms |
60360 KB |
Output is correct |
49 |
Correct |
292 ms |
62912 KB |
Output is correct |
50 |
Correct |
292 ms |
64552 KB |
Output is correct |
51 |
Correct |
12 ms |
19032 KB |
Output is correct |
52 |
Correct |
243 ms |
50896 KB |
Output is correct |
53 |
Correct |
251 ms |
50840 KB |
Output is correct |
54 |
Correct |
245 ms |
50892 KB |
Output is correct |
55 |
Correct |
257 ms |
50908 KB |
Output is correct |
56 |
Correct |
245 ms |
50904 KB |
Output is correct |
57 |
Correct |
285 ms |
51036 KB |
Output is correct |
58 |
Correct |
262 ms |
55736 KB |
Output is correct |
59 |
Correct |
291 ms |
56232 KB |
Output is correct |
60 |
Correct |
294 ms |
55604 KB |
Output is correct |
61 |
Correct |
308 ms |
55484 KB |
Output is correct |
62 |
Correct |
133 ms |
68856 KB |
Output is correct |
63 |
Correct |
126 ms |
68864 KB |
Output is correct |
64 |
Correct |
129 ms |
67572 KB |
Output is correct |
65 |
Correct |
131 ms |
68668 KB |
Output is correct |
66 |
Correct |
211 ms |
56976 KB |
Output is correct |
67 |
Correct |
204 ms |
56884 KB |
Output is correct |
68 |
Correct |
233 ms |
57024 KB |
Output is correct |
69 |
Correct |
201 ms |
56984 KB |
Output is correct |
70 |
Correct |
225 ms |
56972 KB |
Output is correct |
71 |
Correct |
224 ms |
56900 KB |
Output is correct |
72 |
Correct |
207 ms |
56948 KB |
Output is correct |
73 |
Correct |
228 ms |
56228 KB |
Output is correct |
74 |
Correct |
212 ms |
57056 KB |
Output is correct |
75 |
Correct |
231 ms |
57056 KB |
Output is correct |
76 |
Correct |
248 ms |
60860 KB |
Output is correct |
77 |
Correct |
266 ms |
59320 KB |
Output is correct |
78 |
Correct |
280 ms |
60248 KB |
Output is correct |
79 |
Correct |
271 ms |
58524 KB |
Output is correct |
80 |
Correct |
335 ms |
64160 KB |
Output is correct |
81 |
Correct |
268 ms |
61392 KB |
Output is correct |
82 |
Correct |
305 ms |
61564 KB |
Output is correct |
83 |
Correct |
270 ms |
58764 KB |
Output is correct |
84 |
Correct |
286 ms |
63588 KB |
Output is correct |
85 |
Correct |
269 ms |
62288 KB |
Output is correct |
86 |
Correct |
270 ms |
58488 KB |
Output is correct |
87 |
Correct |
306 ms |
59880 KB |
Output is correct |
88 |
Correct |
288 ms |
60460 KB |
Output is correct |
89 |
Correct |
296 ms |
57112 KB |
Output is correct |
90 |
Correct |
258 ms |
57016 KB |
Output is correct |
91 |
Correct |
309 ms |
58824 KB |
Output is correct |
92 |
Correct |
292 ms |
58064 KB |
Output is correct |
93 |
Correct |
326 ms |
57496 KB |
Output is correct |
94 |
Correct |
262 ms |
57044 KB |
Output is correct |
95 |
Correct |
317 ms |
58248 KB |
Output is correct |
96 |
Correct |
266 ms |
57308 KB |
Output is correct |
97 |
Correct |
275 ms |
58884 KB |
Output is correct |