//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x,a) for (const auto& x: a)
#define sz(x) (int)(x).size()
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int _i = 0; _i < (n); _i++) ni(a[_i])
#define nal(a, n) for (int _i = 0; _i < (n); _i++) nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
#define log2(x) (31 - __builtin_clz(x))
struct sparse_table {
int n;
vector<int> a;
vector<vector<int>> st;
int combine(int dl, int dr) {
return a[dl] > a[dr] ? dl : dr;
}
sparse_table() {}
sparse_table(vector<int> & a) : n(a.size()), a(a), st(log2(n) + 1, vector<int>(n)) {
for (int i = 0; i < n; i++)
st[0][i] = i;
for (int j = 1; 1 << j <= n; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[j][i] = combine(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
// query the data on the range [l, r[
int query(int l, int r) {
int s = log2(r - l);
return combine(st[s][l], st[s][r - (1 << s)]);
}
};
struct lowest_common_ancestor {
int n, m = 0;
vector<int> a, v, h, par;
vector<vector<int>>& e;
sparse_table st;
lowest_common_ancestor(vector<vector<int>> & e, int r) : n(e.size()), a(n), v(2 * n - 1), h(2 * n - 1), e(e), par(n) {
dfs(r);
st = sparse_table(h);
}
void dfs(int i, int p = -1, int d = 0) {
par[i] = p;
a[i] = m; v[m] = i; h[m++] = d;
for (int j : e[i]) {
if (j == p)
continue;
dfs(j, i, d - 1);
v[m] = i; h[m++] = d;
}
}
// calculate the lowest common ancestor of x and y
int lca(int x, int y) {
return v[st.query(min(a[x], a[y]), max(a[x], a[y]) + 1)];
}
int ok(int x, int y) {
return lca(par[x], y) == y;
}
};
struct strongly_connected_components {
int n, v = 0, c = 0;
vector<bool> ins;
vector<int> s, num, low, com, cnt;
vector<vector<int>>& e;
strongly_connected_components(vector<vector<int>> & e) : n(e.size()), ins(n), num(n, -1), low(n), com(n), e(e), cnt(n) {
for (int i = 0; i < n; i++)
if (num[i] == -1)
dfs(i);
for (int i = 0; i < n; i++)
trav(x, e[i])
if (com[x] != com[i])
cnt[com[i]] = n+1;
}
// use commented lines for biconnected components in undirected graphs
void dfs(int i) {
// void dfs(int i, int p = -1) {
num[i] = low[i] = v++;
s.push_back(i); ins[i] = true;
for (int j : e[i]) {
// if (j == p) {
// p = -1;
// continue;
// }
if (num[j] == -1)
dfs(j);
// dfs(j, i);
if (ins[j])
low[i] = min(low[i], low[j]);
}
if (low[i] == num[i]) {
int j;
do {
j = s.back(); s.pop_back(); ins[j] = false;
com[j] = c;
cnt[c]++;
} while (j != i);
c++;
}
}
};
int en[N], c[N];
// vector<int> st[N], adj[N];
vector<vector<int>> e;
int main() {
int n, k; scanf("%d %d", &n, &k);
e.resize(n);
for (int i = 1, u, v; i < n; i++) {
scanf("%d %d", &u, &v); u--, v--;
e[u].push_back(v);
e[v].push_back(u);
}
for (int i = 0; i < n; i++) {
scanf("%d", &c[i]); c[i]--;
en[c[i]] = i;
}
lowest_common_ancestor l(e, 0);
for (int i = 0; i < n; i++)
en[c[i]] = l.lca(en[c[i]], i);
vector<vector<int>> f(k);
for (int i = 0; i < n; i++) {
if (l.par[i] != -1 && c[i] != c[l.par[i]] && l.ok(i, en[c[i]])) {
f[c[i]].pb(c[l.par[i]]);
// cout << c[i]+1 << " " << c[l.par[i]]+1 << endl;
}
}
strongly_connected_components sc(f);
int ans = k-1;
for (int i = 0; i < sc.c; i++)
ans = min(ans, sc.cnt[i] - 1);
printf("%d\n", ans);
return 0;
}
Compilation message
capital_city.cpp: In constructor 'lowest_common_ancestor::lowest_common_ancestor(std::vector<std::vector<int> >&, int)':
capital_city.cpp:64:24: warning: 'lowest_common_ancestor::e' will be initialized after [-Wreorder]
vector<vector<int>>& e;
^
capital_city.cpp:63:24: warning: 'std::vector<int> lowest_common_ancestor::par' [-Wreorder]
vector<int> a, v, h, par;
^~~
capital_city.cpp:66:3: warning: when initialized here [-Wreorder]
lowest_common_ancestor(vector<vector<int>> & e, int r) : n(e.size()), a(n), v(2 * n - 1), h(2 * n - 1), e(e), par(n) {
^~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp: In constructor 'strongly_connected_components::strongly_connected_components(std::vector<std::vector<int> >&)':
capital_city.cpp:93:24: warning: 'strongly_connected_components::e' will be initialized after [-Wreorder]
vector<vector<int>>& e;
^
capital_city.cpp:92:33: warning: 'std::vector<int> strongly_connected_components::cnt' [-Wreorder]
vector<int> s, num, low, com, cnt;
^~~
capital_city.cpp:94:3: warning: when initialized here [-Wreorder]
strongly_connected_components(vector<vector<int>> & e) : n(e.size()), ins(n), num(n, -1), low(n), com(n), e(e), cnt(n) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:137:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, k; scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:140:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v); u--, v--;
~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:145:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &c[i]); c[i]--;
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
768 KB |
Output is correct |
12 |
Correct |
6 ms |
768 KB |
Output is correct |
13 |
Correct |
6 ms |
768 KB |
Output is correct |
14 |
Correct |
6 ms |
768 KB |
Output is correct |
15 |
Correct |
6 ms |
768 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
6 ms |
768 KB |
Output is correct |
18 |
Correct |
6 ms |
768 KB |
Output is correct |
19 |
Correct |
6 ms |
768 KB |
Output is correct |
20 |
Correct |
6 ms |
768 KB |
Output is correct |
21 |
Correct |
6 ms |
768 KB |
Output is correct |
22 |
Correct |
6 ms |
768 KB |
Output is correct |
23 |
Correct |
6 ms |
768 KB |
Output is correct |
24 |
Correct |
6 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
768 KB |
Output is correct |
26 |
Correct |
7 ms |
768 KB |
Output is correct |
27 |
Correct |
6 ms |
768 KB |
Output is correct |
28 |
Correct |
7 ms |
768 KB |
Output is correct |
29 |
Correct |
6 ms |
768 KB |
Output is correct |
30 |
Correct |
6 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
61788 KB |
Output is correct |
2 |
Correct |
163 ms |
61532 KB |
Output is correct |
3 |
Correct |
248 ms |
61660 KB |
Output is correct |
4 |
Correct |
164 ms |
61612 KB |
Output is correct |
5 |
Correct |
248 ms |
59996 KB |
Output is correct |
6 |
Correct |
169 ms |
61532 KB |
Output is correct |
7 |
Correct |
233 ms |
59612 KB |
Output is correct |
8 |
Correct |
163 ms |
60128 KB |
Output is correct |
9 |
Correct |
234 ms |
56796 KB |
Output is correct |
10 |
Correct |
245 ms |
55900 KB |
Output is correct |
11 |
Correct |
237 ms |
57052 KB |
Output is correct |
12 |
Correct |
249 ms |
58076 KB |
Output is correct |
13 |
Correct |
237 ms |
55772 KB |
Output is correct |
14 |
Correct |
237 ms |
58204 KB |
Output is correct |
15 |
Correct |
253 ms |
58076 KB |
Output is correct |
16 |
Correct |
234 ms |
56156 KB |
Output is correct |
17 |
Correct |
234 ms |
56412 KB |
Output is correct |
18 |
Correct |
243 ms |
56688 KB |
Output is correct |
19 |
Correct |
236 ms |
57696 KB |
Output is correct |
20 |
Correct |
244 ms |
58644 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
768 KB |
Output is correct |
12 |
Correct |
6 ms |
768 KB |
Output is correct |
13 |
Correct |
6 ms |
768 KB |
Output is correct |
14 |
Correct |
6 ms |
768 KB |
Output is correct |
15 |
Correct |
6 ms |
768 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
6 ms |
768 KB |
Output is correct |
18 |
Correct |
6 ms |
768 KB |
Output is correct |
19 |
Correct |
6 ms |
768 KB |
Output is correct |
20 |
Correct |
6 ms |
768 KB |
Output is correct |
21 |
Correct |
6 ms |
768 KB |
Output is correct |
22 |
Correct |
6 ms |
768 KB |
Output is correct |
23 |
Correct |
6 ms |
768 KB |
Output is correct |
24 |
Correct |
6 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
768 KB |
Output is correct |
26 |
Correct |
7 ms |
768 KB |
Output is correct |
27 |
Correct |
6 ms |
768 KB |
Output is correct |
28 |
Correct |
7 ms |
768 KB |
Output is correct |
29 |
Correct |
6 ms |
768 KB |
Output is correct |
30 |
Correct |
6 ms |
768 KB |
Output is correct |
31 |
Correct |
251 ms |
61788 KB |
Output is correct |
32 |
Correct |
163 ms |
61532 KB |
Output is correct |
33 |
Correct |
248 ms |
61660 KB |
Output is correct |
34 |
Correct |
164 ms |
61612 KB |
Output is correct |
35 |
Correct |
248 ms |
59996 KB |
Output is correct |
36 |
Correct |
169 ms |
61532 KB |
Output is correct |
37 |
Correct |
233 ms |
59612 KB |
Output is correct |
38 |
Correct |
163 ms |
60128 KB |
Output is correct |
39 |
Correct |
234 ms |
56796 KB |
Output is correct |
40 |
Correct |
245 ms |
55900 KB |
Output is correct |
41 |
Correct |
237 ms |
57052 KB |
Output is correct |
42 |
Correct |
249 ms |
58076 KB |
Output is correct |
43 |
Correct |
237 ms |
55772 KB |
Output is correct |
44 |
Correct |
237 ms |
58204 KB |
Output is correct |
45 |
Correct |
253 ms |
58076 KB |
Output is correct |
46 |
Correct |
234 ms |
56156 KB |
Output is correct |
47 |
Correct |
234 ms |
56412 KB |
Output is correct |
48 |
Correct |
243 ms |
56688 KB |
Output is correct |
49 |
Correct |
236 ms |
57696 KB |
Output is correct |
50 |
Correct |
244 ms |
58644 KB |
Output is correct |
51 |
Correct |
5 ms |
384 KB |
Output is correct |
52 |
Correct |
248 ms |
51804 KB |
Output is correct |
53 |
Correct |
248 ms |
51804 KB |
Output is correct |
54 |
Correct |
252 ms |
51932 KB |
Output is correct |
55 |
Correct |
246 ms |
51808 KB |
Output is correct |
56 |
Correct |
242 ms |
51804 KB |
Output is correct |
57 |
Correct |
253 ms |
51952 KB |
Output is correct |
58 |
Correct |
245 ms |
54876 KB |
Output is correct |
59 |
Correct |
238 ms |
55132 KB |
Output is correct |
60 |
Correct |
275 ms |
55748 KB |
Output is correct |
61 |
Correct |
270 ms |
55516 KB |
Output is correct |
62 |
Correct |
163 ms |
61532 KB |
Output is correct |
63 |
Correct |
164 ms |
61464 KB |
Output is correct |
64 |
Correct |
162 ms |
60508 KB |
Output is correct |
65 |
Correct |
164 ms |
61404 KB |
Output is correct |
66 |
Correct |
211 ms |
55252 KB |
Output is correct |
67 |
Correct |
198 ms |
55128 KB |
Output is correct |
68 |
Correct |
202 ms |
55252 KB |
Output is correct |
69 |
Correct |
217 ms |
55360 KB |
Output is correct |
70 |
Correct |
202 ms |
55252 KB |
Output is correct |
71 |
Correct |
199 ms |
55252 KB |
Output is correct |
72 |
Correct |
215 ms |
55380 KB |
Output is correct |
73 |
Correct |
198 ms |
54868 KB |
Output is correct |
74 |
Correct |
209 ms |
55256 KB |
Output is correct |
75 |
Correct |
212 ms |
55380 KB |
Output is correct |
76 |
Correct |
239 ms |
57948 KB |
Output is correct |
77 |
Correct |
251 ms |
57692 KB |
Output is correct |
78 |
Correct |
276 ms |
56540 KB |
Output is correct |
79 |
Correct |
257 ms |
55644 KB |
Output is correct |
80 |
Correct |
258 ms |
58204 KB |
Output is correct |
81 |
Correct |
251 ms |
57000 KB |
Output is correct |
82 |
Correct |
234 ms |
57052 KB |
Output is correct |
83 |
Correct |
245 ms |
55644 KB |
Output is correct |
84 |
Correct |
241 ms |
58076 KB |
Output is correct |
85 |
Correct |
249 ms |
57308 KB |
Output is correct |
86 |
Correct |
242 ms |
55644 KB |
Output is correct |
87 |
Correct |
251 ms |
56260 KB |
Output is correct |
88 |
Correct |
282 ms |
56540 KB |
Output is correct |
89 |
Correct |
242 ms |
55132 KB |
Output is correct |
90 |
Correct |
236 ms |
55004 KB |
Output is correct |
91 |
Correct |
265 ms |
56028 KB |
Output is correct |
92 |
Correct |
242 ms |
55388 KB |
Output is correct |
93 |
Correct |
234 ms |
55260 KB |
Output is correct |
94 |
Correct |
236 ms |
55004 KB |
Output is correct |
95 |
Correct |
242 ms |
55644 KB |
Output is correct |
96 |
Correct |
234 ms |
55132 KB |
Output is correct |
97 |
Correct |
235 ms |
55900 KB |
Output is correct |