#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
#define int long long
using ar2 = array<int,2>;
const int N = (int)2e5+10;
const int LINF = (int)1e18;
int n, k;
vector<int> adj[N];
int p[N], sz[N];
int a[N], tot[N], deg[N];
set<ar2> 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].insert({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]){
auto itr = S[s].lower_bound({v,-1});
int cnt = 0;
if(itr!=end(S[s]) and (*itr)[0]==v)
cnt = ((*itr)[1]), S[s].erase(itr);
S[s].insert({v,cnt+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 |
4 ms |
22352 KB |
Output is correct |
2 |
Correct |
4 ms |
22352 KB |
Output is correct |
3 |
Correct |
6 ms |
22372 KB |
Output is correct |
4 |
Correct |
5 ms |
22352 KB |
Output is correct |
5 |
Correct |
4 ms |
22332 KB |
Output is correct |
6 |
Correct |
4 ms |
22336 KB |
Output is correct |
7 |
Correct |
5 ms |
22380 KB |
Output is correct |
8 |
Correct |
4 ms |
22352 KB |
Output is correct |
9 |
Correct |
5 ms |
22352 KB |
Output is correct |
10 |
Correct |
5 ms |
22352 KB |
Output is correct |
11 |
Correct |
5 ms |
22352 KB |
Output is correct |
12 |
Correct |
4 ms |
22352 KB |
Output is correct |
13 |
Correct |
4 ms |
22352 KB |
Output is correct |
14 |
Correct |
4 ms |
22352 KB |
Output is correct |
15 |
Correct |
4 ms |
22352 KB |
Output is correct |
16 |
Correct |
4 ms |
22352 KB |
Output is correct |
17 |
Correct |
4 ms |
22352 KB |
Output is correct |
18 |
Correct |
4 ms |
22352 KB |
Output is correct |
19 |
Correct |
4 ms |
22324 KB |
Output is correct |
20 |
Correct |
5 ms |
22520 KB |
Output is correct |
21 |
Correct |
4 ms |
22352 KB |
Output is correct |
22 |
Correct |
4 ms |
22352 KB |
Output is correct |
23 |
Correct |
5 ms |
22368 KB |
Output is correct |
24 |
Correct |
4 ms |
22364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22352 KB |
Output is correct |
2 |
Correct |
4 ms |
22352 KB |
Output is correct |
3 |
Correct |
6 ms |
22372 KB |
Output is correct |
4 |
Correct |
5 ms |
22352 KB |
Output is correct |
5 |
Correct |
4 ms |
22332 KB |
Output is correct |
6 |
Correct |
4 ms |
22336 KB |
Output is correct |
7 |
Correct |
5 ms |
22380 KB |
Output is correct |
8 |
Correct |
4 ms |
22352 KB |
Output is correct |
9 |
Correct |
5 ms |
22352 KB |
Output is correct |
10 |
Correct |
5 ms |
22352 KB |
Output is correct |
11 |
Correct |
5 ms |
22352 KB |
Output is correct |
12 |
Correct |
4 ms |
22352 KB |
Output is correct |
13 |
Correct |
4 ms |
22352 KB |
Output is correct |
14 |
Correct |
4 ms |
22352 KB |
Output is correct |
15 |
Correct |
4 ms |
22352 KB |
Output is correct |
16 |
Correct |
4 ms |
22352 KB |
Output is correct |
17 |
Correct |
4 ms |
22352 KB |
Output is correct |
18 |
Correct |
4 ms |
22352 KB |
Output is correct |
19 |
Correct |
4 ms |
22324 KB |
Output is correct |
20 |
Correct |
5 ms |
22520 KB |
Output is correct |
21 |
Correct |
4 ms |
22352 KB |
Output is correct |
22 |
Correct |
4 ms |
22352 KB |
Output is correct |
23 |
Correct |
5 ms |
22368 KB |
Output is correct |
24 |
Correct |
4 ms |
22364 KB |
Output is correct |
25 |
Correct |
4 ms |
22520 KB |
Output is correct |
26 |
Correct |
9 ms |
23120 KB |
Output is correct |
27 |
Correct |
6 ms |
22608 KB |
Output is correct |
28 |
Correct |
21 ms |
23068 KB |
Output is correct |
29 |
Correct |
9 ms |
23376 KB |
Output is correct |
30 |
Correct |
8 ms |
22864 KB |
Output is correct |
31 |
Correct |
5 ms |
22352 KB |
Output is correct |
32 |
Correct |
25 ms |
23120 KB |
Output is correct |
33 |
Correct |
5 ms |
22352 KB |
Output is correct |
34 |
Correct |
7 ms |
22492 KB |
Output is correct |
35 |
Correct |
9 ms |
23376 KB |
Output is correct |
36 |
Correct |
6 ms |
22608 KB |
Output is correct |
37 |
Correct |
11 ms |
23120 KB |
Output is correct |
38 |
Correct |
5 ms |
22352 KB |
Output is correct |
39 |
Correct |
9 ms |
23120 KB |
Output is correct |
40 |
Correct |
6 ms |
22608 KB |
Output is correct |
41 |
Correct |
6 ms |
22608 KB |
Output is correct |
42 |
Correct |
7 ms |
22864 KB |
Output is correct |
43 |
Correct |
7 ms |
23128 KB |
Output is correct |
44 |
Correct |
4 ms |
22352 KB |
Output is correct |
45 |
Correct |
13 ms |
23120 KB |
Output is correct |
46 |
Correct |
8 ms |
22864 KB |
Output is correct |
47 |
Correct |
5 ms |
22352 KB |
Output is correct |
48 |
Correct |
7 ms |
22608 KB |
Output is correct |
49 |
Correct |
9 ms |
22864 KB |
Output is correct |
50 |
Correct |
16 ms |
23116 KB |
Output is correct |
51 |
Correct |
7 ms |
22608 KB |
Output is correct |
52 |
Correct |
7 ms |
22608 KB |
Output is correct |
53 |
Correct |
7 ms |
22608 KB |
Output is correct |
54 |
Correct |
8 ms |
22740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22352 KB |
Output is correct |
2 |
Correct |
4 ms |
22352 KB |
Output is correct |
3 |
Correct |
6 ms |
22372 KB |
Output is correct |
4 |
Correct |
5 ms |
22352 KB |
Output is correct |
5 |
Correct |
4 ms |
22332 KB |
Output is correct |
6 |
Correct |
4 ms |
22336 KB |
Output is correct |
7 |
Correct |
5 ms |
22380 KB |
Output is correct |
8 |
Correct |
4 ms |
22352 KB |
Output is correct |
9 |
Correct |
5 ms |
22352 KB |
Output is correct |
10 |
Correct |
5 ms |
22352 KB |
Output is correct |
11 |
Correct |
5 ms |
22352 KB |
Output is correct |
12 |
Correct |
4 ms |
22352 KB |
Output is correct |
13 |
Correct |
4 ms |
22352 KB |
Output is correct |
14 |
Correct |
4 ms |
22352 KB |
Output is correct |
15 |
Correct |
4 ms |
22352 KB |
Output is correct |
16 |
Correct |
4 ms |
22352 KB |
Output is correct |
17 |
Correct |
4 ms |
22352 KB |
Output is correct |
18 |
Correct |
4 ms |
22352 KB |
Output is correct |
19 |
Correct |
4 ms |
22324 KB |
Output is correct |
20 |
Correct |
5 ms |
22520 KB |
Output is correct |
21 |
Correct |
4 ms |
22352 KB |
Output is correct |
22 |
Correct |
4 ms |
22352 KB |
Output is correct |
23 |
Correct |
5 ms |
22368 KB |
Output is correct |
24 |
Correct |
4 ms |
22364 KB |
Output is correct |
25 |
Correct |
5 ms |
22352 KB |
Output is correct |
26 |
Correct |
70 ms |
33756 KB |
Output is correct |
27 |
Correct |
132 ms |
42612 KB |
Output is correct |
28 |
Correct |
6 ms |
22608 KB |
Output is correct |
29 |
Correct |
5 ms |
22352 KB |
Output is correct |
30 |
Correct |
5 ms |
22320 KB |
Output is correct |
31 |
Correct |
72 ms |
33608 KB |
Output is correct |
32 |
Correct |
6 ms |
22608 KB |
Output is correct |
33 |
Correct |
131 ms |
47712 KB |
Output is correct |
34 |
Correct |
117 ms |
40776 KB |
Output is correct |
35 |
Correct |
6 ms |
22608 KB |
Output is correct |
36 |
Correct |
74 ms |
34896 KB |
Output is correct |
37 |
Correct |
6 ms |
22620 KB |
Output is correct |
38 |
Correct |
6 ms |
22608 KB |
Output is correct |
39 |
Correct |
66 ms |
33848 KB |
Output is correct |
40 |
Correct |
6 ms |
23120 KB |
Output is correct |
41 |
Correct |
68 ms |
33608 KB |
Output is correct |
42 |
Correct |
100 ms |
37964 KB |
Output is correct |
43 |
Correct |
5 ms |
22352 KB |
Output is correct |
44 |
Correct |
87 ms |
48088 KB |
Output is correct |
45 |
Correct |
115 ms |
43336 KB |
Output is correct |
46 |
Correct |
7 ms |
22608 KB |
Output is correct |
47 |
Correct |
7 ms |
22864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
33912 KB |
Output is correct |
2 |
Correct |
122 ms |
40328 KB |
Output is correct |
3 |
Correct |
11 ms |
22988 KB |
Output is correct |
4 |
Correct |
6 ms |
22608 KB |
Output is correct |
5 |
Correct |
4 ms |
22352 KB |
Output is correct |
6 |
Correct |
4 ms |
22352 KB |
Output is correct |
7 |
Correct |
7 ms |
22608 KB |
Output is correct |
8 |
Correct |
191 ms |
51024 KB |
Output is correct |
9 |
Correct |
6 ms |
22608 KB |
Output is correct |
10 |
Correct |
62 ms |
34040 KB |
Output is correct |
11 |
Correct |
4 ms |
22352 KB |
Output is correct |
12 |
Correct |
61 ms |
33612 KB |
Output is correct |
13 |
Correct |
147 ms |
49736 KB |
Output is correct |
14 |
Correct |
197 ms |
66632 KB |
Output is correct |
15 |
Correct |
81 ms |
33856 KB |
Output is correct |
16 |
Correct |
7 ms |
22880 KB |
Output is correct |
17 |
Correct |
4 ms |
22364 KB |
Output is correct |
18 |
Correct |
118 ms |
41532 KB |
Output is correct |
19 |
Execution timed out |
3072 ms |
39188 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22352 KB |
Output is correct |
2 |
Correct |
4 ms |
22352 KB |
Output is correct |
3 |
Correct |
6 ms |
22372 KB |
Output is correct |
4 |
Correct |
5 ms |
22352 KB |
Output is correct |
5 |
Correct |
4 ms |
22332 KB |
Output is correct |
6 |
Correct |
4 ms |
22336 KB |
Output is correct |
7 |
Correct |
5 ms |
22380 KB |
Output is correct |
8 |
Correct |
4 ms |
22352 KB |
Output is correct |
9 |
Correct |
5 ms |
22352 KB |
Output is correct |
10 |
Correct |
5 ms |
22352 KB |
Output is correct |
11 |
Correct |
5 ms |
22352 KB |
Output is correct |
12 |
Correct |
4 ms |
22352 KB |
Output is correct |
13 |
Correct |
4 ms |
22352 KB |
Output is correct |
14 |
Correct |
4 ms |
22352 KB |
Output is correct |
15 |
Correct |
4 ms |
22352 KB |
Output is correct |
16 |
Correct |
4 ms |
22352 KB |
Output is correct |
17 |
Correct |
4 ms |
22352 KB |
Output is correct |
18 |
Correct |
4 ms |
22352 KB |
Output is correct |
19 |
Correct |
4 ms |
22324 KB |
Output is correct |
20 |
Correct |
5 ms |
22520 KB |
Output is correct |
21 |
Correct |
4 ms |
22352 KB |
Output is correct |
22 |
Correct |
4 ms |
22352 KB |
Output is correct |
23 |
Correct |
5 ms |
22368 KB |
Output is correct |
24 |
Correct |
4 ms |
22364 KB |
Output is correct |
25 |
Correct |
4 ms |
22520 KB |
Output is correct |
26 |
Correct |
9 ms |
23120 KB |
Output is correct |
27 |
Correct |
6 ms |
22608 KB |
Output is correct |
28 |
Correct |
21 ms |
23068 KB |
Output is correct |
29 |
Correct |
9 ms |
23376 KB |
Output is correct |
30 |
Correct |
8 ms |
22864 KB |
Output is correct |
31 |
Correct |
5 ms |
22352 KB |
Output is correct |
32 |
Correct |
25 ms |
23120 KB |
Output is correct |
33 |
Correct |
5 ms |
22352 KB |
Output is correct |
34 |
Correct |
7 ms |
22492 KB |
Output is correct |
35 |
Correct |
9 ms |
23376 KB |
Output is correct |
36 |
Correct |
6 ms |
22608 KB |
Output is correct |
37 |
Correct |
11 ms |
23120 KB |
Output is correct |
38 |
Correct |
5 ms |
22352 KB |
Output is correct |
39 |
Correct |
9 ms |
23120 KB |
Output is correct |
40 |
Correct |
6 ms |
22608 KB |
Output is correct |
41 |
Correct |
6 ms |
22608 KB |
Output is correct |
42 |
Correct |
7 ms |
22864 KB |
Output is correct |
43 |
Correct |
7 ms |
23128 KB |
Output is correct |
44 |
Correct |
4 ms |
22352 KB |
Output is correct |
45 |
Correct |
13 ms |
23120 KB |
Output is correct |
46 |
Correct |
8 ms |
22864 KB |
Output is correct |
47 |
Correct |
5 ms |
22352 KB |
Output is correct |
48 |
Correct |
7 ms |
22608 KB |
Output is correct |
49 |
Correct |
9 ms |
22864 KB |
Output is correct |
50 |
Correct |
16 ms |
23116 KB |
Output is correct |
51 |
Correct |
7 ms |
22608 KB |
Output is correct |
52 |
Correct |
7 ms |
22608 KB |
Output is correct |
53 |
Correct |
7 ms |
22608 KB |
Output is correct |
54 |
Correct |
8 ms |
22740 KB |
Output is correct |
55 |
Correct |
5 ms |
22352 KB |
Output is correct |
56 |
Correct |
70 ms |
33756 KB |
Output is correct |
57 |
Correct |
132 ms |
42612 KB |
Output is correct |
58 |
Correct |
6 ms |
22608 KB |
Output is correct |
59 |
Correct |
5 ms |
22352 KB |
Output is correct |
60 |
Correct |
5 ms |
22320 KB |
Output is correct |
61 |
Correct |
72 ms |
33608 KB |
Output is correct |
62 |
Correct |
6 ms |
22608 KB |
Output is correct |
63 |
Correct |
131 ms |
47712 KB |
Output is correct |
64 |
Correct |
117 ms |
40776 KB |
Output is correct |
65 |
Correct |
6 ms |
22608 KB |
Output is correct |
66 |
Correct |
74 ms |
34896 KB |
Output is correct |
67 |
Correct |
6 ms |
22620 KB |
Output is correct |
68 |
Correct |
6 ms |
22608 KB |
Output is correct |
69 |
Correct |
66 ms |
33848 KB |
Output is correct |
70 |
Correct |
6 ms |
23120 KB |
Output is correct |
71 |
Correct |
68 ms |
33608 KB |
Output is correct |
72 |
Correct |
100 ms |
37964 KB |
Output is correct |
73 |
Correct |
5 ms |
22352 KB |
Output is correct |
74 |
Correct |
87 ms |
48088 KB |
Output is correct |
75 |
Correct |
115 ms |
43336 KB |
Output is correct |
76 |
Correct |
7 ms |
22608 KB |
Output is correct |
77 |
Correct |
7 ms |
22864 KB |
Output is correct |
78 |
Correct |
72 ms |
33912 KB |
Output is correct |
79 |
Correct |
122 ms |
40328 KB |
Output is correct |
80 |
Correct |
11 ms |
22988 KB |
Output is correct |
81 |
Correct |
6 ms |
22608 KB |
Output is correct |
82 |
Correct |
4 ms |
22352 KB |
Output is correct |
83 |
Correct |
4 ms |
22352 KB |
Output is correct |
84 |
Correct |
7 ms |
22608 KB |
Output is correct |
85 |
Correct |
191 ms |
51024 KB |
Output is correct |
86 |
Correct |
6 ms |
22608 KB |
Output is correct |
87 |
Correct |
62 ms |
34040 KB |
Output is correct |
88 |
Correct |
4 ms |
22352 KB |
Output is correct |
89 |
Correct |
61 ms |
33612 KB |
Output is correct |
90 |
Correct |
147 ms |
49736 KB |
Output is correct |
91 |
Correct |
197 ms |
66632 KB |
Output is correct |
92 |
Correct |
81 ms |
33856 KB |
Output is correct |
93 |
Correct |
7 ms |
22880 KB |
Output is correct |
94 |
Correct |
4 ms |
22364 KB |
Output is correct |
95 |
Correct |
118 ms |
41532 KB |
Output is correct |
96 |
Execution timed out |
3072 ms |
39188 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |