#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;
const int LINF = (int)1e18;
int n, k;
vector<int> adj[N];
int p[N], sz[N];
int a[N], tot[N], cnt[N], deg[N];
vector<int> v[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){
v[s].pb(a[s]);
for(auto u : adj[s]){
if(u==p) continue;
dfs(u,s);
if(sz(v[s]) < sz(v[u])) v[s].swap(v[u]);
for(auto w : v[u]) v[s].pb(w);
}
int ok = 1;
unordered_map<int,int> cnt; cnt.clear();
for(auto u : v[s]) cnt[u]++;
for(auto u : v[s]) ok&=(cnt[u]==tot[u]);
//for(auto u : v[s]) cnt[u]--;
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++){
int x = findSet(i);
for(int j : adj[i])
deg[x]+=(x!=findSet(j));
}
int cnt = 1;
for(int i = 1; i <= n; i++) cnt+=(deg[i]==1);
cout << cnt/2 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33616 KB |
Output is correct |
2 |
Correct |
6 ms |
33616 KB |
Output is correct |
3 |
Correct |
6 ms |
33788 KB |
Output is correct |
4 |
Correct |
6 ms |
33616 KB |
Output is correct |
5 |
Correct |
6 ms |
33616 KB |
Output is correct |
6 |
Correct |
6 ms |
33616 KB |
Output is correct |
7 |
Correct |
5 ms |
33616 KB |
Output is correct |
8 |
Correct |
6 ms |
33616 KB |
Output is correct |
9 |
Correct |
6 ms |
33616 KB |
Output is correct |
10 |
Correct |
5 ms |
27472 KB |
Output is correct |
11 |
Correct |
5 ms |
27472 KB |
Output is correct |
12 |
Correct |
5 ms |
25424 KB |
Output is correct |
13 |
Correct |
5 ms |
31568 KB |
Output is correct |
14 |
Correct |
6 ms |
33616 KB |
Output is correct |
15 |
Correct |
6 ms |
33616 KB |
Output is correct |
16 |
Correct |
6 ms |
33616 KB |
Output is correct |
17 |
Correct |
6 ms |
33616 KB |
Output is correct |
18 |
Correct |
6 ms |
33616 KB |
Output is correct |
19 |
Correct |
7 ms |
33616 KB |
Output is correct |
20 |
Correct |
6 ms |
33616 KB |
Output is correct |
21 |
Correct |
5 ms |
33616 KB |
Output is correct |
22 |
Correct |
5 ms |
33616 KB |
Output is correct |
23 |
Correct |
6 ms |
33616 KB |
Output is correct |
24 |
Correct |
6 ms |
33616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33616 KB |
Output is correct |
2 |
Correct |
6 ms |
33616 KB |
Output is correct |
3 |
Correct |
6 ms |
33788 KB |
Output is correct |
4 |
Correct |
6 ms |
33616 KB |
Output is correct |
5 |
Correct |
6 ms |
33616 KB |
Output is correct |
6 |
Correct |
6 ms |
33616 KB |
Output is correct |
7 |
Correct |
5 ms |
33616 KB |
Output is correct |
8 |
Correct |
6 ms |
33616 KB |
Output is correct |
9 |
Correct |
6 ms |
33616 KB |
Output is correct |
10 |
Correct |
5 ms |
27472 KB |
Output is correct |
11 |
Correct |
5 ms |
27472 KB |
Output is correct |
12 |
Correct |
5 ms |
25424 KB |
Output is correct |
13 |
Correct |
5 ms |
31568 KB |
Output is correct |
14 |
Correct |
6 ms |
33616 KB |
Output is correct |
15 |
Correct |
6 ms |
33616 KB |
Output is correct |
16 |
Correct |
6 ms |
33616 KB |
Output is correct |
17 |
Correct |
6 ms |
33616 KB |
Output is correct |
18 |
Correct |
6 ms |
33616 KB |
Output is correct |
19 |
Correct |
7 ms |
33616 KB |
Output is correct |
20 |
Correct |
6 ms |
33616 KB |
Output is correct |
21 |
Correct |
5 ms |
33616 KB |
Output is correct |
22 |
Correct |
5 ms |
33616 KB |
Output is correct |
23 |
Correct |
6 ms |
33616 KB |
Output is correct |
24 |
Correct |
6 ms |
33616 KB |
Output is correct |
25 |
Correct |
8 ms |
33616 KB |
Output is correct |
26 |
Correct |
11 ms |
33884 KB |
Output is correct |
27 |
Correct |
11 ms |
33872 KB |
Output is correct |
28 |
Correct |
136 ms |
34112 KB |
Output is correct |
29 |
Correct |
13 ms |
33872 KB |
Output is correct |
30 |
Correct |
12 ms |
33872 KB |
Output is correct |
31 |
Correct |
6 ms |
33616 KB |
Output is correct |
32 |
Correct |
168 ms |
34188 KB |
Output is correct |
33 |
Correct |
6 ms |
33616 KB |
Output is correct |
34 |
Correct |
7 ms |
33760 KB |
Output is correct |
35 |
Correct |
10 ms |
33856 KB |
Output is correct |
36 |
Correct |
10 ms |
33888 KB |
Output is correct |
37 |
Correct |
11 ms |
33872 KB |
Output is correct |
38 |
Correct |
6 ms |
33476 KB |
Output is correct |
39 |
Correct |
10 ms |
33872 KB |
Output is correct |
40 |
Correct |
8 ms |
29776 KB |
Output is correct |
41 |
Correct |
9 ms |
33872 KB |
Output is correct |
42 |
Correct |
6 ms |
29776 KB |
Output is correct |
43 |
Correct |
44 ms |
34128 KB |
Output is correct |
44 |
Correct |
6 ms |
33788 KB |
Output is correct |
45 |
Correct |
69 ms |
33872 KB |
Output is correct |
46 |
Correct |
15 ms |
33872 KB |
Output is correct |
47 |
Correct |
6 ms |
33784 KB |
Output is correct |
48 |
Correct |
10 ms |
33872 KB |
Output is correct |
49 |
Correct |
8 ms |
33872 KB |
Output is correct |
50 |
Correct |
82 ms |
33972 KB |
Output is correct |
51 |
Correct |
10 ms |
33996 KB |
Output is correct |
52 |
Correct |
6 ms |
33872 KB |
Output is correct |
53 |
Correct |
7 ms |
33872 KB |
Output is correct |
54 |
Correct |
9 ms |
33872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33616 KB |
Output is correct |
2 |
Correct |
6 ms |
33616 KB |
Output is correct |
3 |
Correct |
6 ms |
33788 KB |
Output is correct |
4 |
Correct |
6 ms |
33616 KB |
Output is correct |
5 |
Correct |
6 ms |
33616 KB |
Output is correct |
6 |
Correct |
6 ms |
33616 KB |
Output is correct |
7 |
Correct |
5 ms |
33616 KB |
Output is correct |
8 |
Correct |
6 ms |
33616 KB |
Output is correct |
9 |
Correct |
6 ms |
33616 KB |
Output is correct |
10 |
Correct |
5 ms |
27472 KB |
Output is correct |
11 |
Correct |
5 ms |
27472 KB |
Output is correct |
12 |
Correct |
5 ms |
25424 KB |
Output is correct |
13 |
Correct |
5 ms |
31568 KB |
Output is correct |
14 |
Correct |
6 ms |
33616 KB |
Output is correct |
15 |
Correct |
6 ms |
33616 KB |
Output is correct |
16 |
Correct |
6 ms |
33616 KB |
Output is correct |
17 |
Correct |
6 ms |
33616 KB |
Output is correct |
18 |
Correct |
6 ms |
33616 KB |
Output is correct |
19 |
Correct |
7 ms |
33616 KB |
Output is correct |
20 |
Correct |
6 ms |
33616 KB |
Output is correct |
21 |
Correct |
5 ms |
33616 KB |
Output is correct |
22 |
Correct |
5 ms |
33616 KB |
Output is correct |
23 |
Correct |
6 ms |
33616 KB |
Output is correct |
24 |
Correct |
6 ms |
33616 KB |
Output is correct |
25 |
Correct |
6 ms |
33616 KB |
Output is correct |
26 |
Correct |
53 ms |
40544 KB |
Output is correct |
27 |
Correct |
97 ms |
41964 KB |
Output is correct |
28 |
Correct |
12 ms |
33908 KB |
Output is correct |
29 |
Correct |
6 ms |
33616 KB |
Output is correct |
30 |
Correct |
6 ms |
33616 KB |
Output is correct |
31 |
Correct |
146 ms |
42080 KB |
Output is correct |
32 |
Correct |
8 ms |
33784 KB |
Output is correct |
33 |
Execution timed out |
3066 ms |
54600 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
40644 KB |
Output is correct |
2 |
Correct |
69 ms |
45200 KB |
Output is correct |
3 |
Correct |
10 ms |
33872 KB |
Output is correct |
4 |
Correct |
10 ms |
33872 KB |
Output is correct |
5 |
Correct |
5 ms |
33616 KB |
Output is correct |
6 |
Correct |
6 ms |
33616 KB |
Output is correct |
7 |
Correct |
7 ms |
33616 KB |
Output is correct |
8 |
Correct |
621 ms |
44500 KB |
Output is correct |
9 |
Correct |
8 ms |
33872 KB |
Output is correct |
10 |
Correct |
142 ms |
41920 KB |
Output is correct |
11 |
Correct |
5 ms |
33616 KB |
Output is correct |
12 |
Correct |
466 ms |
41996 KB |
Output is correct |
13 |
Correct |
336 ms |
44984 KB |
Output is correct |
14 |
Correct |
259 ms |
48052 KB |
Output is correct |
15 |
Correct |
55 ms |
40764 KB |
Output is correct |
16 |
Correct |
9 ms |
34040 KB |
Output is correct |
17 |
Correct |
6 ms |
33572 KB |
Output is correct |
18 |
Correct |
98 ms |
45448 KB |
Output is correct |
19 |
Execution timed out |
3048 ms |
46296 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33616 KB |
Output is correct |
2 |
Correct |
6 ms |
33616 KB |
Output is correct |
3 |
Correct |
6 ms |
33788 KB |
Output is correct |
4 |
Correct |
6 ms |
33616 KB |
Output is correct |
5 |
Correct |
6 ms |
33616 KB |
Output is correct |
6 |
Correct |
6 ms |
33616 KB |
Output is correct |
7 |
Correct |
5 ms |
33616 KB |
Output is correct |
8 |
Correct |
6 ms |
33616 KB |
Output is correct |
9 |
Correct |
6 ms |
33616 KB |
Output is correct |
10 |
Correct |
5 ms |
27472 KB |
Output is correct |
11 |
Correct |
5 ms |
27472 KB |
Output is correct |
12 |
Correct |
5 ms |
25424 KB |
Output is correct |
13 |
Correct |
5 ms |
31568 KB |
Output is correct |
14 |
Correct |
6 ms |
33616 KB |
Output is correct |
15 |
Correct |
6 ms |
33616 KB |
Output is correct |
16 |
Correct |
6 ms |
33616 KB |
Output is correct |
17 |
Correct |
6 ms |
33616 KB |
Output is correct |
18 |
Correct |
6 ms |
33616 KB |
Output is correct |
19 |
Correct |
7 ms |
33616 KB |
Output is correct |
20 |
Correct |
6 ms |
33616 KB |
Output is correct |
21 |
Correct |
5 ms |
33616 KB |
Output is correct |
22 |
Correct |
5 ms |
33616 KB |
Output is correct |
23 |
Correct |
6 ms |
33616 KB |
Output is correct |
24 |
Correct |
6 ms |
33616 KB |
Output is correct |
25 |
Correct |
8 ms |
33616 KB |
Output is correct |
26 |
Correct |
11 ms |
33884 KB |
Output is correct |
27 |
Correct |
11 ms |
33872 KB |
Output is correct |
28 |
Correct |
136 ms |
34112 KB |
Output is correct |
29 |
Correct |
13 ms |
33872 KB |
Output is correct |
30 |
Correct |
12 ms |
33872 KB |
Output is correct |
31 |
Correct |
6 ms |
33616 KB |
Output is correct |
32 |
Correct |
168 ms |
34188 KB |
Output is correct |
33 |
Correct |
6 ms |
33616 KB |
Output is correct |
34 |
Correct |
7 ms |
33760 KB |
Output is correct |
35 |
Correct |
10 ms |
33856 KB |
Output is correct |
36 |
Correct |
10 ms |
33888 KB |
Output is correct |
37 |
Correct |
11 ms |
33872 KB |
Output is correct |
38 |
Correct |
6 ms |
33476 KB |
Output is correct |
39 |
Correct |
10 ms |
33872 KB |
Output is correct |
40 |
Correct |
8 ms |
29776 KB |
Output is correct |
41 |
Correct |
9 ms |
33872 KB |
Output is correct |
42 |
Correct |
6 ms |
29776 KB |
Output is correct |
43 |
Correct |
44 ms |
34128 KB |
Output is correct |
44 |
Correct |
6 ms |
33788 KB |
Output is correct |
45 |
Correct |
69 ms |
33872 KB |
Output is correct |
46 |
Correct |
15 ms |
33872 KB |
Output is correct |
47 |
Correct |
6 ms |
33784 KB |
Output is correct |
48 |
Correct |
10 ms |
33872 KB |
Output is correct |
49 |
Correct |
8 ms |
33872 KB |
Output is correct |
50 |
Correct |
82 ms |
33972 KB |
Output is correct |
51 |
Correct |
10 ms |
33996 KB |
Output is correct |
52 |
Correct |
6 ms |
33872 KB |
Output is correct |
53 |
Correct |
7 ms |
33872 KB |
Output is correct |
54 |
Correct |
9 ms |
33872 KB |
Output is correct |
55 |
Correct |
6 ms |
33616 KB |
Output is correct |
56 |
Correct |
53 ms |
40544 KB |
Output is correct |
57 |
Correct |
97 ms |
41964 KB |
Output is correct |
58 |
Correct |
12 ms |
33908 KB |
Output is correct |
59 |
Correct |
6 ms |
33616 KB |
Output is correct |
60 |
Correct |
6 ms |
33616 KB |
Output is correct |
61 |
Correct |
146 ms |
42080 KB |
Output is correct |
62 |
Correct |
8 ms |
33784 KB |
Output is correct |
63 |
Execution timed out |
3066 ms |
54600 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |