#include <iostream>
#include <vector>
#include <queue>
#define ll long long
using namespace std;
vector <ll> adj[200000];
bool C[200000];
vector <ll> V[200000];
ll n, k, a, b, cnt, f = 1e18, A[200000], B[200000], sz[200000], P[200000], visited[200000], G[200000];
void dfs_sz(ll u, ll p) {
B[u] = cnt;
sz[u] = 1;
for (auto v : adj[u]) {
if (v != p && !C[v]) {
dfs_sz(v, u);
sz[u] += sz[v];
}
}
}
ll find_cent(ll u, ll p, ll tot) {
ll mx = -1, ret = -1;
for (auto v : adj[u]) {
if (v != p && !C[v]) {
ret = max(ret, find_cent(v, u, tot));
mx = max(mx, sz[v]);
}
}
mx = max(mx, tot-sz[u]);
if (mx <= tot/2) return u;
else return ret;
}
void dfs_par(ll u, ll p) {
for (auto v : adj[u]) {
if (v != p && !C[v]) {
dfs_par(v, u);
P[v] = u;
}
}
}
void solve(ll u) {
ll cur = 0;
++cnt;
dfs_sz(u, -1);
ll cent = find_cent(u, -1, sz[u]);
C[cent] = 1;
dfs_par(cent, -1);
queue <ll> Q;
bool ok = 1;
G[A[cent]] = cnt;
Q.push(A[cent]);
while (!Q.empty()) {
auto g = Q.front();
Q.pop();
++cur;
for (auto v : V[g]) {
if (B[v] != cnt) {
ok = 0;
break;
}
ll x = v;
while (visited[x] != cnt) {
if (G[A[x]] != cnt) {
G[A[x]] = cnt;
Q.push(A[x]);
}
visited[x] = cnt;
if (x == cent) break;
x = P[x];
}
}
if (!ok) break;
}
if (ok) f = min(f, cur);
for (auto v : adj[cent]) {
if (!C[v]) solve(v);
}
}
int main() {
cin >> n >> k;
for (int i=0; i<n-1; ++i) {
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i=0; i<n; ++i) {
G[i] = -1;
visited[i] = -1;
cin >> A[i];
--A[i];
V[A[i]].push_back(i);
}
solve(0);
cout << f-1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19292 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
6 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
3 ms |
19032 KB |
Output is correct |
7 |
Correct |
5 ms |
19036 KB |
Output is correct |
8 |
Correct |
4 ms |
19036 KB |
Output is correct |
9 |
Correct |
4 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19292 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
6 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
3 ms |
19032 KB |
Output is correct |
7 |
Correct |
5 ms |
19036 KB |
Output is correct |
8 |
Correct |
4 ms |
19036 KB |
Output is correct |
9 |
Correct |
4 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
6 ms |
19244 KB |
Output is correct |
12 |
Correct |
6 ms |
19392 KB |
Output is correct |
13 |
Correct |
6 ms |
19292 KB |
Output is correct |
14 |
Correct |
6 ms |
19292 KB |
Output is correct |
15 |
Correct |
6 ms |
19292 KB |
Output is correct |
16 |
Correct |
6 ms |
19292 KB |
Output is correct |
17 |
Correct |
5 ms |
19292 KB |
Output is correct |
18 |
Correct |
5 ms |
19292 KB |
Output is correct |
19 |
Correct |
5 ms |
19292 KB |
Output is correct |
20 |
Correct |
5 ms |
19288 KB |
Output is correct |
21 |
Correct |
5 ms |
19292 KB |
Output is correct |
22 |
Correct |
6 ms |
19544 KB |
Output is correct |
23 |
Correct |
6 ms |
19292 KB |
Output is correct |
24 |
Correct |
7 ms |
19544 KB |
Output is correct |
25 |
Correct |
6 ms |
19288 KB |
Output is correct |
26 |
Correct |
7 ms |
19384 KB |
Output is correct |
27 |
Correct |
6 ms |
19292 KB |
Output is correct |
28 |
Correct |
6 ms |
19292 KB |
Output is correct |
29 |
Correct |
6 ms |
19292 KB |
Output is correct |
30 |
Correct |
6 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
491 ms |
50772 KB |
Output is correct |
2 |
Correct |
242 ms |
51028 KB |
Output is correct |
3 |
Correct |
485 ms |
50268 KB |
Output is correct |
4 |
Correct |
243 ms |
51028 KB |
Output is correct |
5 |
Correct |
486 ms |
46804 KB |
Output is correct |
6 |
Correct |
241 ms |
51176 KB |
Output is correct |
7 |
Correct |
515 ms |
47344 KB |
Output is correct |
8 |
Correct |
244 ms |
50892 KB |
Output is correct |
9 |
Correct |
558 ms |
45908 KB |
Output is correct |
10 |
Correct |
556 ms |
43028 KB |
Output is correct |
11 |
Correct |
561 ms |
46404 KB |
Output is correct |
12 |
Correct |
552 ms |
49592 KB |
Output is correct |
13 |
Correct |
537 ms |
42300 KB |
Output is correct |
14 |
Correct |
555 ms |
49808 KB |
Output is correct |
15 |
Correct |
566 ms |
49492 KB |
Output is correct |
16 |
Correct |
561 ms |
43716 KB |
Output is correct |
17 |
Correct |
537 ms |
43936 KB |
Output is correct |
18 |
Correct |
550 ms |
44432 KB |
Output is correct |
19 |
Correct |
622 ms |
48212 KB |
Output is correct |
20 |
Correct |
532 ms |
50620 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19292 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
6 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
3 ms |
19032 KB |
Output is correct |
7 |
Correct |
5 ms |
19036 KB |
Output is correct |
8 |
Correct |
4 ms |
19036 KB |
Output is correct |
9 |
Correct |
4 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
6 ms |
19244 KB |
Output is correct |
12 |
Correct |
6 ms |
19392 KB |
Output is correct |
13 |
Correct |
6 ms |
19292 KB |
Output is correct |
14 |
Correct |
6 ms |
19292 KB |
Output is correct |
15 |
Correct |
6 ms |
19292 KB |
Output is correct |
16 |
Correct |
6 ms |
19292 KB |
Output is correct |
17 |
Correct |
5 ms |
19292 KB |
Output is correct |
18 |
Correct |
5 ms |
19292 KB |
Output is correct |
19 |
Correct |
5 ms |
19292 KB |
Output is correct |
20 |
Correct |
5 ms |
19288 KB |
Output is correct |
21 |
Correct |
5 ms |
19292 KB |
Output is correct |
22 |
Correct |
6 ms |
19544 KB |
Output is correct |
23 |
Correct |
6 ms |
19292 KB |
Output is correct |
24 |
Correct |
7 ms |
19544 KB |
Output is correct |
25 |
Correct |
6 ms |
19288 KB |
Output is correct |
26 |
Correct |
7 ms |
19384 KB |
Output is correct |
27 |
Correct |
6 ms |
19292 KB |
Output is correct |
28 |
Correct |
6 ms |
19292 KB |
Output is correct |
29 |
Correct |
6 ms |
19292 KB |
Output is correct |
30 |
Correct |
6 ms |
19292 KB |
Output is correct |
31 |
Correct |
491 ms |
50772 KB |
Output is correct |
32 |
Correct |
242 ms |
51028 KB |
Output is correct |
33 |
Correct |
485 ms |
50268 KB |
Output is correct |
34 |
Correct |
243 ms |
51028 KB |
Output is correct |
35 |
Correct |
486 ms |
46804 KB |
Output is correct |
36 |
Correct |
241 ms |
51176 KB |
Output is correct |
37 |
Correct |
515 ms |
47344 KB |
Output is correct |
38 |
Correct |
244 ms |
50892 KB |
Output is correct |
39 |
Correct |
558 ms |
45908 KB |
Output is correct |
40 |
Correct |
556 ms |
43028 KB |
Output is correct |
41 |
Correct |
561 ms |
46404 KB |
Output is correct |
42 |
Correct |
552 ms |
49592 KB |
Output is correct |
43 |
Correct |
537 ms |
42300 KB |
Output is correct |
44 |
Correct |
555 ms |
49808 KB |
Output is correct |
45 |
Correct |
566 ms |
49492 KB |
Output is correct |
46 |
Correct |
561 ms |
43716 KB |
Output is correct |
47 |
Correct |
537 ms |
43936 KB |
Output is correct |
48 |
Correct |
550 ms |
44432 KB |
Output is correct |
49 |
Correct |
622 ms |
48212 KB |
Output is correct |
50 |
Correct |
532 ms |
50620 KB |
Output is correct |
51 |
Correct |
3 ms |
16988 KB |
Output is correct |
52 |
Correct |
448 ms |
32992 KB |
Output is correct |
53 |
Correct |
428 ms |
33364 KB |
Output is correct |
54 |
Correct |
385 ms |
33108 KB |
Output is correct |
55 |
Correct |
451 ms |
33364 KB |
Output is correct |
56 |
Correct |
425 ms |
33108 KB |
Output is correct |
57 |
Correct |
447 ms |
33104 KB |
Output is correct |
58 |
Correct |
445 ms |
37544 KB |
Output is correct |
59 |
Correct |
340 ms |
37460 KB |
Output is correct |
60 |
Correct |
422 ms |
36036 KB |
Output is correct |
61 |
Correct |
459 ms |
35412 KB |
Output is correct |
62 |
Correct |
255 ms |
51284 KB |
Output is correct |
63 |
Correct |
239 ms |
51252 KB |
Output is correct |
64 |
Correct |
248 ms |
51116 KB |
Output is correct |
65 |
Correct |
246 ms |
51300 KB |
Output is correct |
66 |
Correct |
307 ms |
42108 KB |
Output is correct |
67 |
Correct |
347 ms |
42204 KB |
Output is correct |
68 |
Correct |
308 ms |
42180 KB |
Output is correct |
69 |
Correct |
346 ms |
42244 KB |
Output is correct |
70 |
Correct |
365 ms |
42188 KB |
Output is correct |
71 |
Correct |
345 ms |
42396 KB |
Output is correct |
72 |
Correct |
326 ms |
41924 KB |
Output is correct |
73 |
Correct |
340 ms |
40960 KB |
Output is correct |
74 |
Correct |
329 ms |
42180 KB |
Output is correct |
75 |
Correct |
346 ms |
42436 KB |
Output is correct |
76 |
Correct |
427 ms |
40788 KB |
Output is correct |
77 |
Correct |
446 ms |
38588 KB |
Output is correct |
78 |
Correct |
547 ms |
44356 KB |
Output is correct |
79 |
Correct |
589 ms |
41684 KB |
Output is correct |
80 |
Correct |
561 ms |
50168 KB |
Output is correct |
81 |
Correct |
574 ms |
45996 KB |
Output is correct |
82 |
Correct |
541 ms |
46060 KB |
Output is correct |
83 |
Correct |
550 ms |
41896 KB |
Output is correct |
84 |
Correct |
543 ms |
49236 KB |
Output is correct |
85 |
Correct |
586 ms |
47420 KB |
Output is correct |
86 |
Correct |
501 ms |
41640 KB |
Output is correct |
87 |
Correct |
554 ms |
43600 KB |
Output is correct |
88 |
Correct |
476 ms |
45556 KB |
Output is correct |
89 |
Correct |
462 ms |
40720 KB |
Output is correct |
90 |
Correct |
489 ms |
40484 KB |
Output is correct |
91 |
Correct |
444 ms |
43324 KB |
Output is correct |
92 |
Correct |
448 ms |
41780 KB |
Output is correct |
93 |
Correct |
467 ms |
41552 KB |
Output is correct |
94 |
Correct |
455 ms |
40336 KB |
Output is correct |
95 |
Correct |
481 ms |
42580 KB |
Output is correct |
96 |
Correct |
460 ms |
40912 KB |
Output is correct |
97 |
Correct |
457 ms |
43348 KB |
Output is correct |