#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, LOG = 19;
int n, k;
vector<int> tree[N];
vector<int> g[N], rg[N];
int a[N];
vector<int> pos[N];
int up[N][LOG];
int tin[N], tout[N], timer;
int lca[N];
bool mark[N];
bool outdeg[N];
int sz[N];
void dfs(int u, int par) {
tin[u] = timer++;
for(int v : tree[u]) {
if(v == par) continue;
up[v][0] = u;
dfs(v, u);
}
tout[u] = timer++;
}
bool is_ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
int LCA(int u, int v) {
if(is_ancestor(u, v)) return u;
if(is_ancestor(v, u)) return v;
for(int j = LOG - 1; j >= 0; j--) {
if(up[u][j] == 0) continue;
if(is_ancestor(up[u][j], v)) continue;
u = up[u][j];
}
return up[u][0];
}
vector<int> vec;
void dfsrg(int u) {
if(mark[u]) return;
mark[u] = true;
for(int v : rg[u]) {
if(mark[v]) continue;
dfsrg(v);
}
vec.push_back(u);
}
int scc[N];
int id = 0;
void dfsg(int u) {
if(mark[u]) return;
mark[u] = true;
scc[u] = id;
for(int v : g[u]) {
if(mark[v]) continue;
dfsg(v);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
for(int i = 1; i <= n; i++) {
cin >> a[i];
pos[a[i]].push_back(i);
}
dfs(1, 0);
for(int j = 1; j < LOG; j++) for(int i = 1; i <= n; i++) up[i][j] = up[up[i][j - 1]][j - 1];
for(int i = 1; i <= k; i++) {
lca[i] = pos[i][0];
for(int j = 1; j < pos[i].size(); j++) lca[i] = LCA(lca[i], pos[i][j]);
}
for(int i = 2; i <= n; i++) {
if(i == lca[a[i]]) continue;
if(a[up[i][0]] != a[i]) g[a[i]].push_back(a[up[i][0]]);
}
for(int i = 1; i <= k; i++) {
sort(g[i].begin(), g[i].end());
g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
for(int j : g[i]) rg[j].push_back(i);
}
for(int i = 1; i <= k; i++) {
if(mark[i]) continue;
dfsrg(i);
}
reverse(vec.begin(), vec.end());
for(int i = 1; i <= k; i++) mark[i] = false;
for(int i : vec) {
if(mark[i]) continue;
id++;
dfsg(i);
}
for(int i = 1; i <= k; i++) {
for(int j : g[i]) if(scc[i] != scc[j]) outdeg[scc[i]] = true;
sz[scc[i]]++;
}
int ans = k - 1;
for(int i = 1; i <= k; i++) if(!outdeg[scc[i]]) ans = min(ans, sz[scc[i]] - 1);
cout << ans;
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:82:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int j = 1; j < pos[i].size(); j++) lca[i] = LCA(lca[i], pos[i][j]);
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
24668 KB |
Output is correct |
2 |
Correct |
6 ms |
24540 KB |
Output is correct |
3 |
Correct |
6 ms |
24668 KB |
Output is correct |
4 |
Correct |
5 ms |
24668 KB |
Output is correct |
5 |
Correct |
5 ms |
24668 KB |
Output is correct |
6 |
Correct |
5 ms |
24668 KB |
Output is correct |
7 |
Correct |
5 ms |
24668 KB |
Output is correct |
8 |
Correct |
5 ms |
24664 KB |
Output is correct |
9 |
Correct |
5 ms |
24680 KB |
Output is correct |
10 |
Correct |
5 ms |
22616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
24668 KB |
Output is correct |
2 |
Correct |
6 ms |
24540 KB |
Output is correct |
3 |
Correct |
6 ms |
24668 KB |
Output is correct |
4 |
Correct |
5 ms |
24668 KB |
Output is correct |
5 |
Correct |
5 ms |
24668 KB |
Output is correct |
6 |
Correct |
5 ms |
24668 KB |
Output is correct |
7 |
Correct |
5 ms |
24668 KB |
Output is correct |
8 |
Correct |
5 ms |
24664 KB |
Output is correct |
9 |
Correct |
5 ms |
24680 KB |
Output is correct |
10 |
Correct |
5 ms |
22616 KB |
Output is correct |
11 |
Correct |
6 ms |
24872 KB |
Output is correct |
12 |
Correct |
6 ms |
24628 KB |
Output is correct |
13 |
Correct |
6 ms |
24612 KB |
Output is correct |
14 |
Correct |
6 ms |
24664 KB |
Output is correct |
15 |
Correct |
6 ms |
24664 KB |
Output is correct |
16 |
Correct |
6 ms |
24668 KB |
Output is correct |
17 |
Correct |
8 ms |
24876 KB |
Output is correct |
18 |
Correct |
6 ms |
24668 KB |
Output is correct |
19 |
Correct |
6 ms |
24676 KB |
Output is correct |
20 |
Correct |
7 ms |
24668 KB |
Output is correct |
21 |
Correct |
6 ms |
24668 KB |
Output is correct |
22 |
Correct |
6 ms |
24940 KB |
Output is correct |
23 |
Correct |
6 ms |
24924 KB |
Output is correct |
24 |
Correct |
6 ms |
24924 KB |
Output is correct |
25 |
Correct |
6 ms |
24924 KB |
Output is correct |
26 |
Correct |
6 ms |
24920 KB |
Output is correct |
27 |
Correct |
6 ms |
24924 KB |
Output is correct |
28 |
Correct |
6 ms |
24668 KB |
Output is correct |
29 |
Correct |
6 ms |
24668 KB |
Output is correct |
30 |
Correct |
6 ms |
24668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
70680 KB |
Output is correct |
2 |
Correct |
91 ms |
70972 KB |
Output is correct |
3 |
Correct |
156 ms |
70348 KB |
Output is correct |
4 |
Correct |
89 ms |
71120 KB |
Output is correct |
5 |
Correct |
183 ms |
67464 KB |
Output is correct |
6 |
Correct |
92 ms |
70808 KB |
Output is correct |
7 |
Correct |
148 ms |
67356 KB |
Output is correct |
8 |
Correct |
88 ms |
69452 KB |
Output is correct |
9 |
Correct |
133 ms |
63688 KB |
Output is correct |
10 |
Correct |
147 ms |
61776 KB |
Output is correct |
11 |
Correct |
138 ms |
63952 KB |
Output is correct |
12 |
Correct |
130 ms |
66260 KB |
Output is correct |
13 |
Correct |
152 ms |
61380 KB |
Output is correct |
14 |
Correct |
148 ms |
66496 KB |
Output is correct |
15 |
Correct |
136 ms |
66252 KB |
Output is correct |
16 |
Correct |
132 ms |
62112 KB |
Output is correct |
17 |
Correct |
157 ms |
62732 KB |
Output is correct |
18 |
Correct |
135 ms |
62780 KB |
Output is correct |
19 |
Correct |
139 ms |
65368 KB |
Output is correct |
20 |
Correct |
164 ms |
67128 KB |
Output is correct |
21 |
Correct |
5 ms |
22872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
24668 KB |
Output is correct |
2 |
Correct |
6 ms |
24540 KB |
Output is correct |
3 |
Correct |
6 ms |
24668 KB |
Output is correct |
4 |
Correct |
5 ms |
24668 KB |
Output is correct |
5 |
Correct |
5 ms |
24668 KB |
Output is correct |
6 |
Correct |
5 ms |
24668 KB |
Output is correct |
7 |
Correct |
5 ms |
24668 KB |
Output is correct |
8 |
Correct |
5 ms |
24664 KB |
Output is correct |
9 |
Correct |
5 ms |
24680 KB |
Output is correct |
10 |
Correct |
5 ms |
22616 KB |
Output is correct |
11 |
Correct |
6 ms |
24872 KB |
Output is correct |
12 |
Correct |
6 ms |
24628 KB |
Output is correct |
13 |
Correct |
6 ms |
24612 KB |
Output is correct |
14 |
Correct |
6 ms |
24664 KB |
Output is correct |
15 |
Correct |
6 ms |
24664 KB |
Output is correct |
16 |
Correct |
6 ms |
24668 KB |
Output is correct |
17 |
Correct |
8 ms |
24876 KB |
Output is correct |
18 |
Correct |
6 ms |
24668 KB |
Output is correct |
19 |
Correct |
6 ms |
24676 KB |
Output is correct |
20 |
Correct |
7 ms |
24668 KB |
Output is correct |
21 |
Correct |
6 ms |
24668 KB |
Output is correct |
22 |
Correct |
6 ms |
24940 KB |
Output is correct |
23 |
Correct |
6 ms |
24924 KB |
Output is correct |
24 |
Correct |
6 ms |
24924 KB |
Output is correct |
25 |
Correct |
6 ms |
24924 KB |
Output is correct |
26 |
Correct |
6 ms |
24920 KB |
Output is correct |
27 |
Correct |
6 ms |
24924 KB |
Output is correct |
28 |
Correct |
6 ms |
24668 KB |
Output is correct |
29 |
Correct |
6 ms |
24668 KB |
Output is correct |
30 |
Correct |
6 ms |
24668 KB |
Output is correct |
31 |
Correct |
156 ms |
70680 KB |
Output is correct |
32 |
Correct |
91 ms |
70972 KB |
Output is correct |
33 |
Correct |
156 ms |
70348 KB |
Output is correct |
34 |
Correct |
89 ms |
71120 KB |
Output is correct |
35 |
Correct |
183 ms |
67464 KB |
Output is correct |
36 |
Correct |
92 ms |
70808 KB |
Output is correct |
37 |
Correct |
148 ms |
67356 KB |
Output is correct |
38 |
Correct |
88 ms |
69452 KB |
Output is correct |
39 |
Correct |
133 ms |
63688 KB |
Output is correct |
40 |
Correct |
147 ms |
61776 KB |
Output is correct |
41 |
Correct |
138 ms |
63952 KB |
Output is correct |
42 |
Correct |
130 ms |
66260 KB |
Output is correct |
43 |
Correct |
152 ms |
61380 KB |
Output is correct |
44 |
Correct |
148 ms |
66496 KB |
Output is correct |
45 |
Correct |
136 ms |
66252 KB |
Output is correct |
46 |
Correct |
132 ms |
62112 KB |
Output is correct |
47 |
Correct |
157 ms |
62732 KB |
Output is correct |
48 |
Correct |
135 ms |
62780 KB |
Output is correct |
49 |
Correct |
139 ms |
65368 KB |
Output is correct |
50 |
Correct |
164 ms |
67128 KB |
Output is correct |
51 |
Correct |
5 ms |
22872 KB |
Output is correct |
52 |
Correct |
126 ms |
53836 KB |
Output is correct |
53 |
Correct |
126 ms |
53700 KB |
Output is correct |
54 |
Correct |
147 ms |
53588 KB |
Output is correct |
55 |
Correct |
127 ms |
53748 KB |
Output is correct |
56 |
Correct |
130 ms |
53836 KB |
Output is correct |
57 |
Correct |
148 ms |
53768 KB |
Output is correct |
58 |
Correct |
144 ms |
58064 KB |
Output is correct |
59 |
Correct |
143 ms |
58612 KB |
Output is correct |
60 |
Correct |
166 ms |
57804 KB |
Output is correct |
61 |
Correct |
167 ms |
57552 KB |
Output is correct |
62 |
Correct |
97 ms |
71116 KB |
Output is correct |
63 |
Correct |
103 ms |
71232 KB |
Output is correct |
64 |
Correct |
101 ms |
69868 KB |
Output is correct |
65 |
Correct |
92 ms |
70864 KB |
Output is correct |
66 |
Correct |
116 ms |
59592 KB |
Output is correct |
67 |
Correct |
123 ms |
59416 KB |
Output is correct |
68 |
Correct |
115 ms |
59592 KB |
Output is correct |
69 |
Correct |
124 ms |
59512 KB |
Output is correct |
70 |
Correct |
124 ms |
59376 KB |
Output is correct |
71 |
Correct |
119 ms |
59340 KB |
Output is correct |
72 |
Correct |
115 ms |
59336 KB |
Output is correct |
73 |
Correct |
133 ms |
58756 KB |
Output is correct |
74 |
Correct |
124 ms |
59496 KB |
Output is correct |
75 |
Correct |
120 ms |
59336 KB |
Output is correct |
76 |
Correct |
140 ms |
63176 KB |
Output is correct |
77 |
Correct |
169 ms |
61648 KB |
Output is correct |
78 |
Correct |
150 ms |
62840 KB |
Output is correct |
79 |
Correct |
160 ms |
60916 KB |
Output is correct |
80 |
Correct |
141 ms |
66508 KB |
Output is correct |
81 |
Correct |
146 ms |
63984 KB |
Output is correct |
82 |
Correct |
136 ms |
63956 KB |
Output is correct |
83 |
Correct |
176 ms |
61264 KB |
Output is correct |
84 |
Correct |
171 ms |
66076 KB |
Output is correct |
85 |
Correct |
138 ms |
64720 KB |
Output is correct |
86 |
Correct |
137 ms |
60876 KB |
Output is correct |
87 |
Correct |
137 ms |
62216 KB |
Output is correct |
88 |
Correct |
133 ms |
62796 KB |
Output is correct |
89 |
Correct |
132 ms |
59360 KB |
Output is correct |
90 |
Correct |
134 ms |
59088 KB |
Output is correct |
91 |
Correct |
135 ms |
61184 KB |
Output is correct |
92 |
Correct |
133 ms |
60400 KB |
Output is correct |
93 |
Correct |
134 ms |
59852 KB |
Output is correct |
94 |
Correct |
165 ms |
59400 KB |
Output is correct |
95 |
Correct |
140 ms |
60488 KB |
Output is correct |
96 |
Correct |
138 ms |
59604 KB |
Output is correct |
97 |
Correct |
151 ms |
61148 KB |
Output is correct |