/* in the name of allah */
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define mk make_pair
typedef long long ll;
const int N=5e5+7;
int n,k,st[N],fn[N],t,s[N],mark[N],sum[N],ans;
vector<int>vec[N],ver[N];
void dfs1(int v=1,int p=0){
st[v]=t++;
for(auto u:vec[v]){
if(u!=p) dfs1(u,v);
}
fn[v]=t++;
}
void dfs2(int v=1,int p=0){
sum[v]=mark[v];
for(auto u:vec[v]){
if(u==p)continue;
mark[u]=1;
for(int i=1;i<=k;i++){
int x=0,y=0;
for(auto w:ver[i]){
if(st[w]>=st[u] && fn[w]<=fn[u])x++;
else y++;
}
if(x&&y)mark[u]=0;
}
dfs2(u,v);
sum[v]+=sum[u];
}
}
void dfs3(int v=1,int p=0){
if(mark[v] && (sum[v]==1 || sum[v]==sum[1]))ans++;
for(auto u:vec[v]){
if(u==p)continue;
dfs3(u,v);
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>k;
int u,v;
for(int i=0;i<n-1;i++){
cin>>u>>v;
vec[u].pb(v);
vec[v].pb(u);
}
for(int i=1;i<=n;i++){
cin>>s[i];
ver[s[i]].pb(i);
}
dfs1();
dfs2();
dfs3();
cout<<ans/2+(ans%2==1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23812 KB |
Output is correct |
2 |
Correct |
12 ms |
23816 KB |
Output is correct |
3 |
Correct |
12 ms |
23784 KB |
Output is correct |
4 |
Correct |
11 ms |
23808 KB |
Output is correct |
5 |
Correct |
13 ms |
23792 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23812 KB |
Output is correct |
8 |
Correct |
13 ms |
23808 KB |
Output is correct |
9 |
Correct |
12 ms |
23808 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23816 KB |
Output is correct |
12 |
Correct |
11 ms |
23752 KB |
Output is correct |
13 |
Correct |
12 ms |
23736 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23808 KB |
Output is correct |
16 |
Correct |
11 ms |
23816 KB |
Output is correct |
17 |
Correct |
13 ms |
23784 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
11 ms |
23812 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23812 KB |
Output is correct |
2 |
Correct |
12 ms |
23816 KB |
Output is correct |
3 |
Correct |
12 ms |
23784 KB |
Output is correct |
4 |
Correct |
11 ms |
23808 KB |
Output is correct |
5 |
Correct |
13 ms |
23792 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23812 KB |
Output is correct |
8 |
Correct |
13 ms |
23808 KB |
Output is correct |
9 |
Correct |
12 ms |
23808 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23816 KB |
Output is correct |
12 |
Correct |
11 ms |
23752 KB |
Output is correct |
13 |
Correct |
12 ms |
23736 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23808 KB |
Output is correct |
16 |
Correct |
11 ms |
23816 KB |
Output is correct |
17 |
Correct |
13 ms |
23784 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
11 ms |
23812 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
34 ms |
24020 KB |
Output is correct |
27 |
Correct |
21 ms |
24052 KB |
Output is correct |
28 |
Correct |
41 ms |
24188 KB |
Output is correct |
29 |
Correct |
40 ms |
24120 KB |
Output is correct |
30 |
Correct |
26 ms |
24064 KB |
Output is correct |
31 |
Correct |
11 ms |
23764 KB |
Output is correct |
32 |
Correct |
40 ms |
24276 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
26 ms |
24056 KB |
Output is correct |
35 |
Correct |
40 ms |
24068 KB |
Output is correct |
36 |
Correct |
22 ms |
24040 KB |
Output is correct |
37 |
Correct |
35 ms |
24048 KB |
Output is correct |
38 |
Correct |
11 ms |
23764 KB |
Output is correct |
39 |
Correct |
37 ms |
24020 KB |
Output is correct |
40 |
Correct |
35 ms |
24044 KB |
Output is correct |
41 |
Correct |
24 ms |
24028 KB |
Output is correct |
42 |
Correct |
37 ms |
24084 KB |
Output is correct |
43 |
Correct |
30 ms |
24252 KB |
Output is correct |
44 |
Correct |
12 ms |
23764 KB |
Output is correct |
45 |
Correct |
38 ms |
24152 KB |
Output is correct |
46 |
Correct |
35 ms |
24020 KB |
Output is correct |
47 |
Correct |
11 ms |
23764 KB |
Output is correct |
48 |
Correct |
28 ms |
24068 KB |
Output is correct |
49 |
Correct |
37 ms |
24020 KB |
Output is correct |
50 |
Correct |
56 ms |
24172 KB |
Output is correct |
51 |
Correct |
28 ms |
24020 KB |
Output is correct |
52 |
Correct |
27 ms |
24012 KB |
Output is correct |
53 |
Correct |
36 ms |
24012 KB |
Output is correct |
54 |
Correct |
25 ms |
24064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23812 KB |
Output is correct |
2 |
Correct |
12 ms |
23816 KB |
Output is correct |
3 |
Correct |
12 ms |
23784 KB |
Output is correct |
4 |
Correct |
11 ms |
23808 KB |
Output is correct |
5 |
Correct |
13 ms |
23792 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23812 KB |
Output is correct |
8 |
Correct |
13 ms |
23808 KB |
Output is correct |
9 |
Correct |
12 ms |
23808 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23816 KB |
Output is correct |
12 |
Correct |
11 ms |
23752 KB |
Output is correct |
13 |
Correct |
12 ms |
23736 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23808 KB |
Output is correct |
16 |
Correct |
11 ms |
23816 KB |
Output is correct |
17 |
Correct |
13 ms |
23784 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
11 ms |
23812 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
10 ms |
23784 KB |
Output is correct |
26 |
Execution timed out |
3053 ms |
31236 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3079 ms |
31272 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23812 KB |
Output is correct |
2 |
Correct |
12 ms |
23816 KB |
Output is correct |
3 |
Correct |
12 ms |
23784 KB |
Output is correct |
4 |
Correct |
11 ms |
23808 KB |
Output is correct |
5 |
Correct |
13 ms |
23792 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23812 KB |
Output is correct |
8 |
Correct |
13 ms |
23808 KB |
Output is correct |
9 |
Correct |
12 ms |
23808 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23816 KB |
Output is correct |
12 |
Correct |
11 ms |
23752 KB |
Output is correct |
13 |
Correct |
12 ms |
23736 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23808 KB |
Output is correct |
16 |
Correct |
11 ms |
23816 KB |
Output is correct |
17 |
Correct |
13 ms |
23784 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
11 ms |
23812 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
34 ms |
24020 KB |
Output is correct |
27 |
Correct |
21 ms |
24052 KB |
Output is correct |
28 |
Correct |
41 ms |
24188 KB |
Output is correct |
29 |
Correct |
40 ms |
24120 KB |
Output is correct |
30 |
Correct |
26 ms |
24064 KB |
Output is correct |
31 |
Correct |
11 ms |
23764 KB |
Output is correct |
32 |
Correct |
40 ms |
24276 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
26 ms |
24056 KB |
Output is correct |
35 |
Correct |
40 ms |
24068 KB |
Output is correct |
36 |
Correct |
22 ms |
24040 KB |
Output is correct |
37 |
Correct |
35 ms |
24048 KB |
Output is correct |
38 |
Correct |
11 ms |
23764 KB |
Output is correct |
39 |
Correct |
37 ms |
24020 KB |
Output is correct |
40 |
Correct |
35 ms |
24044 KB |
Output is correct |
41 |
Correct |
24 ms |
24028 KB |
Output is correct |
42 |
Correct |
37 ms |
24084 KB |
Output is correct |
43 |
Correct |
30 ms |
24252 KB |
Output is correct |
44 |
Correct |
12 ms |
23764 KB |
Output is correct |
45 |
Correct |
38 ms |
24152 KB |
Output is correct |
46 |
Correct |
35 ms |
24020 KB |
Output is correct |
47 |
Correct |
11 ms |
23764 KB |
Output is correct |
48 |
Correct |
28 ms |
24068 KB |
Output is correct |
49 |
Correct |
37 ms |
24020 KB |
Output is correct |
50 |
Correct |
56 ms |
24172 KB |
Output is correct |
51 |
Correct |
28 ms |
24020 KB |
Output is correct |
52 |
Correct |
27 ms |
24012 KB |
Output is correct |
53 |
Correct |
36 ms |
24012 KB |
Output is correct |
54 |
Correct |
25 ms |
24064 KB |
Output is correct |
55 |
Correct |
10 ms |
23784 KB |
Output is correct |
56 |
Execution timed out |
3053 ms |
31236 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |