// M
#include<bits/stdc++.h>
using namespace std;
const int N = 200005, LG = 18;
int n, k, cp, A[N], H[N], P[N][LG], M[N], C[N], SZ[N], lca[N];
vector < int > TP, Adj[N], Out[N], In[N], V[N];
void DFS(int v, int p)
{
P[v][0] = p;
for (int j = 1; j < LG; j ++)
P[v][j] = P[P[v][j - 1]][j - 1];
for (int u : Adj[v])
if (u != p)
H[u] = H[v] + 1, DFS(u, v);
}
inline int LCA(int v, int u)
{
if (H[v] < H[u])
swap(v, u);
for (int i = 0; i < LG; i ++)
if ((H[v] - H[u]) >> i & 1)
v = P[v][i];
if (v == u)
return v;
for (int i = LG - 1; ~ i; i --)
if (P[v][i] != P[u][i])
v = P[v][i], u = P[u][i];
return P[v][0];
}
void DFS2(int v)
{
M[v] = 1;
for (int u : Out[v])
if (!M[u]) DFS2(u);
TP.push_back(v);
}
void DFS3(int v)
{
C[v] = cp;
for (int u : In[v])
if (!C[u]) DFS3(u);
}
int main()
{
scanf("%d%d", &n, &k);
for (int i = 1; i < n; i ++)
{
int v, u;
scanf("%d%d", &v, &u);
Adj[v].push_back(u);
Adj[u].push_back(v);
}
for (int i = 1; i <= n; i ++)
scanf("%d", &A[i]), V[A[i]].push_back(i);
DFS(1, 0);
for (int i = 1; i <= k; i ++)
{
lca[i] = V[i][0];
for (int v : V[i])
lca[i] = LCA(lca[i], v);
}
for (int i = 2; i <= n; i ++)
if (A[i] != A[P[i][0]] && i != lca[A[i]])
Out[A[i]].push_back(A[P[i][0]]),
In[A[P[i][0]]].push_back(A[i]);
for (int i = 1; i <= k; i ++)
if (!M[i]) DFS2(i);
reverse(TP.begin(), TP.end());
for (int v : TP)
if (!C[v]) ++ cp, DFS3(v);
memset(M, 0, sizeof(M));
for (int i = 1; i <= k; i ++)
{
SZ[C[i]] ++;
for (int v : Out[i])
if (C[v] != C[i])
M[C[i]] = 1;
}
int Mn = k;
for (int i = 1; i <= cp; i ++)
if (!M[i]) Mn = min(Mn, SZ[i]);
return !printf("%d\n", Mn - 1);
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:49:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &v, &u);
~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:54:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i]), V[A[i]].push_back(i);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19968 KB |
Output is correct |
2 |
Correct |
15 ms |
19968 KB |
Output is correct |
3 |
Correct |
12 ms |
19968 KB |
Output is correct |
4 |
Correct |
14 ms |
19968 KB |
Output is correct |
5 |
Correct |
12 ms |
19968 KB |
Output is correct |
6 |
Correct |
14 ms |
19968 KB |
Output is correct |
7 |
Correct |
12 ms |
19968 KB |
Output is correct |
8 |
Correct |
12 ms |
19968 KB |
Output is correct |
9 |
Correct |
12 ms |
19968 KB |
Output is correct |
10 |
Correct |
12 ms |
19968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19968 KB |
Output is correct |
2 |
Correct |
15 ms |
19968 KB |
Output is correct |
3 |
Correct |
12 ms |
19968 KB |
Output is correct |
4 |
Correct |
14 ms |
19968 KB |
Output is correct |
5 |
Correct |
12 ms |
19968 KB |
Output is correct |
6 |
Correct |
14 ms |
19968 KB |
Output is correct |
7 |
Correct |
12 ms |
19968 KB |
Output is correct |
8 |
Correct |
12 ms |
19968 KB |
Output is correct |
9 |
Correct |
12 ms |
19968 KB |
Output is correct |
10 |
Correct |
12 ms |
19968 KB |
Output is correct |
11 |
Correct |
16 ms |
20224 KB |
Output is correct |
12 |
Correct |
14 ms |
20224 KB |
Output is correct |
13 |
Correct |
15 ms |
20224 KB |
Output is correct |
14 |
Correct |
16 ms |
20224 KB |
Output is correct |
15 |
Correct |
15 ms |
20352 KB |
Output is correct |
16 |
Correct |
14 ms |
20224 KB |
Output is correct |
17 |
Correct |
14 ms |
20224 KB |
Output is correct |
18 |
Correct |
14 ms |
20352 KB |
Output is correct |
19 |
Correct |
15 ms |
20256 KB |
Output is correct |
20 |
Correct |
14 ms |
20224 KB |
Output is correct |
21 |
Correct |
16 ms |
20224 KB |
Output is correct |
22 |
Correct |
16 ms |
20352 KB |
Output is correct |
23 |
Correct |
15 ms |
20352 KB |
Output is correct |
24 |
Correct |
15 ms |
20224 KB |
Output is correct |
25 |
Correct |
16 ms |
20352 KB |
Output is correct |
26 |
Correct |
14 ms |
20324 KB |
Output is correct |
27 |
Correct |
14 ms |
20352 KB |
Output is correct |
28 |
Correct |
14 ms |
20224 KB |
Output is correct |
29 |
Correct |
16 ms |
20224 KB |
Output is correct |
30 |
Correct |
14 ms |
20224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
472 ms |
65776 KB |
Output is correct |
2 |
Correct |
194 ms |
65928 KB |
Output is correct |
3 |
Correct |
475 ms |
65776 KB |
Output is correct |
4 |
Correct |
197 ms |
66036 KB |
Output is correct |
5 |
Correct |
463 ms |
63216 KB |
Output is correct |
6 |
Correct |
187 ms |
65908 KB |
Output is correct |
7 |
Correct |
400 ms |
62744 KB |
Output is correct |
8 |
Correct |
183 ms |
64208 KB |
Output is correct |
9 |
Correct |
353 ms |
59044 KB |
Output is correct |
10 |
Correct |
354 ms |
57464 KB |
Output is correct |
11 |
Correct |
404 ms |
59256 KB |
Output is correct |
12 |
Correct |
394 ms |
60788 KB |
Output is correct |
13 |
Correct |
385 ms |
57324 KB |
Output is correct |
14 |
Correct |
362 ms |
60920 KB |
Output is correct |
15 |
Correct |
432 ms |
61064 KB |
Output is correct |
16 |
Correct |
367 ms |
57848 KB |
Output is correct |
17 |
Correct |
364 ms |
58484 KB |
Output is correct |
18 |
Correct |
352 ms |
58432 KB |
Output is correct |
19 |
Correct |
422 ms |
60404 KB |
Output is correct |
20 |
Correct |
383 ms |
61596 KB |
Output is correct |
21 |
Correct |
12 ms |
19968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19968 KB |
Output is correct |
2 |
Correct |
15 ms |
19968 KB |
Output is correct |
3 |
Correct |
12 ms |
19968 KB |
Output is correct |
4 |
Correct |
14 ms |
19968 KB |
Output is correct |
5 |
Correct |
12 ms |
19968 KB |
Output is correct |
6 |
Correct |
14 ms |
19968 KB |
Output is correct |
7 |
Correct |
12 ms |
19968 KB |
Output is correct |
8 |
Correct |
12 ms |
19968 KB |
Output is correct |
9 |
Correct |
12 ms |
19968 KB |
Output is correct |
10 |
Correct |
12 ms |
19968 KB |
Output is correct |
11 |
Correct |
16 ms |
20224 KB |
Output is correct |
12 |
Correct |
14 ms |
20224 KB |
Output is correct |
13 |
Correct |
15 ms |
20224 KB |
Output is correct |
14 |
Correct |
16 ms |
20224 KB |
Output is correct |
15 |
Correct |
15 ms |
20352 KB |
Output is correct |
16 |
Correct |
14 ms |
20224 KB |
Output is correct |
17 |
Correct |
14 ms |
20224 KB |
Output is correct |
18 |
Correct |
14 ms |
20352 KB |
Output is correct |
19 |
Correct |
15 ms |
20256 KB |
Output is correct |
20 |
Correct |
14 ms |
20224 KB |
Output is correct |
21 |
Correct |
16 ms |
20224 KB |
Output is correct |
22 |
Correct |
16 ms |
20352 KB |
Output is correct |
23 |
Correct |
15 ms |
20352 KB |
Output is correct |
24 |
Correct |
15 ms |
20224 KB |
Output is correct |
25 |
Correct |
16 ms |
20352 KB |
Output is correct |
26 |
Correct |
14 ms |
20324 KB |
Output is correct |
27 |
Correct |
14 ms |
20352 KB |
Output is correct |
28 |
Correct |
14 ms |
20224 KB |
Output is correct |
29 |
Correct |
16 ms |
20224 KB |
Output is correct |
30 |
Correct |
14 ms |
20224 KB |
Output is correct |
31 |
Correct |
472 ms |
65776 KB |
Output is correct |
32 |
Correct |
194 ms |
65928 KB |
Output is correct |
33 |
Correct |
475 ms |
65776 KB |
Output is correct |
34 |
Correct |
197 ms |
66036 KB |
Output is correct |
35 |
Correct |
463 ms |
63216 KB |
Output is correct |
36 |
Correct |
187 ms |
65908 KB |
Output is correct |
37 |
Correct |
400 ms |
62744 KB |
Output is correct |
38 |
Correct |
183 ms |
64208 KB |
Output is correct |
39 |
Correct |
353 ms |
59044 KB |
Output is correct |
40 |
Correct |
354 ms |
57464 KB |
Output is correct |
41 |
Correct |
404 ms |
59256 KB |
Output is correct |
42 |
Correct |
394 ms |
60788 KB |
Output is correct |
43 |
Correct |
385 ms |
57324 KB |
Output is correct |
44 |
Correct |
362 ms |
60920 KB |
Output is correct |
45 |
Correct |
432 ms |
61064 KB |
Output is correct |
46 |
Correct |
367 ms |
57848 KB |
Output is correct |
47 |
Correct |
364 ms |
58484 KB |
Output is correct |
48 |
Correct |
352 ms |
58432 KB |
Output is correct |
49 |
Correct |
422 ms |
60404 KB |
Output is correct |
50 |
Correct |
383 ms |
61596 KB |
Output is correct |
51 |
Correct |
12 ms |
19968 KB |
Output is correct |
52 |
Correct |
358 ms |
51064 KB |
Output is correct |
53 |
Correct |
355 ms |
51120 KB |
Output is correct |
54 |
Correct |
370 ms |
51064 KB |
Output is correct |
55 |
Correct |
338 ms |
51192 KB |
Output is correct |
56 |
Correct |
332 ms |
51064 KB |
Output is correct |
57 |
Correct |
330 ms |
51192 KB |
Output is correct |
58 |
Correct |
375 ms |
55284 KB |
Output is correct |
59 |
Correct |
376 ms |
55792 KB |
Output is correct |
60 |
Correct |
407 ms |
55320 KB |
Output is correct |
61 |
Correct |
423 ms |
55408 KB |
Output is correct |
62 |
Correct |
187 ms |
65904 KB |
Output is correct |
63 |
Correct |
188 ms |
65904 KB |
Output is correct |
64 |
Correct |
188 ms |
64772 KB |
Output is correct |
65 |
Correct |
188 ms |
65776 KB |
Output is correct |
66 |
Correct |
302 ms |
55536 KB |
Output is correct |
67 |
Correct |
315 ms |
55536 KB |
Output is correct |
68 |
Correct |
307 ms |
55536 KB |
Output is correct |
69 |
Correct |
352 ms |
55724 KB |
Output is correct |
70 |
Correct |
319 ms |
55536 KB |
Output is correct |
71 |
Correct |
351 ms |
55664 KB |
Output is correct |
72 |
Correct |
321 ms |
55536 KB |
Output is correct |
73 |
Correct |
345 ms |
55024 KB |
Output is correct |
74 |
Correct |
322 ms |
55536 KB |
Output is correct |
75 |
Correct |
319 ms |
55676 KB |
Output is correct |
76 |
Correct |
370 ms |
60016 KB |
Output is correct |
77 |
Correct |
391 ms |
58864 KB |
Output is correct |
78 |
Correct |
363 ms |
58228 KB |
Output is correct |
79 |
Correct |
381 ms |
56948 KB |
Output is correct |
80 |
Correct |
374 ms |
61044 KB |
Output is correct |
81 |
Correct |
406 ms |
59000 KB |
Output is correct |
82 |
Correct |
402 ms |
59256 KB |
Output is correct |
83 |
Correct |
389 ms |
57204 KB |
Output is correct |
84 |
Correct |
386 ms |
60792 KB |
Output is correct |
85 |
Correct |
396 ms |
59764 KB |
Output is correct |
86 |
Correct |
371 ms |
56816 KB |
Output is correct |
87 |
Correct |
375 ms |
57868 KB |
Output is correct |
88 |
Correct |
418 ms |
58508 KB |
Output is correct |
89 |
Correct |
404 ms |
56108 KB |
Output is correct |
90 |
Correct |
388 ms |
55792 KB |
Output is correct |
91 |
Correct |
401 ms |
57204 KB |
Output is correct |
92 |
Correct |
436 ms |
56436 KB |
Output is correct |
93 |
Correct |
399 ms |
56160 KB |
Output is correct |
94 |
Correct |
358 ms |
55796 KB |
Output is correct |
95 |
Correct |
386 ms |
56888 KB |
Output is correct |
96 |
Correct |
399 ms |
56052 KB |
Output is correct |
97 |
Correct |
370 ms |
57076 KB |
Output is correct |