/*input
12 4
7 9
1 3
4 6
2 4
10 12
1 2
2 10
11 1
2 8
5 3
6 7
3 1 1 2 4 3 3 2 2 3 4 4
*/
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int MAXN = 200007, LOG = __lg(MAXN) + 1, MAXV = MAXN * LOG;
int N, lg, anc[LOG][MAXN], dep[MAXN], city[MAXN], K;
vector<int> towns[MAXN];
vector<int> tree[MAXN], G[MAXV];
int low[MAXV], vis[MAXV], col[MAXV], T = 0, ans = MAXN;
stack<int> st;
bool in_comp[MAXV];
void dfs(int u, int p)
{
for (int v : tree[u]) if (v != p) {
anc[0][v] = u;
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
int get_lca(int u, int v)
{
if (dep[u] < dep[v]) swap(u, v);
for (int j = lg - 1; j >= 0; --j) if (dep[u] - (1 << j) >= dep[v]) u = anc[j][u];
if (u == v) return u;
for (int j = lg - 1; j >= 0; --j) if (anc[j][u] != anc[j][v]) u = anc[j][u], v = anc[j][v];
return anc[0][u];
}
void dfs2(int u)
{
st.push(u);
low[u] = vis[u] = ++T; col[u] = 1;
for (int v : G[u]) {
if (col[v] == 2);
else if (col[v] == 1) {
low[u] = min(low[u], vis[v]);
} else {
// cout << (u - 1) / N << ' ' << (u - (u - 1) / N * N) << ": " << (v - 1) / N << ' ' << (v - (v - 1) / N * N) << endl;
dfs2(v);
low[u] = min(low[u], low[v]);
}
}
if (low[u] == vis[u]) {
int cost = 0;
vector<int> vec;
// cout << "comp: ";
for (int w = st.top();; w = st.top()) {
st.pop();
vec.push_back(w);
col[w] = 2;
// cout << '(' << (w - 1) / N << ' ' << (w - (w - 1) / N * N) << ") ";
if (w == u) break;
}
bool valid = true;
for (int w : vec) in_comp[w] = 1;
for (int w : vec) for (int v : G[w]) if (!in_comp[v]) { valid = false; break; }
for (int w : vec) in_comp[w] = 0;
if (valid) {
int cost = 0;
for (int w : vec) if (w <= N && !in_comp[city[w]]) {
cost += (in_comp[city[w]] = 1);
}
ans = min(ans, cost);
for (int w : vec) if (w <= N && in_comp[city[w]]) {
in_comp[city[w]] = 0;
}
}
// cout << valid << endl;
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
lg = __lg(N) + 1;
int V = lg * N;
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
dep[1] = 1, dfs(1, 0);
for (int j = 1; j < lg; ++j) for (int u = 1; u <= N; ++u) {
anc[j][u] = anc[j - 1][anc[j - 1][u]];
if (dep[u] >= (1 << j)) {
G[j * N + u].push_back((j - 1) * N + u);
G[j * N + u].push_back((j - 1) * N + anc[j - 1][u]);
}
}
for (int u = 1; u <= N; ++u) {
int c;
cin >> c;
towns[c].push_back(u);
city[u] = c;
}
for (int c = 1; c <= K; ++c) {
int lca = towns[c][0];
for (int v : towns[c]) lca = get_lca(lca, v);
lca = anc[0][lca];
for (int v : towns[c]) {
int w = v;
for (int j = lg - 1; j >= 0; --j) if (dep[w] - (1 << j) >= dep[lca]) {
G[v].push_back(j * N + w);
w = anc[j][w];
}
if (v != towns[c][0]) {
G[v].push_back(towns[c][0]);
G[towns[c][0]].push_back(v);
}
}
}
// cout << V << endl;
for (int j = 0; j < lg; ++j) for (int u = 1; u <= N; ++u) {
if (dep[u] >= (1 << j) && !col[j * N + u]) {
dfs2(j * N + u);
}
}
cout << ans - 1 << endl;
}
Compilation message
capital_city.cpp: In function 'void dfs2(int)':
capital_city.cpp:71:7: warning: unused variable 'cost' [-Wunused-variable]
int cost = 0;
^~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:106:6: warning: unused variable 'V' [-Wunused-variable]
int V = lg * N;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
94328 KB |
Output is correct |
2 |
Correct |
61 ms |
94328 KB |
Output is correct |
3 |
Correct |
62 ms |
94332 KB |
Output is correct |
4 |
Correct |
60 ms |
94328 KB |
Output is correct |
5 |
Correct |
60 ms |
94328 KB |
Output is correct |
6 |
Correct |
60 ms |
94328 KB |
Output is correct |
7 |
Correct |
60 ms |
94328 KB |
Output is correct |
8 |
Correct |
60 ms |
94328 KB |
Output is correct |
9 |
Correct |
59 ms |
94328 KB |
Output is correct |
10 |
Correct |
61 ms |
94328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
94328 KB |
Output is correct |
2 |
Correct |
61 ms |
94328 KB |
Output is correct |
3 |
Correct |
62 ms |
94332 KB |
Output is correct |
4 |
Correct |
60 ms |
94328 KB |
Output is correct |
5 |
Correct |
60 ms |
94328 KB |
Output is correct |
6 |
Correct |
60 ms |
94328 KB |
Output is correct |
7 |
Correct |
60 ms |
94328 KB |
Output is correct |
8 |
Correct |
60 ms |
94328 KB |
Output is correct |
9 |
Correct |
59 ms |
94328 KB |
Output is correct |
10 |
Correct |
61 ms |
94328 KB |
Output is correct |
11 |
Correct |
65 ms |
95096 KB |
Output is correct |
12 |
Correct |
65 ms |
95144 KB |
Output is correct |
13 |
Correct |
63 ms |
95096 KB |
Output is correct |
14 |
Correct |
67 ms |
95224 KB |
Output is correct |
15 |
Correct |
63 ms |
95480 KB |
Output is correct |
16 |
Correct |
66 ms |
95352 KB |
Output is correct |
17 |
Correct |
67 ms |
95992 KB |
Output is correct |
18 |
Correct |
65 ms |
96116 KB |
Output is correct |
19 |
Correct |
65 ms |
95884 KB |
Output is correct |
20 |
Correct |
71 ms |
96380 KB |
Output is correct |
21 |
Correct |
66 ms |
96248 KB |
Output is correct |
22 |
Correct |
67 ms |
96508 KB |
Output is correct |
23 |
Correct |
65 ms |
95736 KB |
Output is correct |
24 |
Correct |
65 ms |
95352 KB |
Output is correct |
25 |
Correct |
70 ms |
95992 KB |
Output is correct |
26 |
Correct |
68 ms |
95864 KB |
Output is correct |
27 |
Correct |
66 ms |
95988 KB |
Output is correct |
28 |
Correct |
66 ms |
95736 KB |
Output is correct |
29 |
Correct |
65 ms |
95744 KB |
Output is correct |
30 |
Correct |
65 ms |
95736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2491 ms |
423696 KB |
Output is correct |
2 |
Correct |
1055 ms |
427176 KB |
Output is correct |
3 |
Correct |
2452 ms |
415808 KB |
Output is correct |
4 |
Correct |
1031 ms |
427000 KB |
Output is correct |
5 |
Correct |
2442 ms |
363428 KB |
Output is correct |
6 |
Correct |
1018 ms |
424656 KB |
Output is correct |
7 |
Correct |
2354 ms |
386476 KB |
Output is correct |
8 |
Correct |
987 ms |
398952 KB |
Output is correct |
9 |
Correct |
2114 ms |
337464 KB |
Output is correct |
10 |
Correct |
2155 ms |
328140 KB |
Output is correct |
11 |
Correct |
2179 ms |
341308 KB |
Output is correct |
12 |
Correct |
2208 ms |
346760 KB |
Output is correct |
13 |
Correct |
2187 ms |
338092 KB |
Output is correct |
14 |
Correct |
2192 ms |
344104 KB |
Output is correct |
15 |
Correct |
2285 ms |
345856 KB |
Output is correct |
16 |
Correct |
2209 ms |
348804 KB |
Output is correct |
17 |
Correct |
2175 ms |
341836 KB |
Output is correct |
18 |
Correct |
2142 ms |
337932 KB |
Output is correct |
19 |
Correct |
2165 ms |
355252 KB |
Output is correct |
20 |
Correct |
2135 ms |
328192 KB |
Output is correct |
21 |
Correct |
60 ms |
94328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
94328 KB |
Output is correct |
2 |
Correct |
61 ms |
94328 KB |
Output is correct |
3 |
Correct |
62 ms |
94332 KB |
Output is correct |
4 |
Correct |
60 ms |
94328 KB |
Output is correct |
5 |
Correct |
60 ms |
94328 KB |
Output is correct |
6 |
Correct |
60 ms |
94328 KB |
Output is correct |
7 |
Correct |
60 ms |
94328 KB |
Output is correct |
8 |
Correct |
60 ms |
94328 KB |
Output is correct |
9 |
Correct |
59 ms |
94328 KB |
Output is correct |
10 |
Correct |
61 ms |
94328 KB |
Output is correct |
11 |
Correct |
65 ms |
95096 KB |
Output is correct |
12 |
Correct |
65 ms |
95144 KB |
Output is correct |
13 |
Correct |
63 ms |
95096 KB |
Output is correct |
14 |
Correct |
67 ms |
95224 KB |
Output is correct |
15 |
Correct |
63 ms |
95480 KB |
Output is correct |
16 |
Correct |
66 ms |
95352 KB |
Output is correct |
17 |
Correct |
67 ms |
95992 KB |
Output is correct |
18 |
Correct |
65 ms |
96116 KB |
Output is correct |
19 |
Correct |
65 ms |
95884 KB |
Output is correct |
20 |
Correct |
71 ms |
96380 KB |
Output is correct |
21 |
Correct |
66 ms |
96248 KB |
Output is correct |
22 |
Correct |
67 ms |
96508 KB |
Output is correct |
23 |
Correct |
65 ms |
95736 KB |
Output is correct |
24 |
Correct |
65 ms |
95352 KB |
Output is correct |
25 |
Correct |
70 ms |
95992 KB |
Output is correct |
26 |
Correct |
68 ms |
95864 KB |
Output is correct |
27 |
Correct |
66 ms |
95988 KB |
Output is correct |
28 |
Correct |
66 ms |
95736 KB |
Output is correct |
29 |
Correct |
65 ms |
95744 KB |
Output is correct |
30 |
Correct |
65 ms |
95736 KB |
Output is correct |
31 |
Correct |
2491 ms |
423696 KB |
Output is correct |
32 |
Correct |
1055 ms |
427176 KB |
Output is correct |
33 |
Correct |
2452 ms |
415808 KB |
Output is correct |
34 |
Correct |
1031 ms |
427000 KB |
Output is correct |
35 |
Correct |
2442 ms |
363428 KB |
Output is correct |
36 |
Correct |
1018 ms |
424656 KB |
Output is correct |
37 |
Correct |
2354 ms |
386476 KB |
Output is correct |
38 |
Correct |
987 ms |
398952 KB |
Output is correct |
39 |
Correct |
2114 ms |
337464 KB |
Output is correct |
40 |
Correct |
2155 ms |
328140 KB |
Output is correct |
41 |
Correct |
2179 ms |
341308 KB |
Output is correct |
42 |
Correct |
2208 ms |
346760 KB |
Output is correct |
43 |
Correct |
2187 ms |
338092 KB |
Output is correct |
44 |
Correct |
2192 ms |
344104 KB |
Output is correct |
45 |
Correct |
2285 ms |
345856 KB |
Output is correct |
46 |
Correct |
2209 ms |
348804 KB |
Output is correct |
47 |
Correct |
2175 ms |
341836 KB |
Output is correct |
48 |
Correct |
2142 ms |
337932 KB |
Output is correct |
49 |
Correct |
2165 ms |
355252 KB |
Output is correct |
50 |
Correct |
2135 ms |
328192 KB |
Output is correct |
51 |
Correct |
60 ms |
94328 KB |
Output is correct |
52 |
Correct |
726 ms |
172428 KB |
Output is correct |
53 |
Correct |
730 ms |
174064 KB |
Output is correct |
54 |
Correct |
730 ms |
173448 KB |
Output is correct |
55 |
Correct |
740 ms |
174512 KB |
Output is correct |
56 |
Correct |
727 ms |
173552 KB |
Output is correct |
57 |
Correct |
730 ms |
174260 KB |
Output is correct |
58 |
Correct |
1443 ms |
281368 KB |
Output is correct |
59 |
Correct |
1501 ms |
282764 KB |
Output is correct |
60 |
Correct |
1705 ms |
259196 KB |
Output is correct |
61 |
Correct |
1742 ms |
258660 KB |
Output is correct |
62 |
Correct |
1081 ms |
426996 KB |
Output is correct |
63 |
Correct |
1087 ms |
426980 KB |
Output is correct |
64 |
Correct |
1052 ms |
407856 KB |
Output is correct |
65 |
Correct |
1091 ms |
424212 KB |
Output is correct |
66 |
Correct |
1777 ms |
393116 KB |
Output is correct |
67 |
Correct |
1923 ms |
406020 KB |
Output is correct |
68 |
Correct |
1791 ms |
393068 KB |
Output is correct |
69 |
Correct |
1878 ms |
416592 KB |
Output is correct |
70 |
Correct |
1858 ms |
416876 KB |
Output is correct |
71 |
Correct |
1691 ms |
394748 KB |
Output is correct |
72 |
Correct |
1639 ms |
310644 KB |
Output is correct |
73 |
Correct |
1822 ms |
378776 KB |
Output is correct |
74 |
Correct |
1642 ms |
358084 KB |
Output is correct |
75 |
Correct |
1782 ms |
418176 KB |
Output is correct |
76 |
Correct |
946 ms |
273144 KB |
Output is correct |
77 |
Correct |
968 ms |
257736 KB |
Output is correct |
78 |
Correct |
2155 ms |
337832 KB |
Output is correct |
79 |
Correct |
2300 ms |
345140 KB |
Output is correct |
80 |
Correct |
2317 ms |
343696 KB |
Output is correct |
81 |
Correct |
2210 ms |
344400 KB |
Output is correct |
82 |
Correct |
2224 ms |
341024 KB |
Output is correct |
83 |
Correct |
2207 ms |
343160 KB |
Output is correct |
84 |
Correct |
2257 ms |
344164 KB |
Output is correct |
85 |
Correct |
2189 ms |
337640 KB |
Output is correct |
86 |
Correct |
2344 ms |
340212 KB |
Output is correct |
87 |
Correct |
2386 ms |
343912 KB |
Output is correct |
88 |
Correct |
2190 ms |
330864 KB |
Output is correct |
89 |
Correct |
2081 ms |
313048 KB |
Output is correct |
90 |
Correct |
2001 ms |
312460 KB |
Output is correct |
91 |
Correct |
2154 ms |
319000 KB |
Output is correct |
92 |
Correct |
2170 ms |
328444 KB |
Output is correct |
93 |
Correct |
2049 ms |
330120 KB |
Output is correct |
94 |
Correct |
2005 ms |
324372 KB |
Output is correct |
95 |
Correct |
1932 ms |
309580 KB |
Output is correct |
96 |
Correct |
1997 ms |
323544 KB |
Output is correct |
97 |
Correct |
1968 ms |
328124 KB |
Output is correct |