#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
using ar2 = array<int,2>;
const int N = (int)5e5+10;
int n, k;
vector<int> adj[N];
int p[N], sz[N];
int a[N], tot[N], deg[N];
map<int,int> S[N];
int findSet(int i){return i==p[i]?i:p[i]=findSet(p[i]);}
void unionSet(int i, int j){
int x = findSet(i), y = findSet(j);
if(x==y) return;
if(sz[x]<sz[y])swap(x,y);
p[y] = x, sz[x]+=sz[y];
}
void dfs(int s, int p){
S[s][a[s]]=1;
for(auto u : adj[s]){
if(u==p) continue;
dfs(u,s);
if(sz(S[s]) < sz(S[u])) S[s].swap(S[u]);
for(auto [v,w] : S[u]) S[s][v]+=w;
}
int ok = 1;
for(auto [u,v] : S[s]) ok&=(tot[u]==v);
if(p and !ok) unionSet(s,p);
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) p[i]=i,sz[i]=1;
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
adj[a].pb(b); adj[b].pb(a);
}
for(int i = 1; i <= n; i++)
cin >> a[i], tot[a[i]]++;
dfs(1,0);
for(int i = 1; i <= n; i++)
for(int j : adj[i])
if(findSet(i)!=findSet(j))
deg[findSet(i)]++;
int cnt = 1;
for(int i = 1; i <= n; i++) cnt+=(deg[i]==1);
cout << cnt/2 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
45392 KB |
Output is correct |
2 |
Correct |
8 ms |
45392 KB |
Output is correct |
3 |
Correct |
9 ms |
45392 KB |
Output is correct |
4 |
Correct |
8 ms |
45392 KB |
Output is correct |
5 |
Correct |
8 ms |
45392 KB |
Output is correct |
6 |
Correct |
9 ms |
45392 KB |
Output is correct |
7 |
Correct |
9 ms |
45392 KB |
Output is correct |
8 |
Correct |
9 ms |
45392 KB |
Output is correct |
9 |
Correct |
8 ms |
45304 KB |
Output is correct |
10 |
Correct |
9 ms |
45392 KB |
Output is correct |
11 |
Correct |
8 ms |
45392 KB |
Output is correct |
12 |
Correct |
8 ms |
45392 KB |
Output is correct |
13 |
Correct |
8 ms |
45392 KB |
Output is correct |
14 |
Correct |
8 ms |
45392 KB |
Output is correct |
15 |
Correct |
8 ms |
45392 KB |
Output is correct |
16 |
Correct |
8 ms |
45392 KB |
Output is correct |
17 |
Correct |
8 ms |
45392 KB |
Output is correct |
18 |
Correct |
9 ms |
45480 KB |
Output is correct |
19 |
Correct |
9 ms |
45392 KB |
Output is correct |
20 |
Correct |
8 ms |
45420 KB |
Output is correct |
21 |
Correct |
9 ms |
45392 KB |
Output is correct |
22 |
Correct |
8 ms |
45392 KB |
Output is correct |
23 |
Correct |
8 ms |
45392 KB |
Output is correct |
24 |
Correct |
8 ms |
45404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
45392 KB |
Output is correct |
2 |
Correct |
8 ms |
45392 KB |
Output is correct |
3 |
Correct |
9 ms |
45392 KB |
Output is correct |
4 |
Correct |
8 ms |
45392 KB |
Output is correct |
5 |
Correct |
8 ms |
45392 KB |
Output is correct |
6 |
Correct |
9 ms |
45392 KB |
Output is correct |
7 |
Correct |
9 ms |
45392 KB |
Output is correct |
8 |
Correct |
9 ms |
45392 KB |
Output is correct |
9 |
Correct |
8 ms |
45304 KB |
Output is correct |
10 |
Correct |
9 ms |
45392 KB |
Output is correct |
11 |
Correct |
8 ms |
45392 KB |
Output is correct |
12 |
Correct |
8 ms |
45392 KB |
Output is correct |
13 |
Correct |
8 ms |
45392 KB |
Output is correct |
14 |
Correct |
8 ms |
45392 KB |
Output is correct |
15 |
Correct |
8 ms |
45392 KB |
Output is correct |
16 |
Correct |
8 ms |
45392 KB |
Output is correct |
17 |
Correct |
8 ms |
45392 KB |
Output is correct |
18 |
Correct |
9 ms |
45480 KB |
Output is correct |
19 |
Correct |
9 ms |
45392 KB |
Output is correct |
20 |
Correct |
8 ms |
45420 KB |
Output is correct |
21 |
Correct |
9 ms |
45392 KB |
Output is correct |
22 |
Correct |
8 ms |
45392 KB |
Output is correct |
23 |
Correct |
8 ms |
45392 KB |
Output is correct |
24 |
Correct |
8 ms |
45404 KB |
Output is correct |
25 |
Correct |
9 ms |
43344 KB |
Output is correct |
26 |
Correct |
10 ms |
43856 KB |
Output is correct |
27 |
Correct |
8 ms |
39504 KB |
Output is correct |
28 |
Correct |
21 ms |
41808 KB |
Output is correct |
29 |
Correct |
11 ms |
42064 KB |
Output is correct |
30 |
Correct |
10 ms |
46076 KB |
Output is correct |
31 |
Correct |
7 ms |
39264 KB |
Output is correct |
32 |
Correct |
24 ms |
37844 KB |
Output is correct |
33 |
Correct |
7 ms |
35920 KB |
Output is correct |
34 |
Correct |
8 ms |
37456 KB |
Output is correct |
35 |
Correct |
11 ms |
46160 KB |
Output is correct |
36 |
Correct |
13 ms |
36032 KB |
Output is correct |
37 |
Correct |
10 ms |
36432 KB |
Output is correct |
38 |
Correct |
10 ms |
35920 KB |
Output is correct |
39 |
Correct |
11 ms |
36688 KB |
Output is correct |
40 |
Correct |
11 ms |
36024 KB |
Output is correct |
41 |
Correct |
8 ms |
39672 KB |
Output is correct |
42 |
Correct |
9 ms |
39504 KB |
Output is correct |
43 |
Correct |
10 ms |
39760 KB |
Output is correct |
44 |
Correct |
8 ms |
39248 KB |
Output is correct |
45 |
Correct |
15 ms |
39760 KB |
Output is correct |
46 |
Correct |
12 ms |
45904 KB |
Output is correct |
47 |
Correct |
8 ms |
43344 KB |
Output is correct |
48 |
Correct |
10 ms |
37456 KB |
Output is correct |
49 |
Correct |
13 ms |
37496 KB |
Output is correct |
50 |
Correct |
17 ms |
36432 KB |
Output is correct |
51 |
Correct |
10 ms |
45648 KB |
Output is correct |
52 |
Correct |
9 ms |
37456 KB |
Output is correct |
53 |
Correct |
14 ms |
36180 KB |
Output is correct |
54 |
Correct |
15 ms |
36432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
45392 KB |
Output is correct |
2 |
Correct |
8 ms |
45392 KB |
Output is correct |
3 |
Correct |
9 ms |
45392 KB |
Output is correct |
4 |
Correct |
8 ms |
45392 KB |
Output is correct |
5 |
Correct |
8 ms |
45392 KB |
Output is correct |
6 |
Correct |
9 ms |
45392 KB |
Output is correct |
7 |
Correct |
9 ms |
45392 KB |
Output is correct |
8 |
Correct |
9 ms |
45392 KB |
Output is correct |
9 |
Correct |
8 ms |
45304 KB |
Output is correct |
10 |
Correct |
9 ms |
45392 KB |
Output is correct |
11 |
Correct |
8 ms |
45392 KB |
Output is correct |
12 |
Correct |
8 ms |
45392 KB |
Output is correct |
13 |
Correct |
8 ms |
45392 KB |
Output is correct |
14 |
Correct |
8 ms |
45392 KB |
Output is correct |
15 |
Correct |
8 ms |
45392 KB |
Output is correct |
16 |
Correct |
8 ms |
45392 KB |
Output is correct |
17 |
Correct |
8 ms |
45392 KB |
Output is correct |
18 |
Correct |
9 ms |
45480 KB |
Output is correct |
19 |
Correct |
9 ms |
45392 KB |
Output is correct |
20 |
Correct |
8 ms |
45420 KB |
Output is correct |
21 |
Correct |
9 ms |
45392 KB |
Output is correct |
22 |
Correct |
8 ms |
45392 KB |
Output is correct |
23 |
Correct |
8 ms |
45392 KB |
Output is correct |
24 |
Correct |
8 ms |
45404 KB |
Output is correct |
25 |
Correct |
11 ms |
35920 KB |
Output is correct |
26 |
Correct |
65 ms |
45248 KB |
Output is correct |
27 |
Correct |
96 ms |
51792 KB |
Output is correct |
28 |
Correct |
12 ms |
36176 KB |
Output is correct |
29 |
Correct |
8 ms |
41296 KB |
Output is correct |
30 |
Correct |
8 ms |
43432 KB |
Output is correct |
31 |
Correct |
61 ms |
51300 KB |
Output is correct |
32 |
Correct |
9 ms |
45648 KB |
Output is correct |
33 |
Correct |
99 ms |
63308 KB |
Output is correct |
34 |
Correct |
98 ms |
58696 KB |
Output is correct |
35 |
Correct |
10 ms |
45540 KB |
Output is correct |
36 |
Correct |
72 ms |
54444 KB |
Output is correct |
37 |
Correct |
8 ms |
39504 KB |
Output is correct |
38 |
Correct |
9 ms |
43600 KB |
Output is correct |
39 |
Correct |
57 ms |
45368 KB |
Output is correct |
40 |
Correct |
10 ms |
39760 KB |
Output is correct |
41 |
Correct |
68 ms |
48052 KB |
Output is correct |
42 |
Correct |
77 ms |
56648 KB |
Output is correct |
43 |
Correct |
9 ms |
45392 KB |
Output is correct |
44 |
Correct |
88 ms |
63824 KB |
Output is correct |
45 |
Correct |
84 ms |
59572 KB |
Output is correct |
46 |
Correct |
9 ms |
45648 KB |
Output is correct |
47 |
Correct |
10 ms |
45904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
53440 KB |
Output is correct |
2 |
Correct |
103 ms |
58260 KB |
Output is correct |
3 |
Correct |
11 ms |
46076 KB |
Output is correct |
4 |
Correct |
9 ms |
45648 KB |
Output is correct |
5 |
Correct |
8 ms |
45392 KB |
Output is correct |
6 |
Correct |
8 ms |
45392 KB |
Output is correct |
7 |
Correct |
10 ms |
45648 KB |
Output is correct |
8 |
Correct |
146 ms |
66216 KB |
Output is correct |
9 |
Correct |
10 ms |
45648 KB |
Output is correct |
10 |
Correct |
69 ms |
53496 KB |
Output is correct |
11 |
Correct |
9 ms |
45392 KB |
Output is correct |
12 |
Correct |
66 ms |
53320 KB |
Output is correct |
13 |
Correct |
141 ms |
65096 KB |
Output is correct |
14 |
Correct |
200 ms |
77640 KB |
Output is correct |
15 |
Correct |
56 ms |
53568 KB |
Output is correct |
16 |
Correct |
10 ms |
45648 KB |
Output is correct |
17 |
Correct |
9 ms |
45392 KB |
Output is correct |
18 |
Correct |
112 ms |
58688 KB |
Output is correct |
19 |
Execution timed out |
3061 ms |
56948 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
45392 KB |
Output is correct |
2 |
Correct |
8 ms |
45392 KB |
Output is correct |
3 |
Correct |
9 ms |
45392 KB |
Output is correct |
4 |
Correct |
8 ms |
45392 KB |
Output is correct |
5 |
Correct |
8 ms |
45392 KB |
Output is correct |
6 |
Correct |
9 ms |
45392 KB |
Output is correct |
7 |
Correct |
9 ms |
45392 KB |
Output is correct |
8 |
Correct |
9 ms |
45392 KB |
Output is correct |
9 |
Correct |
8 ms |
45304 KB |
Output is correct |
10 |
Correct |
9 ms |
45392 KB |
Output is correct |
11 |
Correct |
8 ms |
45392 KB |
Output is correct |
12 |
Correct |
8 ms |
45392 KB |
Output is correct |
13 |
Correct |
8 ms |
45392 KB |
Output is correct |
14 |
Correct |
8 ms |
45392 KB |
Output is correct |
15 |
Correct |
8 ms |
45392 KB |
Output is correct |
16 |
Correct |
8 ms |
45392 KB |
Output is correct |
17 |
Correct |
8 ms |
45392 KB |
Output is correct |
18 |
Correct |
9 ms |
45480 KB |
Output is correct |
19 |
Correct |
9 ms |
45392 KB |
Output is correct |
20 |
Correct |
8 ms |
45420 KB |
Output is correct |
21 |
Correct |
9 ms |
45392 KB |
Output is correct |
22 |
Correct |
8 ms |
45392 KB |
Output is correct |
23 |
Correct |
8 ms |
45392 KB |
Output is correct |
24 |
Correct |
8 ms |
45404 KB |
Output is correct |
25 |
Correct |
9 ms |
43344 KB |
Output is correct |
26 |
Correct |
10 ms |
43856 KB |
Output is correct |
27 |
Correct |
8 ms |
39504 KB |
Output is correct |
28 |
Correct |
21 ms |
41808 KB |
Output is correct |
29 |
Correct |
11 ms |
42064 KB |
Output is correct |
30 |
Correct |
10 ms |
46076 KB |
Output is correct |
31 |
Correct |
7 ms |
39264 KB |
Output is correct |
32 |
Correct |
24 ms |
37844 KB |
Output is correct |
33 |
Correct |
7 ms |
35920 KB |
Output is correct |
34 |
Correct |
8 ms |
37456 KB |
Output is correct |
35 |
Correct |
11 ms |
46160 KB |
Output is correct |
36 |
Correct |
13 ms |
36032 KB |
Output is correct |
37 |
Correct |
10 ms |
36432 KB |
Output is correct |
38 |
Correct |
10 ms |
35920 KB |
Output is correct |
39 |
Correct |
11 ms |
36688 KB |
Output is correct |
40 |
Correct |
11 ms |
36024 KB |
Output is correct |
41 |
Correct |
8 ms |
39672 KB |
Output is correct |
42 |
Correct |
9 ms |
39504 KB |
Output is correct |
43 |
Correct |
10 ms |
39760 KB |
Output is correct |
44 |
Correct |
8 ms |
39248 KB |
Output is correct |
45 |
Correct |
15 ms |
39760 KB |
Output is correct |
46 |
Correct |
12 ms |
45904 KB |
Output is correct |
47 |
Correct |
8 ms |
43344 KB |
Output is correct |
48 |
Correct |
10 ms |
37456 KB |
Output is correct |
49 |
Correct |
13 ms |
37496 KB |
Output is correct |
50 |
Correct |
17 ms |
36432 KB |
Output is correct |
51 |
Correct |
10 ms |
45648 KB |
Output is correct |
52 |
Correct |
9 ms |
37456 KB |
Output is correct |
53 |
Correct |
14 ms |
36180 KB |
Output is correct |
54 |
Correct |
15 ms |
36432 KB |
Output is correct |
55 |
Correct |
11 ms |
35920 KB |
Output is correct |
56 |
Correct |
65 ms |
45248 KB |
Output is correct |
57 |
Correct |
96 ms |
51792 KB |
Output is correct |
58 |
Correct |
12 ms |
36176 KB |
Output is correct |
59 |
Correct |
8 ms |
41296 KB |
Output is correct |
60 |
Correct |
8 ms |
43432 KB |
Output is correct |
61 |
Correct |
61 ms |
51300 KB |
Output is correct |
62 |
Correct |
9 ms |
45648 KB |
Output is correct |
63 |
Correct |
99 ms |
63308 KB |
Output is correct |
64 |
Correct |
98 ms |
58696 KB |
Output is correct |
65 |
Correct |
10 ms |
45540 KB |
Output is correct |
66 |
Correct |
72 ms |
54444 KB |
Output is correct |
67 |
Correct |
8 ms |
39504 KB |
Output is correct |
68 |
Correct |
9 ms |
43600 KB |
Output is correct |
69 |
Correct |
57 ms |
45368 KB |
Output is correct |
70 |
Correct |
10 ms |
39760 KB |
Output is correct |
71 |
Correct |
68 ms |
48052 KB |
Output is correct |
72 |
Correct |
77 ms |
56648 KB |
Output is correct |
73 |
Correct |
9 ms |
45392 KB |
Output is correct |
74 |
Correct |
88 ms |
63824 KB |
Output is correct |
75 |
Correct |
84 ms |
59572 KB |
Output is correct |
76 |
Correct |
9 ms |
45648 KB |
Output is correct |
77 |
Correct |
10 ms |
45904 KB |
Output is correct |
78 |
Correct |
62 ms |
53440 KB |
Output is correct |
79 |
Correct |
103 ms |
58260 KB |
Output is correct |
80 |
Correct |
11 ms |
46076 KB |
Output is correct |
81 |
Correct |
9 ms |
45648 KB |
Output is correct |
82 |
Correct |
8 ms |
45392 KB |
Output is correct |
83 |
Correct |
8 ms |
45392 KB |
Output is correct |
84 |
Correct |
10 ms |
45648 KB |
Output is correct |
85 |
Correct |
146 ms |
66216 KB |
Output is correct |
86 |
Correct |
10 ms |
45648 KB |
Output is correct |
87 |
Correct |
69 ms |
53496 KB |
Output is correct |
88 |
Correct |
9 ms |
45392 KB |
Output is correct |
89 |
Correct |
66 ms |
53320 KB |
Output is correct |
90 |
Correct |
141 ms |
65096 KB |
Output is correct |
91 |
Correct |
200 ms |
77640 KB |
Output is correct |
92 |
Correct |
56 ms |
53568 KB |
Output is correct |
93 |
Correct |
10 ms |
45648 KB |
Output is correct |
94 |
Correct |
9 ms |
45392 KB |
Output is correct |
95 |
Correct |
112 ms |
58688 KB |
Output is correct |
96 |
Execution timed out |
3061 ms |
56948 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |