#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 Cnt[500005];
vector<ll> V;
void dfs(ll u, ll p, ll d){
Cnt[A[u]] += d;
V.push_back(A[u]);
for(auto v : T[u]){
if(v == p || vis[v]) continue;
dfs(v, u, d);
}
}
ll Comp(ll u, ll p, ll c){
ll ok = (Cnt[A[u]] > 0);
for(auto v : T[u]){
if(v == p || vis[v]) continue;
if(Comp(v, u, c)) ok = 1;
}
if(ok) Union(A[u], c);
return ok;
}
void dnc(ll _u){
ll u = Cent(_u, -1, Size(_u, -1));
vis[u] = 1;
Cnt[A[u]]++;
for(auto v : T[u]){
if(!vis[v]) dfs(v, u, 1);
}
for(auto v : T[u]){
if(vis[v]) continue;
V.clear();
dfs(v, u, -1);
Comp(v, u, A[u]);
for(auto e : V) Cnt[e]++;
}
for(auto v : T[u]){
if(!vis[v]) dfs(v, u, -1);
}
Cnt[A[u]]--;
for(auto v : T[u]){
if(!vis[v]) 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 f = 0;
for(ll i = 1; i <= N; i++){
if(deg[i] == 1) f++;
}
cout << (f + 1) / 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
13400 KB |
Output is correct |
2 |
Correct |
5 ms |
13400 KB |
Output is correct |
3 |
Correct |
5 ms |
13404 KB |
Output is correct |
4 |
Correct |
5 ms |
13400 KB |
Output is correct |
5 |
Correct |
5 ms |
13404 KB |
Output is correct |
6 |
Correct |
5 ms |
13400 KB |
Output is correct |
7 |
Correct |
6 ms |
13400 KB |
Output is correct |
8 |
Correct |
6 ms |
13404 KB |
Output is correct |
9 |
Correct |
6 ms |
13400 KB |
Output is correct |
10 |
Correct |
8 ms |
13404 KB |
Output is correct |
11 |
Correct |
6 ms |
13400 KB |
Output is correct |
12 |
Correct |
5 ms |
13400 KB |
Output is correct |
13 |
Correct |
5 ms |
13404 KB |
Output is correct |
14 |
Correct |
5 ms |
13488 KB |
Output is correct |
15 |
Correct |
6 ms |
13404 KB |
Output is correct |
16 |
Correct |
5 ms |
13404 KB |
Output is correct |
17 |
Correct |
5 ms |
13400 KB |
Output is correct |
18 |
Correct |
5 ms |
13404 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
5 ms |
13404 KB |
Output is correct |
21 |
Correct |
5 ms |
13404 KB |
Output is correct |
22 |
Correct |
6 ms |
13556 KB |
Output is correct |
23 |
Correct |
6 ms |
13404 KB |
Output is correct |
24 |
Correct |
7 ms |
13404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
13400 KB |
Output is correct |
2 |
Correct |
5 ms |
13400 KB |
Output is correct |
3 |
Correct |
5 ms |
13404 KB |
Output is correct |
4 |
Correct |
5 ms |
13400 KB |
Output is correct |
5 |
Correct |
5 ms |
13404 KB |
Output is correct |
6 |
Correct |
5 ms |
13400 KB |
Output is correct |
7 |
Correct |
6 ms |
13400 KB |
Output is correct |
8 |
Correct |
6 ms |
13404 KB |
Output is correct |
9 |
Correct |
6 ms |
13400 KB |
Output is correct |
10 |
Correct |
8 ms |
13404 KB |
Output is correct |
11 |
Correct |
6 ms |
13400 KB |
Output is correct |
12 |
Correct |
5 ms |
13400 KB |
Output is correct |
13 |
Correct |
5 ms |
13404 KB |
Output is correct |
14 |
Correct |
5 ms |
13488 KB |
Output is correct |
15 |
Correct |
6 ms |
13404 KB |
Output is correct |
16 |
Correct |
5 ms |
13404 KB |
Output is correct |
17 |
Correct |
5 ms |
13400 KB |
Output is correct |
18 |
Correct |
5 ms |
13404 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
5 ms |
13404 KB |
Output is correct |
21 |
Correct |
5 ms |
13404 KB |
Output is correct |
22 |
Correct |
6 ms |
13556 KB |
Output is correct |
23 |
Correct |
6 ms |
13404 KB |
Output is correct |
24 |
Correct |
7 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13400 KB |
Output is correct |
26 |
Correct |
8 ms |
13660 KB |
Output is correct |
27 |
Correct |
7 ms |
13864 KB |
Output is correct |
28 |
Correct |
8 ms |
13916 KB |
Output is correct |
29 |
Correct |
7 ms |
13660 KB |
Output is correct |
30 |
Correct |
8 ms |
13748 KB |
Output is correct |
31 |
Correct |
5 ms |
13404 KB |
Output is correct |
32 |
Correct |
8 ms |
13912 KB |
Output is correct |
33 |
Correct |
5 ms |
13404 KB |
Output is correct |
34 |
Correct |
6 ms |
13680 KB |
Output is correct |
35 |
Correct |
7 ms |
13844 KB |
Output is correct |
36 |
Correct |
7 ms |
13656 KB |
Output is correct |
37 |
Correct |
7 ms |
13660 KB |
Output is correct |
38 |
Correct |
7 ms |
13504 KB |
Output is correct |
39 |
Correct |
7 ms |
13596 KB |
Output is correct |
40 |
Correct |
10 ms |
13660 KB |
Output is correct |
41 |
Correct |
8 ms |
14168 KB |
Output is correct |
42 |
Correct |
6 ms |
13660 KB |
Output is correct |
43 |
Correct |
8 ms |
13916 KB |
Output is correct |
44 |
Correct |
5 ms |
13352 KB |
Output is correct |
45 |
Correct |
7 ms |
13916 KB |
Output is correct |
46 |
Correct |
7 ms |
13660 KB |
Output is correct |
47 |
Correct |
5 ms |
13404 KB |
Output is correct |
48 |
Correct |
7 ms |
13660 KB |
Output is correct |
49 |
Correct |
6 ms |
13660 KB |
Output is correct |
50 |
Correct |
8 ms |
13916 KB |
Output is correct |
51 |
Correct |
7 ms |
13660 KB |
Output is correct |
52 |
Correct |
6 ms |
13832 KB |
Output is correct |
53 |
Correct |
5 ms |
13660 KB |
Output is correct |
54 |
Correct |
7 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
13400 KB |
Output is correct |
2 |
Correct |
5 ms |
13400 KB |
Output is correct |
3 |
Correct |
5 ms |
13404 KB |
Output is correct |
4 |
Correct |
5 ms |
13400 KB |
Output is correct |
5 |
Correct |
5 ms |
13404 KB |
Output is correct |
6 |
Correct |
5 ms |
13400 KB |
Output is correct |
7 |
Correct |
6 ms |
13400 KB |
Output is correct |
8 |
Correct |
6 ms |
13404 KB |
Output is correct |
9 |
Correct |
6 ms |
13400 KB |
Output is correct |
10 |
Correct |
8 ms |
13404 KB |
Output is correct |
11 |
Correct |
6 ms |
13400 KB |
Output is correct |
12 |
Correct |
5 ms |
13400 KB |
Output is correct |
13 |
Correct |
5 ms |
13404 KB |
Output is correct |
14 |
Correct |
5 ms |
13488 KB |
Output is correct |
15 |
Correct |
6 ms |
13404 KB |
Output is correct |
16 |
Correct |
5 ms |
13404 KB |
Output is correct |
17 |
Correct |
5 ms |
13400 KB |
Output is correct |
18 |
Correct |
5 ms |
13404 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
5 ms |
13404 KB |
Output is correct |
21 |
Correct |
5 ms |
13404 KB |
Output is correct |
22 |
Correct |
6 ms |
13556 KB |
Output is correct |
23 |
Correct |
6 ms |
13404 KB |
Output is correct |
24 |
Correct |
7 ms |
13404 KB |
Output is correct |
25 |
Correct |
6 ms |
13404 KB |
Output is correct |
26 |
Correct |
34 ms |
21392 KB |
Output is correct |
27 |
Correct |
133 ms |
21188 KB |
Output is correct |
28 |
Correct |
7 ms |
13912 KB |
Output is correct |
29 |
Correct |
5 ms |
13404 KB |
Output is correct |
30 |
Correct |
5 ms |
13484 KB |
Output is correct |
31 |
Correct |
132 ms |
21316 KB |
Output is correct |
32 |
Correct |
6 ms |
13656 KB |
Output is correct |
33 |
Correct |
281 ms |
27280 KB |
Output is correct |
34 |
Correct |
131 ms |
22036 KB |
Output is correct |
35 |
Correct |
9 ms |
13656 KB |
Output is correct |
36 |
Correct |
234 ms |
21888 KB |
Output is correct |
37 |
Correct |
7 ms |
13656 KB |
Output is correct |
38 |
Correct |
7 ms |
13660 KB |
Output is correct |
39 |
Correct |
37 ms |
22540 KB |
Output is correct |
40 |
Correct |
7 ms |
13916 KB |
Output is correct |
41 |
Correct |
125 ms |
21204 KB |
Output is correct |
42 |
Correct |
279 ms |
23236 KB |
Output is correct |
43 |
Correct |
5 ms |
13404 KB |
Output is correct |
44 |
Correct |
284 ms |
27544 KB |
Output is correct |
45 |
Correct |
213 ms |
25292 KB |
Output is correct |
46 |
Correct |
6 ms |
13656 KB |
Output is correct |
47 |
Correct |
7 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
21388 KB |
Output is correct |
2 |
Correct |
38 ms |
22868 KB |
Output is correct |
3 |
Correct |
7 ms |
13660 KB |
Output is correct |
4 |
Correct |
7 ms |
13868 KB |
Output is correct |
5 |
Correct |
5 ms |
13404 KB |
Output is correct |
6 |
Correct |
4 ms |
13404 KB |
Output is correct |
7 |
Correct |
6 ms |
13660 KB |
Output is correct |
8 |
Correct |
153 ms |
22656 KB |
Output is correct |
9 |
Correct |
7 ms |
13660 KB |
Output is correct |
10 |
Correct |
146 ms |
21192 KB |
Output is correct |
11 |
Correct |
5 ms |
13400 KB |
Output is correct |
12 |
Correct |
227 ms |
21504 KB |
Output is correct |
13 |
Correct |
159 ms |
22396 KB |
Output is correct |
14 |
Correct |
129 ms |
23228 KB |
Output is correct |
15 |
Correct |
37 ms |
22532 KB |
Output is correct |
16 |
Correct |
7 ms |
13660 KB |
Output is correct |
17 |
Correct |
5 ms |
12124 KB |
Output is correct |
18 |
Correct |
39 ms |
22168 KB |
Output is correct |
19 |
Correct |
287 ms |
26268 KB |
Output is correct |
20 |
Correct |
7 ms |
13656 KB |
Output is correct |
21 |
Correct |
5 ms |
13404 KB |
Output is correct |
22 |
Correct |
39 ms |
22132 KB |
Output is correct |
23 |
Correct |
7 ms |
13660 KB |
Output is correct |
24 |
Correct |
196 ms |
21652 KB |
Output is correct |
25 |
Correct |
219 ms |
26392 KB |
Output is correct |
26 |
Correct |
6 ms |
13656 KB |
Output is correct |
27 |
Correct |
7 ms |
13916 KB |
Output is correct |
28 |
Correct |
7 ms |
13660 KB |
Output is correct |
29 |
Correct |
6 ms |
13660 KB |
Output is correct |
30 |
Correct |
6 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
13400 KB |
Output is correct |
2 |
Correct |
5 ms |
13400 KB |
Output is correct |
3 |
Correct |
5 ms |
13404 KB |
Output is correct |
4 |
Correct |
5 ms |
13400 KB |
Output is correct |
5 |
Correct |
5 ms |
13404 KB |
Output is correct |
6 |
Correct |
5 ms |
13400 KB |
Output is correct |
7 |
Correct |
6 ms |
13400 KB |
Output is correct |
8 |
Correct |
6 ms |
13404 KB |
Output is correct |
9 |
Correct |
6 ms |
13400 KB |
Output is correct |
10 |
Correct |
8 ms |
13404 KB |
Output is correct |
11 |
Correct |
6 ms |
13400 KB |
Output is correct |
12 |
Correct |
5 ms |
13400 KB |
Output is correct |
13 |
Correct |
5 ms |
13404 KB |
Output is correct |
14 |
Correct |
5 ms |
13488 KB |
Output is correct |
15 |
Correct |
6 ms |
13404 KB |
Output is correct |
16 |
Correct |
5 ms |
13404 KB |
Output is correct |
17 |
Correct |
5 ms |
13400 KB |
Output is correct |
18 |
Correct |
5 ms |
13404 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
5 ms |
13404 KB |
Output is correct |
21 |
Correct |
5 ms |
13404 KB |
Output is correct |
22 |
Correct |
6 ms |
13556 KB |
Output is correct |
23 |
Correct |
6 ms |
13404 KB |
Output is correct |
24 |
Correct |
7 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13400 KB |
Output is correct |
26 |
Correct |
8 ms |
13660 KB |
Output is correct |
27 |
Correct |
7 ms |
13864 KB |
Output is correct |
28 |
Correct |
8 ms |
13916 KB |
Output is correct |
29 |
Correct |
7 ms |
13660 KB |
Output is correct |
30 |
Correct |
8 ms |
13748 KB |
Output is correct |
31 |
Correct |
5 ms |
13404 KB |
Output is correct |
32 |
Correct |
8 ms |
13912 KB |
Output is correct |
33 |
Correct |
5 ms |
13404 KB |
Output is correct |
34 |
Correct |
6 ms |
13680 KB |
Output is correct |
35 |
Correct |
7 ms |
13844 KB |
Output is correct |
36 |
Correct |
7 ms |
13656 KB |
Output is correct |
37 |
Correct |
7 ms |
13660 KB |
Output is correct |
38 |
Correct |
7 ms |
13504 KB |
Output is correct |
39 |
Correct |
7 ms |
13596 KB |
Output is correct |
40 |
Correct |
10 ms |
13660 KB |
Output is correct |
41 |
Correct |
8 ms |
14168 KB |
Output is correct |
42 |
Correct |
6 ms |
13660 KB |
Output is correct |
43 |
Correct |
8 ms |
13916 KB |
Output is correct |
44 |
Correct |
5 ms |
13352 KB |
Output is correct |
45 |
Correct |
7 ms |
13916 KB |
Output is correct |
46 |
Correct |
7 ms |
13660 KB |
Output is correct |
47 |
Correct |
5 ms |
13404 KB |
Output is correct |
48 |
Correct |
7 ms |
13660 KB |
Output is correct |
49 |
Correct |
6 ms |
13660 KB |
Output is correct |
50 |
Correct |
8 ms |
13916 KB |
Output is correct |
51 |
Correct |
7 ms |
13660 KB |
Output is correct |
52 |
Correct |
6 ms |
13832 KB |
Output is correct |
53 |
Correct |
5 ms |
13660 KB |
Output is correct |
54 |
Correct |
7 ms |
13660 KB |
Output is correct |
55 |
Correct |
6 ms |
13404 KB |
Output is correct |
56 |
Correct |
34 ms |
21392 KB |
Output is correct |
57 |
Correct |
133 ms |
21188 KB |
Output is correct |
58 |
Correct |
7 ms |
13912 KB |
Output is correct |
59 |
Correct |
5 ms |
13404 KB |
Output is correct |
60 |
Correct |
5 ms |
13484 KB |
Output is correct |
61 |
Correct |
132 ms |
21316 KB |
Output is correct |
62 |
Correct |
6 ms |
13656 KB |
Output is correct |
63 |
Correct |
281 ms |
27280 KB |
Output is correct |
64 |
Correct |
131 ms |
22036 KB |
Output is correct |
65 |
Correct |
9 ms |
13656 KB |
Output is correct |
66 |
Correct |
234 ms |
21888 KB |
Output is correct |
67 |
Correct |
7 ms |
13656 KB |
Output is correct |
68 |
Correct |
7 ms |
13660 KB |
Output is correct |
69 |
Correct |
37 ms |
22540 KB |
Output is correct |
70 |
Correct |
7 ms |
13916 KB |
Output is correct |
71 |
Correct |
125 ms |
21204 KB |
Output is correct |
72 |
Correct |
279 ms |
23236 KB |
Output is correct |
73 |
Correct |
5 ms |
13404 KB |
Output is correct |
74 |
Correct |
284 ms |
27544 KB |
Output is correct |
75 |
Correct |
213 ms |
25292 KB |
Output is correct |
76 |
Correct |
6 ms |
13656 KB |
Output is correct |
77 |
Correct |
7 ms |
13660 KB |
Output is correct |
78 |
Correct |
34 ms |
21388 KB |
Output is correct |
79 |
Correct |
38 ms |
22868 KB |
Output is correct |
80 |
Correct |
7 ms |
13660 KB |
Output is correct |
81 |
Correct |
7 ms |
13868 KB |
Output is correct |
82 |
Correct |
5 ms |
13404 KB |
Output is correct |
83 |
Correct |
4 ms |
13404 KB |
Output is correct |
84 |
Correct |
6 ms |
13660 KB |
Output is correct |
85 |
Correct |
153 ms |
22656 KB |
Output is correct |
86 |
Correct |
7 ms |
13660 KB |
Output is correct |
87 |
Correct |
146 ms |
21192 KB |
Output is correct |
88 |
Correct |
5 ms |
13400 KB |
Output is correct |
89 |
Correct |
227 ms |
21504 KB |
Output is correct |
90 |
Correct |
159 ms |
22396 KB |
Output is correct |
91 |
Correct |
129 ms |
23228 KB |
Output is correct |
92 |
Correct |
37 ms |
22532 KB |
Output is correct |
93 |
Correct |
7 ms |
13660 KB |
Output is correct |
94 |
Correct |
5 ms |
12124 KB |
Output is correct |
95 |
Correct |
39 ms |
22168 KB |
Output is correct |
96 |
Correct |
287 ms |
26268 KB |
Output is correct |
97 |
Correct |
7 ms |
13656 KB |
Output is correct |
98 |
Correct |
5 ms |
13404 KB |
Output is correct |
99 |
Correct |
39 ms |
22132 KB |
Output is correct |
100 |
Correct |
7 ms |
13660 KB |
Output is correct |
101 |
Correct |
196 ms |
21652 KB |
Output is correct |
102 |
Correct |
219 ms |
26392 KB |
Output is correct |
103 |
Correct |
6 ms |
13656 KB |
Output is correct |
104 |
Correct |
7 ms |
13916 KB |
Output is correct |
105 |
Correct |
7 ms |
13660 KB |
Output is correct |
106 |
Correct |
6 ms |
13660 KB |
Output is correct |
107 |
Correct |
6 ms |
13660 KB |
Output is correct |
108 |
Correct |
319 ms |
54528 KB |
Output is correct |
109 |
Execution timed out |
3094 ms |
64936 KB |
Time limit exceeded |
110 |
Halted |
0 ms |
0 KB |
- |