#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=5e5+5,LG=__lg(maxN)+5;
const ll inf=2e18;
int n,k,a[maxN+1],num[maxN+1],timer=0,h[maxN+1],up[maxN+1][LG+1],f[maxN+1];
vector<int> adj[maxN+1],vec[maxN+1],nadj[maxN+1];
void dfs(int u)
{
num[u]=++timer;
for(auto v:adj[u])
{
if(v==up[u][0])
{
continue;
}
up[v][0]=u;
h[v]=h[u]+1;
for(int j=1;j<=LG;j++)
{
up[v][j]=up[up[v][j-1]][j-1];
}
dfs(v);
}
}
int lca(int u,int v)
{
if(h[u]<h[v])
{
swap(u,v);
}
for(int j=LG;j>=0;j--)
{
if(h[up[u][j]]>=h[v])
{
u=up[u][j];
}
}
if(u==v)
{
return u;
}
for(int j=LG;j>=0;j--)
{
if(up[u][j]!=up[v][j])
{
u=up[u][j];
v=up[v][j];
}
}
return up[u][0];
}
void dfs1(int u)
{
for(auto v:adj[u])
{
if(v!=up[u][0])
{
dfs1(v);
f[u]+=f[v];
}
}
}
void dfs2(int u,int p)
{
for(auto v:adj[u])
{
if(v!=up[u][0])
{
int nv=p;
if(!f[v])
{
//cout<<u<<" "<<v<<'\n';
nadj[v].push_back(p);
nadj[p].push_back(v);
nv=v;
}
dfs2(v,nv);
}
}
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
h[1]=1;
dfs(1);
for(int i=1;i<=n;i++)
{
cin>>a[i];
vec[a[i]].push_back(i);
}
for(int i=1;i<=k;i++)
{
sort(vec[i].begin(),vec[i].end(),[&](int x,int y)
{
return num[x]<num[y];
});
int len=vec[i].size();
if(len==1)
{
continue;
}
for(int j=0;j<len;j++)
{
int u=vec[i][j],v=vec[i][(j+1)%len];
f[u]++;
f[lca(u,v)]--;
}
}
dfs1(1);
dfs2(1,1);
int cnt=0;
for(int i=1;i<=n;i++)
{
cnt+=(nadj[i].size()==1);
}
cout<<(cnt+1)/2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
35676 KB |
Output is correct |
2 |
Correct |
16 ms |
35672 KB |
Output is correct |
3 |
Correct |
16 ms |
35704 KB |
Output is correct |
4 |
Correct |
16 ms |
35664 KB |
Output is correct |
5 |
Correct |
18 ms |
35676 KB |
Output is correct |
6 |
Correct |
16 ms |
35672 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
19 ms |
35676 KB |
Output is correct |
9 |
Correct |
18 ms |
35672 KB |
Output is correct |
10 |
Correct |
16 ms |
35676 KB |
Output is correct |
11 |
Correct |
15 ms |
35696 KB |
Output is correct |
12 |
Correct |
16 ms |
35676 KB |
Output is correct |
13 |
Correct |
15 ms |
35696 KB |
Output is correct |
14 |
Correct |
16 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35676 KB |
Output is correct |
16 |
Correct |
15 ms |
35676 KB |
Output is correct |
17 |
Correct |
21 ms |
35700 KB |
Output is correct |
18 |
Correct |
19 ms |
35672 KB |
Output is correct |
19 |
Correct |
22 ms |
35676 KB |
Output is correct |
20 |
Correct |
16 ms |
35672 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
18 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35672 KB |
Output is correct |
24 |
Correct |
16 ms |
35728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
35676 KB |
Output is correct |
2 |
Correct |
16 ms |
35672 KB |
Output is correct |
3 |
Correct |
16 ms |
35704 KB |
Output is correct |
4 |
Correct |
16 ms |
35664 KB |
Output is correct |
5 |
Correct |
18 ms |
35676 KB |
Output is correct |
6 |
Correct |
16 ms |
35672 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
19 ms |
35676 KB |
Output is correct |
9 |
Correct |
18 ms |
35672 KB |
Output is correct |
10 |
Correct |
16 ms |
35676 KB |
Output is correct |
11 |
Correct |
15 ms |
35696 KB |
Output is correct |
12 |
Correct |
16 ms |
35676 KB |
Output is correct |
13 |
Correct |
15 ms |
35696 KB |
Output is correct |
14 |
Correct |
16 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35676 KB |
Output is correct |
16 |
Correct |
15 ms |
35676 KB |
Output is correct |
17 |
Correct |
21 ms |
35700 KB |
Output is correct |
18 |
Correct |
19 ms |
35672 KB |
Output is correct |
19 |
Correct |
22 ms |
35676 KB |
Output is correct |
20 |
Correct |
16 ms |
35672 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
18 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35672 KB |
Output is correct |
24 |
Correct |
16 ms |
35728 KB |
Output is correct |
25 |
Correct |
18 ms |
35672 KB |
Output is correct |
26 |
Correct |
22 ms |
36208 KB |
Output is correct |
27 |
Correct |
19 ms |
36184 KB |
Output is correct |
28 |
Correct |
18 ms |
36444 KB |
Output is correct |
29 |
Correct |
18 ms |
36188 KB |
Output is correct |
30 |
Correct |
17 ms |
36188 KB |
Output is correct |
31 |
Correct |
17 ms |
35676 KB |
Output is correct |
32 |
Correct |
17 ms |
36468 KB |
Output is correct |
33 |
Correct |
16 ms |
35676 KB |
Output is correct |
34 |
Correct |
17 ms |
36032 KB |
Output is correct |
35 |
Correct |
18 ms |
36188 KB |
Output is correct |
36 |
Correct |
17 ms |
36060 KB |
Output is correct |
37 |
Correct |
21 ms |
36188 KB |
Output is correct |
38 |
Correct |
15 ms |
35676 KB |
Output is correct |
39 |
Correct |
18 ms |
36184 KB |
Output is correct |
40 |
Correct |
19 ms |
36184 KB |
Output is correct |
41 |
Correct |
18 ms |
36188 KB |
Output is correct |
42 |
Correct |
17 ms |
36188 KB |
Output is correct |
43 |
Correct |
16 ms |
36308 KB |
Output is correct |
44 |
Correct |
15 ms |
35672 KB |
Output is correct |
45 |
Correct |
17 ms |
36184 KB |
Output is correct |
46 |
Correct |
17 ms |
36188 KB |
Output is correct |
47 |
Correct |
18 ms |
35676 KB |
Output is correct |
48 |
Correct |
18 ms |
36220 KB |
Output is correct |
49 |
Correct |
17 ms |
36184 KB |
Output is correct |
50 |
Correct |
18 ms |
36188 KB |
Output is correct |
51 |
Correct |
18 ms |
36188 KB |
Output is correct |
52 |
Correct |
17 ms |
36188 KB |
Output is correct |
53 |
Correct |
17 ms |
36188 KB |
Output is correct |
54 |
Correct |
19 ms |
36188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
35676 KB |
Output is correct |
2 |
Correct |
16 ms |
35672 KB |
Output is correct |
3 |
Correct |
16 ms |
35704 KB |
Output is correct |
4 |
Correct |
16 ms |
35664 KB |
Output is correct |
5 |
Correct |
18 ms |
35676 KB |
Output is correct |
6 |
Correct |
16 ms |
35672 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
19 ms |
35676 KB |
Output is correct |
9 |
Correct |
18 ms |
35672 KB |
Output is correct |
10 |
Correct |
16 ms |
35676 KB |
Output is correct |
11 |
Correct |
15 ms |
35696 KB |
Output is correct |
12 |
Correct |
16 ms |
35676 KB |
Output is correct |
13 |
Correct |
15 ms |
35696 KB |
Output is correct |
14 |
Correct |
16 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35676 KB |
Output is correct |
16 |
Correct |
15 ms |
35676 KB |
Output is correct |
17 |
Correct |
21 ms |
35700 KB |
Output is correct |
18 |
Correct |
19 ms |
35672 KB |
Output is correct |
19 |
Correct |
22 ms |
35676 KB |
Output is correct |
20 |
Correct |
16 ms |
35672 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
18 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35672 KB |
Output is correct |
24 |
Correct |
16 ms |
35728 KB |
Output is correct |
25 |
Correct |
16 ms |
35672 KB |
Output is correct |
26 |
Correct |
90 ms |
52448 KB |
Output is correct |
27 |
Correct |
88 ms |
51808 KB |
Output is correct |
28 |
Correct |
21 ms |
36188 KB |
Output is correct |
29 |
Correct |
16 ms |
35676 KB |
Output is correct |
30 |
Correct |
15 ms |
35732 KB |
Output is correct |
31 |
Correct |
82 ms |
51668 KB |
Output is correct |
32 |
Correct |
17 ms |
36188 KB |
Output is correct |
33 |
Correct |
117 ms |
58964 KB |
Output is correct |
34 |
Correct |
96 ms |
51796 KB |
Output is correct |
35 |
Correct |
17 ms |
36188 KB |
Output is correct |
36 |
Correct |
96 ms |
52308 KB |
Output is correct |
37 |
Correct |
17 ms |
36068 KB |
Output is correct |
38 |
Correct |
17 ms |
36144 KB |
Output is correct |
39 |
Correct |
83 ms |
51952 KB |
Output is correct |
40 |
Correct |
17 ms |
36188 KB |
Output is correct |
41 |
Correct |
85 ms |
51600 KB |
Output is correct |
42 |
Correct |
107 ms |
54096 KB |
Output is correct |
43 |
Correct |
16 ms |
35676 KB |
Output is correct |
44 |
Correct |
119 ms |
59008 KB |
Output is correct |
45 |
Correct |
109 ms |
55592 KB |
Output is correct |
46 |
Correct |
17 ms |
36184 KB |
Output is correct |
47 |
Correct |
17 ms |
35996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
52104 KB |
Output is correct |
2 |
Correct |
79 ms |
58056 KB |
Output is correct |
3 |
Correct |
21 ms |
36208 KB |
Output is correct |
4 |
Correct |
17 ms |
36216 KB |
Output is correct |
5 |
Correct |
16 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35676 KB |
Output is correct |
7 |
Correct |
17 ms |
36188 KB |
Output is correct |
8 |
Correct |
94 ms |
53328 KB |
Output is correct |
9 |
Correct |
17 ms |
36188 KB |
Output is correct |
10 |
Correct |
93 ms |
51960 KB |
Output is correct |
11 |
Correct |
17 ms |
35932 KB |
Output is correct |
12 |
Correct |
96 ms |
51796 KB |
Output is correct |
13 |
Correct |
95 ms |
53592 KB |
Output is correct |
14 |
Correct |
90 ms |
57936 KB |
Output is correct |
15 |
Correct |
77 ms |
52120 KB |
Output is correct |
16 |
Correct |
17 ms |
36100 KB |
Output is correct |
17 |
Correct |
16 ms |
35676 KB |
Output is correct |
18 |
Correct |
93 ms |
57416 KB |
Output is correct |
19 |
Correct |
103 ms |
61588 KB |
Output is correct |
20 |
Correct |
17 ms |
36184 KB |
Output is correct |
21 |
Correct |
17 ms |
35672 KB |
Output is correct |
22 |
Correct |
101 ms |
53824 KB |
Output is correct |
23 |
Correct |
17 ms |
36184 KB |
Output is correct |
24 |
Correct |
92 ms |
52560 KB |
Output is correct |
25 |
Correct |
98 ms |
60240 KB |
Output is correct |
26 |
Correct |
16 ms |
36188 KB |
Output is correct |
27 |
Correct |
22 ms |
36184 KB |
Output is correct |
28 |
Correct |
17 ms |
36184 KB |
Output is correct |
29 |
Correct |
17 ms |
36184 KB |
Output is correct |
30 |
Correct |
17 ms |
36188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
35676 KB |
Output is correct |
2 |
Correct |
16 ms |
35672 KB |
Output is correct |
3 |
Correct |
16 ms |
35704 KB |
Output is correct |
4 |
Correct |
16 ms |
35664 KB |
Output is correct |
5 |
Correct |
18 ms |
35676 KB |
Output is correct |
6 |
Correct |
16 ms |
35672 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
19 ms |
35676 KB |
Output is correct |
9 |
Correct |
18 ms |
35672 KB |
Output is correct |
10 |
Correct |
16 ms |
35676 KB |
Output is correct |
11 |
Correct |
15 ms |
35696 KB |
Output is correct |
12 |
Correct |
16 ms |
35676 KB |
Output is correct |
13 |
Correct |
15 ms |
35696 KB |
Output is correct |
14 |
Correct |
16 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35676 KB |
Output is correct |
16 |
Correct |
15 ms |
35676 KB |
Output is correct |
17 |
Correct |
21 ms |
35700 KB |
Output is correct |
18 |
Correct |
19 ms |
35672 KB |
Output is correct |
19 |
Correct |
22 ms |
35676 KB |
Output is correct |
20 |
Correct |
16 ms |
35672 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
18 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35672 KB |
Output is correct |
24 |
Correct |
16 ms |
35728 KB |
Output is correct |
25 |
Correct |
18 ms |
35672 KB |
Output is correct |
26 |
Correct |
22 ms |
36208 KB |
Output is correct |
27 |
Correct |
19 ms |
36184 KB |
Output is correct |
28 |
Correct |
18 ms |
36444 KB |
Output is correct |
29 |
Correct |
18 ms |
36188 KB |
Output is correct |
30 |
Correct |
17 ms |
36188 KB |
Output is correct |
31 |
Correct |
17 ms |
35676 KB |
Output is correct |
32 |
Correct |
17 ms |
36468 KB |
Output is correct |
33 |
Correct |
16 ms |
35676 KB |
Output is correct |
34 |
Correct |
17 ms |
36032 KB |
Output is correct |
35 |
Correct |
18 ms |
36188 KB |
Output is correct |
36 |
Correct |
17 ms |
36060 KB |
Output is correct |
37 |
Correct |
21 ms |
36188 KB |
Output is correct |
38 |
Correct |
15 ms |
35676 KB |
Output is correct |
39 |
Correct |
18 ms |
36184 KB |
Output is correct |
40 |
Correct |
19 ms |
36184 KB |
Output is correct |
41 |
Correct |
18 ms |
36188 KB |
Output is correct |
42 |
Correct |
17 ms |
36188 KB |
Output is correct |
43 |
Correct |
16 ms |
36308 KB |
Output is correct |
44 |
Correct |
15 ms |
35672 KB |
Output is correct |
45 |
Correct |
17 ms |
36184 KB |
Output is correct |
46 |
Correct |
17 ms |
36188 KB |
Output is correct |
47 |
Correct |
18 ms |
35676 KB |
Output is correct |
48 |
Correct |
18 ms |
36220 KB |
Output is correct |
49 |
Correct |
17 ms |
36184 KB |
Output is correct |
50 |
Correct |
18 ms |
36188 KB |
Output is correct |
51 |
Correct |
18 ms |
36188 KB |
Output is correct |
52 |
Correct |
17 ms |
36188 KB |
Output is correct |
53 |
Correct |
17 ms |
36188 KB |
Output is correct |
54 |
Correct |
19 ms |
36188 KB |
Output is correct |
55 |
Correct |
16 ms |
35672 KB |
Output is correct |
56 |
Correct |
90 ms |
52448 KB |
Output is correct |
57 |
Correct |
88 ms |
51808 KB |
Output is correct |
58 |
Correct |
21 ms |
36188 KB |
Output is correct |
59 |
Correct |
16 ms |
35676 KB |
Output is correct |
60 |
Correct |
15 ms |
35732 KB |
Output is correct |
61 |
Correct |
82 ms |
51668 KB |
Output is correct |
62 |
Correct |
17 ms |
36188 KB |
Output is correct |
63 |
Correct |
117 ms |
58964 KB |
Output is correct |
64 |
Correct |
96 ms |
51796 KB |
Output is correct |
65 |
Correct |
17 ms |
36188 KB |
Output is correct |
66 |
Correct |
96 ms |
52308 KB |
Output is correct |
67 |
Correct |
17 ms |
36068 KB |
Output is correct |
68 |
Correct |
17 ms |
36144 KB |
Output is correct |
69 |
Correct |
83 ms |
51952 KB |
Output is correct |
70 |
Correct |
17 ms |
36188 KB |
Output is correct |
71 |
Correct |
85 ms |
51600 KB |
Output is correct |
72 |
Correct |
107 ms |
54096 KB |
Output is correct |
73 |
Correct |
16 ms |
35676 KB |
Output is correct |
74 |
Correct |
119 ms |
59008 KB |
Output is correct |
75 |
Correct |
109 ms |
55592 KB |
Output is correct |
76 |
Correct |
17 ms |
36184 KB |
Output is correct |
77 |
Correct |
17 ms |
35996 KB |
Output is correct |
78 |
Correct |
72 ms |
52104 KB |
Output is correct |
79 |
Correct |
79 ms |
58056 KB |
Output is correct |
80 |
Correct |
21 ms |
36208 KB |
Output is correct |
81 |
Correct |
17 ms |
36216 KB |
Output is correct |
82 |
Correct |
16 ms |
35676 KB |
Output is correct |
83 |
Correct |
15 ms |
35676 KB |
Output is correct |
84 |
Correct |
17 ms |
36188 KB |
Output is correct |
85 |
Correct |
94 ms |
53328 KB |
Output is correct |
86 |
Correct |
17 ms |
36188 KB |
Output is correct |
87 |
Correct |
93 ms |
51960 KB |
Output is correct |
88 |
Correct |
17 ms |
35932 KB |
Output is correct |
89 |
Correct |
96 ms |
51796 KB |
Output is correct |
90 |
Correct |
95 ms |
53592 KB |
Output is correct |
91 |
Correct |
90 ms |
57936 KB |
Output is correct |
92 |
Correct |
77 ms |
52120 KB |
Output is correct |
93 |
Correct |
17 ms |
36100 KB |
Output is correct |
94 |
Correct |
16 ms |
35676 KB |
Output is correct |
95 |
Correct |
93 ms |
57416 KB |
Output is correct |
96 |
Correct |
103 ms |
61588 KB |
Output is correct |
97 |
Correct |
17 ms |
36184 KB |
Output is correct |
98 |
Correct |
17 ms |
35672 KB |
Output is correct |
99 |
Correct |
101 ms |
53824 KB |
Output is correct |
100 |
Correct |
17 ms |
36184 KB |
Output is correct |
101 |
Correct |
92 ms |
52560 KB |
Output is correct |
102 |
Correct |
98 ms |
60240 KB |
Output is correct |
103 |
Correct |
16 ms |
36188 KB |
Output is correct |
104 |
Correct |
22 ms |
36184 KB |
Output is correct |
105 |
Correct |
17 ms |
36184 KB |
Output is correct |
106 |
Correct |
17 ms |
36184 KB |
Output is correct |
107 |
Correct |
17 ms |
36188 KB |
Output is correct |
108 |
Correct |
461 ms |
125884 KB |
Output is correct |
109 |
Correct |
874 ms |
134120 KB |
Output is correct |
110 |
Correct |
859 ms |
145344 KB |
Output is correct |
111 |
Correct |
890 ms |
169748 KB |
Output is correct |
112 |
Correct |
722 ms |
154788 KB |
Output is correct |
113 |
Correct |
481 ms |
146220 KB |
Output is correct |
114 |
Correct |
591 ms |
116328 KB |
Output is correct |
115 |
Correct |
604 ms |
116156 KB |
Output is correct |
116 |
Correct |
691 ms |
120916 KB |
Output is correct |
117 |
Correct |
633 ms |
148312 KB |
Output is correct |
118 |
Correct |
541 ms |
116564 KB |
Output is correct |
119 |
Correct |
618 ms |
148304 KB |
Output is correct |
120 |
Correct |
774 ms |
161744 KB |
Output is correct |
121 |
Correct |
634 ms |
148300 KB |
Output is correct |
122 |
Correct |
704 ms |
128968 KB |
Output is correct |
123 |
Correct |
507 ms |
149436 KB |
Output is correct |
124 |
Correct |
771 ms |
140148 KB |
Output is correct |