#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=2e5, MX=1e6;
int n, k, c[mxN], n2;
int d[mxN], s[mxN], p[mxN], hd[mxN], tin[mxN], tin2[mxN];
int who[mxN], cnt[mxN];
vector<int> oc[mxN], adj1[mxN], adj2[MX], adj3[MX], st, nodes;
bool vis[MX], out[MX];
void dfs1(int u=0) {
s[u]=1;
for (int& v : adj1[u]) {
adj1[v].erase(find(adj1[v].begin(), adj1[v].end(), u));
d[v]=d[u]+1, p[v]=u;
dfs1(v);
s[u]+=s[v];
if (s[v]>s[adj1[u][0]])
swap(v, adj1[u][0]);
}
}
void dfs2(int u=0, int h=0) {
static int timer=0;
hd[u]=h, tin[u]=timer++, tin2[tin[u]]=u;
if (adj1[u].empty())
return;
dfs2(adj1[u][0], h);
for (int i=1; i<adj1[u].size(); ++i)
dfs2(adj1[u][i], adj1[u][i]);
}
int lca(int u, int v) {
for (; hd[u]!=hd[v]; v=p[hd[v]])
if (d[hd[u]]>d[hd[v]])
swap(u, v);
return d[u]<d[v]?u:v;
}
void ae(int u, int v) {
adj2[u].push_back(v);
adj3[v].push_back(u);
}
void bld(int u=1, int l=0, int r=n-1) {
if (l==r) {
ae(u+k, c[tin2[l]]);
n2=max(n2, u+k+1);
return;
}
int mid=(l+r)/2;
ae(u+k, 2*u+k);
ae(u+k, 2*u+1+k);
bld(2*u, l, mid);
bld(2*u+1, mid+1, r);
}
void qry(int ql, int qr, int u=1, int l=0, int r=n-1) {
if (l>qr||r<ql)
return;
if (ql<=l&&r<=qr) {
nodes.push_back(u+k);
return;
}
int mid=(l+r)/2;
qry(ql, qr, 2*u, l, mid);
qry(ql, qr, 2*u+1, mid+1, r);
}
vector<int> gt(int u, int v) {
assert(hd[u]==hd[v]&&tin[u]<=tin[v]);
nodes.clear();
qry(tin[u], tin[v]);
return nodes;
}
void dfs3(int u) {
vis[u]=1;
for (int v : adj2[u])
if (!vis[v])
dfs3(v);
st.push_back(u);
}
void dfs4(int u, int rt) {
vis[u]=0, who[u]=rt;
cnt[rt]+=u<k;
for (int v : adj3[u])
if (vis[v])
dfs4(v, rt);
}
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();
dfs2();
bld();
for (int i=0; i<n; ++i) {
ae(i+n2, c[i]);
if (i!=hd[i])
ae(i+n2, p[i]+n2);
}
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 u : oc[i]) {
for (; hd[u]!=hd[l]; u=p[hd[u]])
ae(i, n2+u);
for (int j : gt(l, u))
ae(i, j);
}
}
n2+=n;
for (int i=0; i<n2; ++i)
if (!vis[i])
dfs3(i);
reverse(st.begin(), st.end());
for (int i : st)
if (vis[i])
dfs4(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 'void dfs2(int, int)':
capital_city.cpp:32:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i=1; i<adj1[u].size(); ++i)
| ~^~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:120:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int j=1; j<oc[i].size(); ++j)
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
56648 KB |
Output is correct |
2 |
Correct |
31 ms |
56660 KB |
Output is correct |
3 |
Correct |
28 ms |
56656 KB |
Output is correct |
4 |
Correct |
29 ms |
56736 KB |
Output is correct |
5 |
Correct |
28 ms |
56688 KB |
Output is correct |
6 |
Correct |
27 ms |
56708 KB |
Output is correct |
7 |
Correct |
34 ms |
56716 KB |
Output is correct |
8 |
Correct |
47 ms |
56652 KB |
Output is correct |
9 |
Correct |
29 ms |
56628 KB |
Output is correct |
10 |
Correct |
34 ms |
56696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
56648 KB |
Output is correct |
2 |
Correct |
31 ms |
56660 KB |
Output is correct |
3 |
Correct |
28 ms |
56656 KB |
Output is correct |
4 |
Correct |
29 ms |
56736 KB |
Output is correct |
5 |
Correct |
28 ms |
56688 KB |
Output is correct |
6 |
Correct |
27 ms |
56708 KB |
Output is correct |
7 |
Correct |
34 ms |
56716 KB |
Output is correct |
8 |
Correct |
47 ms |
56652 KB |
Output is correct |
9 |
Correct |
29 ms |
56628 KB |
Output is correct |
10 |
Correct |
34 ms |
56696 KB |
Output is correct |
11 |
Correct |
31 ms |
57508 KB |
Output is correct |
12 |
Correct |
38 ms |
57420 KB |
Output is correct |
13 |
Correct |
57 ms |
57492 KB |
Output is correct |
14 |
Correct |
33 ms |
57464 KB |
Output is correct |
15 |
Correct |
34 ms |
57452 KB |
Output is correct |
16 |
Correct |
31 ms |
57472 KB |
Output is correct |
17 |
Correct |
36 ms |
57564 KB |
Output is correct |
18 |
Correct |
29 ms |
57656 KB |
Output is correct |
19 |
Correct |
35 ms |
57588 KB |
Output is correct |
20 |
Correct |
30 ms |
57556 KB |
Output is correct |
21 |
Correct |
35 ms |
57676 KB |
Output is correct |
22 |
Correct |
30 ms |
57736 KB |
Output is correct |
23 |
Correct |
31 ms |
57520 KB |
Output is correct |
24 |
Correct |
37 ms |
57464 KB |
Output is correct |
25 |
Correct |
32 ms |
57556 KB |
Output is correct |
26 |
Correct |
31 ms |
57644 KB |
Output is correct |
27 |
Correct |
32 ms |
57600 KB |
Output is correct |
28 |
Correct |
33 ms |
57596 KB |
Output is correct |
29 |
Correct |
30 ms |
57556 KB |
Output is correct |
30 |
Correct |
37 ms |
57464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
962 ms |
158472 KB |
Output is correct |
2 |
Correct |
474 ms |
159484 KB |
Output is correct |
3 |
Correct |
879 ms |
157752 KB |
Output is correct |
4 |
Correct |
475 ms |
159428 KB |
Output is correct |
5 |
Correct |
855 ms |
150900 KB |
Output is correct |
6 |
Correct |
470 ms |
159568 KB |
Output is correct |
7 |
Correct |
878 ms |
151740 KB |
Output is correct |
8 |
Correct |
499 ms |
157232 KB |
Output is correct |
9 |
Correct |
713 ms |
143204 KB |
Output is correct |
10 |
Correct |
768 ms |
139724 KB |
Output is correct |
11 |
Correct |
744 ms |
142788 KB |
Output is correct |
12 |
Correct |
728 ms |
148988 KB |
Output is correct |
13 |
Correct |
724 ms |
137396 KB |
Output is correct |
14 |
Correct |
785 ms |
148948 KB |
Output is correct |
15 |
Correct |
743 ms |
148028 KB |
Output is correct |
16 |
Correct |
736 ms |
142192 KB |
Output is correct |
17 |
Correct |
768 ms |
143804 KB |
Output is correct |
18 |
Correct |
741 ms |
140384 KB |
Output is correct |
19 |
Correct |
776 ms |
146976 KB |
Output is correct |
20 |
Correct |
735 ms |
151288 KB |
Output is correct |
21 |
Correct |
32 ms |
56628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
56648 KB |
Output is correct |
2 |
Correct |
31 ms |
56660 KB |
Output is correct |
3 |
Correct |
28 ms |
56656 KB |
Output is correct |
4 |
Correct |
29 ms |
56736 KB |
Output is correct |
5 |
Correct |
28 ms |
56688 KB |
Output is correct |
6 |
Correct |
27 ms |
56708 KB |
Output is correct |
7 |
Correct |
34 ms |
56716 KB |
Output is correct |
8 |
Correct |
47 ms |
56652 KB |
Output is correct |
9 |
Correct |
29 ms |
56628 KB |
Output is correct |
10 |
Correct |
34 ms |
56696 KB |
Output is correct |
11 |
Correct |
31 ms |
57508 KB |
Output is correct |
12 |
Correct |
38 ms |
57420 KB |
Output is correct |
13 |
Correct |
57 ms |
57492 KB |
Output is correct |
14 |
Correct |
33 ms |
57464 KB |
Output is correct |
15 |
Correct |
34 ms |
57452 KB |
Output is correct |
16 |
Correct |
31 ms |
57472 KB |
Output is correct |
17 |
Correct |
36 ms |
57564 KB |
Output is correct |
18 |
Correct |
29 ms |
57656 KB |
Output is correct |
19 |
Correct |
35 ms |
57588 KB |
Output is correct |
20 |
Correct |
30 ms |
57556 KB |
Output is correct |
21 |
Correct |
35 ms |
57676 KB |
Output is correct |
22 |
Correct |
30 ms |
57736 KB |
Output is correct |
23 |
Correct |
31 ms |
57520 KB |
Output is correct |
24 |
Correct |
37 ms |
57464 KB |
Output is correct |
25 |
Correct |
32 ms |
57556 KB |
Output is correct |
26 |
Correct |
31 ms |
57644 KB |
Output is correct |
27 |
Correct |
32 ms |
57600 KB |
Output is correct |
28 |
Correct |
33 ms |
57596 KB |
Output is correct |
29 |
Correct |
30 ms |
57556 KB |
Output is correct |
30 |
Correct |
37 ms |
57464 KB |
Output is correct |
31 |
Correct |
962 ms |
158472 KB |
Output is correct |
32 |
Correct |
474 ms |
159484 KB |
Output is correct |
33 |
Correct |
879 ms |
157752 KB |
Output is correct |
34 |
Correct |
475 ms |
159428 KB |
Output is correct |
35 |
Correct |
855 ms |
150900 KB |
Output is correct |
36 |
Correct |
470 ms |
159568 KB |
Output is correct |
37 |
Correct |
878 ms |
151740 KB |
Output is correct |
38 |
Correct |
499 ms |
157232 KB |
Output is correct |
39 |
Correct |
713 ms |
143204 KB |
Output is correct |
40 |
Correct |
768 ms |
139724 KB |
Output is correct |
41 |
Correct |
744 ms |
142788 KB |
Output is correct |
42 |
Correct |
728 ms |
148988 KB |
Output is correct |
43 |
Correct |
724 ms |
137396 KB |
Output is correct |
44 |
Correct |
785 ms |
148948 KB |
Output is correct |
45 |
Correct |
743 ms |
148028 KB |
Output is correct |
46 |
Correct |
736 ms |
142192 KB |
Output is correct |
47 |
Correct |
768 ms |
143804 KB |
Output is correct |
48 |
Correct |
741 ms |
140384 KB |
Output is correct |
49 |
Correct |
776 ms |
146976 KB |
Output is correct |
50 |
Correct |
735 ms |
151288 KB |
Output is correct |
51 |
Correct |
32 ms |
56628 KB |
Output is correct |
52 |
Correct |
712 ms |
132768 KB |
Output is correct |
53 |
Correct |
710 ms |
133596 KB |
Output is correct |
54 |
Correct |
633 ms |
133652 KB |
Output is correct |
55 |
Correct |
650 ms |
133264 KB |
Output is correct |
56 |
Correct |
653 ms |
133604 KB |
Output is correct |
57 |
Correct |
651 ms |
133680 KB |
Output is correct |
58 |
Correct |
789 ms |
132208 KB |
Output is correct |
59 |
Incorrect |
722 ms |
133380 KB |
Output isn't correct |
60 |
Halted |
0 ms |
0 KB |
- |