// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
#define __DBG 1
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) if (__DBG) cerr << #x << " = " << x << endl;
using namespace std;
//using namespace __gnu_pbds;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = (int)2e5 + 5;
const int K = 18;
vector<int> adj[MAXN], adj2[MAXN], adj3[MAXN], ord;
int tin[MAXN], tout[MAXN], timer;
int par[MAXN][K];
pii e[MAXN];
int n, m, nn;
int col[MAXN], col2[MAXN], sz[MAXN];
int boss[MAXN];
bool used[MAXN], leaf[MAXN];
void dfs(int v, int pr) {
par[v][0] = pr;
for (int i = 1; i < K; ++i) {
par[v][i] = par[par[v][i - 1]][i - 1];
}
tin[v] = ++timer;
for (int to : adj[v]) {
if (to != pr) {
dfs(to, v);
}
}
tout[v] = timer;
}
bool isUpper(int a, int b) {
return tin[a] <= tin[b] && tin[b] <= tout[a];
}
int lca(int a, int b) {
if (isUpper(a, b)) {
return a;
}
if (isUpper(b, a)) {
return b;
}
for (int i = K - 1; i >= 0; --i) {
if (!isUpper(par[a][i], b)) {
a = par[a][i];
}
}
return par[a][0];
}
void dfs2(int v) {
used[v] = 1;
for (int to : adj2[v]) {
if (!used[to]) {
dfs2(to);
}
}
ord.pb(v);
}
void dfs3(int v) {
col2[v] = nn;
++sz[nn];
used[v] = 1;
for (int to : adj3[v]) {
if (!used[to]) {
dfs3(to);
}
}
}
void addEdge(int u, int v) {
//printf("! %d %d\n", u, v);
adj2[u].pb(v);
adj3[v].pb(u);
}
void solve() {
scanf("%d %d", &n, &m);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
e[i] = mp(u, v);
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= n; ++i) {
scanf("%d", &col[i]);
}
dfs(1, 1);
for (int i = 1; i <= n; ++i) {
if (boss[col[i]] == 0) {
boss[col[i]] = i;
}
else {
boss[col[i]] = lca(boss[col[i]], i);
}
}
for (int i = 1; i < n; ++i) {
int a = e[i].fi;
int b = e[i].se;
if (a != boss[col[a]] && b == par[a][0]) {
addEdge(col[a], col[b]);
}
if (b != boss[col[b]] && a == par[b][0]) {
addEdge(col[b], col[a]);
}
}
for (int i = 1; i <= m; ++i) {
used[i] = 0;
}
for (int i = 1; i <= m; ++i) {
if (!used[i]) {
dfs2(i);
}
}
for (int i = 1; i <= m; ++i) {
used[i] = 0;
}
for (int i = 1; i <= m; ++i) {
int v = ord[m - i];
if (!used[v]) {
++nn;
dfs3(v);
}
}
for (int i = 1; i <= nn; ++i) {
leaf[i] = 1;
}
for (int i = 1; i <= m; ++i) {
for (int to : adj2[i]) {
int a = col2[i];
int b = col2[to];
if (a != b) {
leaf[a] = 0;
}
}
}
int ans = m;
for (int i = 1; i <= nn; ++i) {
if (leaf[i]) {
ans = min(ans, sz[i]);
}
}
printf("%d\n", ans - 1);
}
int main() {
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message
capital_city.cpp: In function 'void solve()':
capital_city.cpp:106:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &col[i]);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
14 ms |
14720 KB |
Output is correct |
12 |
Correct |
13 ms |
14720 KB |
Output is correct |
13 |
Correct |
14 ms |
14720 KB |
Output is correct |
14 |
Correct |
14 ms |
14720 KB |
Output is correct |
15 |
Correct |
14 ms |
14720 KB |
Output is correct |
16 |
Correct |
14 ms |
14720 KB |
Output is correct |
17 |
Correct |
13 ms |
14720 KB |
Output is correct |
18 |
Correct |
13 ms |
14720 KB |
Output is correct |
19 |
Correct |
16 ms |
14720 KB |
Output is correct |
20 |
Correct |
14 ms |
14848 KB |
Output is correct |
21 |
Correct |
13 ms |
14720 KB |
Output is correct |
22 |
Correct |
14 ms |
14848 KB |
Output is correct |
23 |
Correct |
14 ms |
14848 KB |
Output is correct |
24 |
Correct |
13 ms |
14848 KB |
Output is correct |
25 |
Correct |
14 ms |
14848 KB |
Output is correct |
26 |
Correct |
14 ms |
14848 KB |
Output is correct |
27 |
Correct |
13 ms |
14848 KB |
Output is correct |
28 |
Correct |
13 ms |
14848 KB |
Output is correct |
29 |
Correct |
13 ms |
14848 KB |
Output is correct |
30 |
Correct |
14 ms |
14720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
297 ms |
56012 KB |
Output is correct |
2 |
Correct |
151 ms |
56180 KB |
Output is correct |
3 |
Correct |
345 ms |
55796 KB |
Output is correct |
4 |
Correct |
159 ms |
56180 KB |
Output is correct |
5 |
Correct |
309 ms |
53620 KB |
Output is correct |
6 |
Correct |
200 ms |
56180 KB |
Output is correct |
7 |
Correct |
286 ms |
53616 KB |
Output is correct |
8 |
Correct |
157 ms |
55280 KB |
Output is correct |
9 |
Correct |
345 ms |
50268 KB |
Output is correct |
10 |
Correct |
285 ms |
48628 KB |
Output is correct |
11 |
Correct |
279 ms |
50420 KB |
Output is correct |
12 |
Correct |
288 ms |
52084 KB |
Output is correct |
13 |
Correct |
281 ms |
48500 KB |
Output is correct |
14 |
Correct |
310 ms |
52208 KB |
Output is correct |
15 |
Correct |
287 ms |
52084 KB |
Output is correct |
16 |
Correct |
300 ms |
49268 KB |
Output is correct |
17 |
Correct |
288 ms |
49396 KB |
Output is correct |
18 |
Correct |
292 ms |
49652 KB |
Output is correct |
19 |
Correct |
293 ms |
51448 KB |
Output is correct |
20 |
Correct |
300 ms |
52724 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
14 ms |
14720 KB |
Output is correct |
12 |
Correct |
13 ms |
14720 KB |
Output is correct |
13 |
Correct |
14 ms |
14720 KB |
Output is correct |
14 |
Correct |
14 ms |
14720 KB |
Output is correct |
15 |
Correct |
14 ms |
14720 KB |
Output is correct |
16 |
Correct |
14 ms |
14720 KB |
Output is correct |
17 |
Correct |
13 ms |
14720 KB |
Output is correct |
18 |
Correct |
13 ms |
14720 KB |
Output is correct |
19 |
Correct |
16 ms |
14720 KB |
Output is correct |
20 |
Correct |
14 ms |
14848 KB |
Output is correct |
21 |
Correct |
13 ms |
14720 KB |
Output is correct |
22 |
Correct |
14 ms |
14848 KB |
Output is correct |
23 |
Correct |
14 ms |
14848 KB |
Output is correct |
24 |
Correct |
13 ms |
14848 KB |
Output is correct |
25 |
Correct |
14 ms |
14848 KB |
Output is correct |
26 |
Correct |
14 ms |
14848 KB |
Output is correct |
27 |
Correct |
13 ms |
14848 KB |
Output is correct |
28 |
Correct |
13 ms |
14848 KB |
Output is correct |
29 |
Correct |
13 ms |
14848 KB |
Output is correct |
30 |
Correct |
14 ms |
14720 KB |
Output is correct |
31 |
Correct |
297 ms |
56012 KB |
Output is correct |
32 |
Correct |
151 ms |
56180 KB |
Output is correct |
33 |
Correct |
345 ms |
55796 KB |
Output is correct |
34 |
Correct |
159 ms |
56180 KB |
Output is correct |
35 |
Correct |
309 ms |
53620 KB |
Output is correct |
36 |
Correct |
200 ms |
56180 KB |
Output is correct |
37 |
Correct |
286 ms |
53616 KB |
Output is correct |
38 |
Correct |
157 ms |
55280 KB |
Output is correct |
39 |
Correct |
345 ms |
50268 KB |
Output is correct |
40 |
Correct |
285 ms |
48628 KB |
Output is correct |
41 |
Correct |
279 ms |
50420 KB |
Output is correct |
42 |
Correct |
288 ms |
52084 KB |
Output is correct |
43 |
Correct |
281 ms |
48500 KB |
Output is correct |
44 |
Correct |
310 ms |
52208 KB |
Output is correct |
45 |
Correct |
287 ms |
52084 KB |
Output is correct |
46 |
Correct |
300 ms |
49268 KB |
Output is correct |
47 |
Correct |
288 ms |
49396 KB |
Output is correct |
48 |
Correct |
292 ms |
49652 KB |
Output is correct |
49 |
Correct |
293 ms |
51448 KB |
Output is correct |
50 |
Correct |
300 ms |
52724 KB |
Output is correct |
51 |
Correct |
13 ms |
14592 KB |
Output is correct |
52 |
Correct |
292 ms |
43000 KB |
Output is correct |
53 |
Correct |
302 ms |
43032 KB |
Output is correct |
54 |
Correct |
284 ms |
43060 KB |
Output is correct |
55 |
Correct |
312 ms |
43128 KB |
Output is correct |
56 |
Correct |
297 ms |
43000 KB |
Output is correct |
57 |
Correct |
320 ms |
43128 KB |
Output is correct |
58 |
Correct |
305 ms |
46324 KB |
Output is correct |
59 |
Correct |
316 ms |
46836 KB |
Output is correct |
60 |
Correct |
342 ms |
45940 KB |
Output is correct |
61 |
Correct |
350 ms |
45936 KB |
Output is correct |
62 |
Correct |
168 ms |
56296 KB |
Output is correct |
63 |
Correct |
158 ms |
56180 KB |
Output is correct |
64 |
Correct |
157 ms |
55408 KB |
Output is correct |
65 |
Correct |
153 ms |
56304 KB |
Output is correct |
66 |
Correct |
246 ms |
47728 KB |
Output is correct |
67 |
Correct |
245 ms |
47576 KB |
Output is correct |
68 |
Correct |
239 ms |
47712 KB |
Output is correct |
69 |
Correct |
239 ms |
47600 KB |
Output is correct |
70 |
Correct |
254 ms |
47640 KB |
Output is correct |
71 |
Correct |
234 ms |
47600 KB |
Output is correct |
72 |
Correct |
245 ms |
47600 KB |
Output is correct |
73 |
Correct |
253 ms |
47216 KB |
Output is correct |
74 |
Correct |
238 ms |
47596 KB |
Output is correct |
75 |
Correct |
253 ms |
47728 KB |
Output is correct |
76 |
Correct |
294 ms |
50292 KB |
Output is correct |
77 |
Correct |
315 ms |
49140 KB |
Output is correct |
78 |
Correct |
289 ms |
49444 KB |
Output is correct |
79 |
Correct |
277 ms |
48120 KB |
Output is correct |
80 |
Correct |
315 ms |
52468 KB |
Output is correct |
81 |
Correct |
304 ms |
50292 KB |
Output is correct |
82 |
Correct |
324 ms |
50424 KB |
Output is correct |
83 |
Correct |
285 ms |
48372 KB |
Output is correct |
84 |
Correct |
285 ms |
51956 KB |
Output is correct |
85 |
Correct |
285 ms |
50932 KB |
Output is correct |
86 |
Correct |
275 ms |
48120 KB |
Output is correct |
87 |
Correct |
278 ms |
49144 KB |
Output is correct |
88 |
Correct |
308 ms |
49656 KB |
Output is correct |
89 |
Correct |
305 ms |
47172 KB |
Output is correct |
90 |
Correct |
288 ms |
47088 KB |
Output is correct |
91 |
Correct |
304 ms |
48500 KB |
Output is correct |
92 |
Correct |
309 ms |
47736 KB |
Output is correct |
93 |
Correct |
302 ms |
47480 KB |
Output is correct |
94 |
Correct |
304 ms |
47128 KB |
Output is correct |
95 |
Correct |
300 ms |
48024 KB |
Output is correct |
96 |
Correct |
309 ms |
47348 KB |
Output is correct |
97 |
Correct |
303 ms |
48372 KB |
Output is correct |