#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
ll N, K, A[500005], S[500005], vis[500005];
vector<ll> T[500005];
ll p[500005];
ll Find(ll n) { return (p[n] < 0 ? n : p[n] = Find(p[n])); }
void Union(ll u, ll v) { u = Find(u), v = Find(v); if(u != v) p[u] += p[v], p[v] = u; }
ll Size(ll u, ll p){
S[u] = 1;
for(auto v : T[u]){
if(v != p && !vis[v]) S[u] += Size(v, u);
}
return S[u];
}
ll Cent(ll u, ll p, ll n){
for(auto v : T[u]){
if(v != p && !vis[v] && S[v] > n / 2) return Cent(v, u, n);
}
return u;
}
ll chk[500005];
void dfs(ll u, ll p, ll c){
chk[u] = (A[u] == c);
for(auto v : T[u]){
if(v == p || vis[v]) continue;
dfs(v, u, c);
if(chk[v]) chk[u] = 1;
}
if(chk[u]){
if(Find(A[u]) != Find(c)){
Union(A[u], c);
}
}
}
void dnc(ll _u){
ll u = Cent(_u, -1, Size(_u, -1));
vis[u] = 1;
for(auto v : T[u]){
if(vis[v]) continue;
dfs(v, u, A[u]);
}
for(auto v : T[u]){
if(vis[v]) continue;
dnc(v);
}
}
ll deg[500005];
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> N >> K;
for(ll i = 1; i < N; i++){
ll u, v; cin >> u >> v;
T[u].push_back(v); T[v].push_back(u);
}
for(ll i = 1; i <= N; i++){
cin >> A[i];
}
fill(p, p + N + 1, -1LL);
dnc(1);
for(ll i = 1; i <= N; i++){
for(auto j : T[i]){
if(i > j) continue;
ll a = Find(A[i]), b = Find(A[j]);
if(a != b){
deg[a]++, deg[b]++;
}
}
}
ll Cnt = 0;
for(ll i = 1; i <= N; i++){
if(deg[i] == 1) Cnt++;
}
cout << (Cnt + 1) / 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12212 KB |
Output is correct |
4 |
Correct |
5 ms |
12188 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
7 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
4 ms |
12120 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
6 ms |
12120 KB |
Output is correct |
12 |
Correct |
6 ms |
12120 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12132 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12128 KB |
Output is correct |
17 |
Correct |
5 ms |
12196 KB |
Output is correct |
18 |
Correct |
4 ms |
12040 KB |
Output is correct |
19 |
Correct |
5 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
21 |
Correct |
5 ms |
12124 KB |
Output is correct |
22 |
Correct |
4 ms |
12124 KB |
Output is correct |
23 |
Correct |
4 ms |
12124 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12212 KB |
Output is correct |
4 |
Correct |
5 ms |
12188 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
7 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
4 ms |
12120 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
6 ms |
12120 KB |
Output is correct |
12 |
Correct |
6 ms |
12120 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12132 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12128 KB |
Output is correct |
17 |
Correct |
5 ms |
12196 KB |
Output is correct |
18 |
Correct |
4 ms |
12040 KB |
Output is correct |
19 |
Correct |
5 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
21 |
Correct |
5 ms |
12124 KB |
Output is correct |
22 |
Correct |
4 ms |
12124 KB |
Output is correct |
23 |
Correct |
4 ms |
12124 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
25 |
Correct |
4 ms |
12124 KB |
Output is correct |
26 |
Incorrect |
6 ms |
12380 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12212 KB |
Output is correct |
4 |
Correct |
5 ms |
12188 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
7 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
4 ms |
12120 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
6 ms |
12120 KB |
Output is correct |
12 |
Correct |
6 ms |
12120 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12132 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12128 KB |
Output is correct |
17 |
Correct |
5 ms |
12196 KB |
Output is correct |
18 |
Correct |
4 ms |
12040 KB |
Output is correct |
19 |
Correct |
5 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
21 |
Correct |
5 ms |
12124 KB |
Output is correct |
22 |
Correct |
4 ms |
12124 KB |
Output is correct |
23 |
Correct |
4 ms |
12124 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
25 |
Correct |
4 ms |
12124 KB |
Output is correct |
26 |
Correct |
30 ms |
21440 KB |
Output is correct |
27 |
Correct |
75 ms |
21072 KB |
Output is correct |
28 |
Correct |
6 ms |
12636 KB |
Output is correct |
29 |
Correct |
5 ms |
12124 KB |
Output is correct |
30 |
Correct |
5 ms |
12080 KB |
Output is correct |
31 |
Correct |
80 ms |
21076 KB |
Output is correct |
32 |
Correct |
5 ms |
12376 KB |
Output is correct |
33 |
Correct |
158 ms |
26512 KB |
Output is correct |
34 |
Correct |
73 ms |
21328 KB |
Output is correct |
35 |
Correct |
6 ms |
12380 KB |
Output is correct |
36 |
Correct |
136 ms |
21160 KB |
Output is correct |
37 |
Correct |
7 ms |
12376 KB |
Output is correct |
38 |
Correct |
6 ms |
12376 KB |
Output is correct |
39 |
Correct |
38 ms |
21316 KB |
Output is correct |
40 |
Correct |
7 ms |
12636 KB |
Output is correct |
41 |
Correct |
77 ms |
21124 KB |
Output is correct |
42 |
Correct |
148 ms |
23332 KB |
Output is correct |
43 |
Incorrect |
6 ms |
12120 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
21460 KB |
Output is correct |
2 |
Correct |
33 ms |
22472 KB |
Output is correct |
3 |
Incorrect |
8 ms |
12376 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12212 KB |
Output is correct |
4 |
Correct |
5 ms |
12188 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
7 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
4 ms |
12120 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
6 ms |
12120 KB |
Output is correct |
12 |
Correct |
6 ms |
12120 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12132 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12128 KB |
Output is correct |
17 |
Correct |
5 ms |
12196 KB |
Output is correct |
18 |
Correct |
4 ms |
12040 KB |
Output is correct |
19 |
Correct |
5 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
21 |
Correct |
5 ms |
12124 KB |
Output is correct |
22 |
Correct |
4 ms |
12124 KB |
Output is correct |
23 |
Correct |
4 ms |
12124 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
25 |
Correct |
4 ms |
12124 KB |
Output is correct |
26 |
Incorrect |
6 ms |
12380 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |