#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define F first
#define S second
#define err(x) cerr<<#x<<" : "<<x<<'\n';
#define dmid int mid=(r+l)/2
#define lc 2*id
#define rc 2*id+1
#define pb push_back
#define all(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
using namespace std;
const int N=5e5+4;
const int lg=20;
vector<int> adj[N];
vector<int> vc[N];
int par[lg][N];
int h[N];
bool ok[N];
int dp[N],up[N];
int n,k;
void dfs(int v,int p){
for(int u:adj[v]) if(u!=p){
dfs(u,v);
dp[v]+=dp[u];
maxm(up[v],up[u]-1);
}
ok[v]=(up[v]>0);
if(dp[v]==0&&v!=p&&!ok[v]){
dp[v]=1;
}
}
void DFS(int v,int p){
h[v]=h[p]+1;
par[0][v]=p;
for(int u:adj[v]) if(u!=p) DFS(u,v);
}
int PAR(int v,int kk){
while(kk>0){
v=par[__builtin_ctz(kk)][v];
kk-=(kk&(-kk));
}
return v;
}
int LCA(int u,int v){
if(h[u]>h[v]) swap(u,v);
v=PAR(v,h[v]-h[u]);
if(v==u) return v;
FORR(j,lg-1,0) if(par[j][u]!=par[j][v]){
u=par[j][u];
v=par[j][v];
}
return (par[0][v]);
}
int main(){
iostream::sync_with_stdio(0); cin.tie(0);
cin>>n>>k;
FOR(_,1,n-1){
int u,v; cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
if(n<=2){
int w0,w1; cin>>w0;
if(n==1){
cout<<"0\n";
return 0;
}
if(n==2){
cin>>w1;
cout<<(w0!=w1)<<'\n';
return 0;
}
}
FOR(i,1,n){
int w; cin>>w;
vc[w].pb(i);
}
int root=1;
FOR(i,1,n) if(SZ(adj[i])>1) root=i;
DFS(root,root);
FOR(j,1,lg-1) FOR(i,1,n) par[j][i]=par[j-1][par[j-1][i]];
FOR(i,1,k) if(!vc[i].empty()){
int H=h[vc[i][0]];
FOR(j,1,SZ(vc[i])-1) minm(H,h[LCA(vc[i][j],vc[i][j-1])]);
for(int v:vc[i]) up[v]=h[v]-H;
}
dfs(root,root);
FOR(i,1,n) if(i!=root&&!ok[i]&&dp[i]==dp[root]){
dp[root]++;
break;
}
cout<<(dp[root]/2+(dp[root]&1))<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23892 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23712 KB |
Output is correct |
8 |
Correct |
13 ms |
23936 KB |
Output is correct |
9 |
Correct |
13 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23988 KB |
Output is correct |
11 |
Correct |
14 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23920 KB |
Output is correct |
13 |
Correct |
11 ms |
23864 KB |
Output is correct |
14 |
Correct |
11 ms |
23884 KB |
Output is correct |
15 |
Correct |
12 ms |
23988 KB |
Output is correct |
16 |
Correct |
11 ms |
23892 KB |
Output is correct |
17 |
Correct |
14 ms |
23856 KB |
Output is correct |
18 |
Correct |
11 ms |
23892 KB |
Output is correct |
19 |
Correct |
15 ms |
23924 KB |
Output is correct |
20 |
Correct |
13 ms |
23928 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
11 ms |
23900 KB |
Output is correct |
23 |
Correct |
11 ms |
23876 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23892 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23712 KB |
Output is correct |
8 |
Correct |
13 ms |
23936 KB |
Output is correct |
9 |
Correct |
13 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23988 KB |
Output is correct |
11 |
Correct |
14 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23920 KB |
Output is correct |
13 |
Correct |
11 ms |
23864 KB |
Output is correct |
14 |
Correct |
11 ms |
23884 KB |
Output is correct |
15 |
Correct |
12 ms |
23988 KB |
Output is correct |
16 |
Correct |
11 ms |
23892 KB |
Output is correct |
17 |
Correct |
14 ms |
23856 KB |
Output is correct |
18 |
Correct |
11 ms |
23892 KB |
Output is correct |
19 |
Correct |
15 ms |
23924 KB |
Output is correct |
20 |
Correct |
13 ms |
23928 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
11 ms |
23900 KB |
Output is correct |
23 |
Correct |
11 ms |
23876 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
11 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
24296 KB |
Output is correct |
27 |
Correct |
13 ms |
24276 KB |
Output is correct |
28 |
Correct |
13 ms |
24532 KB |
Output is correct |
29 |
Correct |
14 ms |
24328 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
12 ms |
23892 KB |
Output is correct |
32 |
Correct |
15 ms |
24580 KB |
Output is correct |
33 |
Correct |
13 ms |
23888 KB |
Output is correct |
34 |
Correct |
13 ms |
24268 KB |
Output is correct |
35 |
Correct |
13 ms |
24336 KB |
Output is correct |
36 |
Correct |
13 ms |
24248 KB |
Output is correct |
37 |
Correct |
14 ms |
24400 KB |
Output is correct |
38 |
Correct |
12 ms |
24020 KB |
Output is correct |
39 |
Correct |
14 ms |
24400 KB |
Output is correct |
40 |
Correct |
14 ms |
24380 KB |
Output is correct |
41 |
Correct |
13 ms |
24416 KB |
Output is correct |
42 |
Correct |
14 ms |
24404 KB |
Output is correct |
43 |
Correct |
14 ms |
24532 KB |
Output is correct |
44 |
Correct |
11 ms |
23992 KB |
Output is correct |
45 |
Correct |
13 ms |
24364 KB |
Output is correct |
46 |
Correct |
13 ms |
24368 KB |
Output is correct |
47 |
Correct |
12 ms |
23916 KB |
Output is correct |
48 |
Correct |
13 ms |
24388 KB |
Output is correct |
49 |
Correct |
14 ms |
24344 KB |
Output is correct |
50 |
Correct |
13 ms |
24528 KB |
Output is correct |
51 |
Correct |
16 ms |
24348 KB |
Output is correct |
52 |
Correct |
15 ms |
24276 KB |
Output is correct |
53 |
Correct |
14 ms |
24360 KB |
Output is correct |
54 |
Correct |
15 ms |
24296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23892 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23712 KB |
Output is correct |
8 |
Correct |
13 ms |
23936 KB |
Output is correct |
9 |
Correct |
13 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23988 KB |
Output is correct |
11 |
Correct |
14 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23920 KB |
Output is correct |
13 |
Correct |
11 ms |
23864 KB |
Output is correct |
14 |
Correct |
11 ms |
23884 KB |
Output is correct |
15 |
Correct |
12 ms |
23988 KB |
Output is correct |
16 |
Correct |
11 ms |
23892 KB |
Output is correct |
17 |
Correct |
14 ms |
23856 KB |
Output is correct |
18 |
Correct |
11 ms |
23892 KB |
Output is correct |
19 |
Correct |
15 ms |
23924 KB |
Output is correct |
20 |
Correct |
13 ms |
23928 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
11 ms |
23900 KB |
Output is correct |
23 |
Correct |
11 ms |
23876 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
12 ms |
23896 KB |
Output is correct |
26 |
Correct |
61 ms |
36712 KB |
Output is correct |
27 |
Correct |
88 ms |
36700 KB |
Output is correct |
28 |
Correct |
13 ms |
24276 KB |
Output is correct |
29 |
Correct |
12 ms |
23904 KB |
Output is correct |
30 |
Correct |
12 ms |
23892 KB |
Output is correct |
31 |
Correct |
85 ms |
36760 KB |
Output is correct |
32 |
Correct |
13 ms |
24276 KB |
Output is correct |
33 |
Correct |
124 ms |
41360 KB |
Output is correct |
34 |
Correct |
82 ms |
36856 KB |
Output is correct |
35 |
Correct |
14 ms |
24276 KB |
Output is correct |
36 |
Correct |
92 ms |
37432 KB |
Output is correct |
37 |
Correct |
16 ms |
24384 KB |
Output is correct |
38 |
Correct |
13 ms |
24320 KB |
Output is correct |
39 |
Correct |
59 ms |
36628 KB |
Output is correct |
40 |
Correct |
13 ms |
24532 KB |
Output is correct |
41 |
Correct |
75 ms |
36812 KB |
Output is correct |
42 |
Correct |
110 ms |
39676 KB |
Output is correct |
43 |
Correct |
12 ms |
23892 KB |
Output is correct |
44 |
Correct |
125 ms |
40784 KB |
Output is correct |
45 |
Correct |
105 ms |
40620 KB |
Output is correct |
46 |
Correct |
13 ms |
24388 KB |
Output is correct |
47 |
Correct |
14 ms |
24348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
36700 KB |
Output is correct |
2 |
Correct |
65 ms |
39604 KB |
Output is correct |
3 |
Correct |
14 ms |
24308 KB |
Output is correct |
4 |
Correct |
14 ms |
24276 KB |
Output is correct |
5 |
Correct |
13 ms |
23916 KB |
Output is correct |
6 |
Correct |
12 ms |
23944 KB |
Output is correct |
7 |
Correct |
16 ms |
24276 KB |
Output is correct |
8 |
Correct |
104 ms |
37876 KB |
Output is correct |
9 |
Correct |
14 ms |
24288 KB |
Output is correct |
10 |
Correct |
86 ms |
36904 KB |
Output is correct |
11 |
Correct |
12 ms |
23904 KB |
Output is correct |
12 |
Correct |
85 ms |
36828 KB |
Output is correct |
13 |
Correct |
97 ms |
37780 KB |
Output is correct |
14 |
Correct |
86 ms |
39416 KB |
Output is correct |
15 |
Correct |
57 ms |
36608 KB |
Output is correct |
16 |
Correct |
13 ms |
24328 KB |
Output is correct |
17 |
Correct |
11 ms |
23892 KB |
Output is correct |
18 |
Correct |
68 ms |
39360 KB |
Output is correct |
19 |
Correct |
98 ms |
43424 KB |
Output is correct |
20 |
Correct |
15 ms |
24440 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
83 ms |
38076 KB |
Output is correct |
23 |
Correct |
13 ms |
24380 KB |
Output is correct |
24 |
Correct |
80 ms |
37056 KB |
Output is correct |
25 |
Correct |
84 ms |
42448 KB |
Output is correct |
26 |
Correct |
14 ms |
24420 KB |
Output is correct |
27 |
Correct |
14 ms |
24540 KB |
Output is correct |
28 |
Correct |
13 ms |
24384 KB |
Output is correct |
29 |
Correct |
13 ms |
24276 KB |
Output is correct |
30 |
Correct |
13 ms |
24404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23892 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23712 KB |
Output is correct |
8 |
Correct |
13 ms |
23936 KB |
Output is correct |
9 |
Correct |
13 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23988 KB |
Output is correct |
11 |
Correct |
14 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23920 KB |
Output is correct |
13 |
Correct |
11 ms |
23864 KB |
Output is correct |
14 |
Correct |
11 ms |
23884 KB |
Output is correct |
15 |
Correct |
12 ms |
23988 KB |
Output is correct |
16 |
Correct |
11 ms |
23892 KB |
Output is correct |
17 |
Correct |
14 ms |
23856 KB |
Output is correct |
18 |
Correct |
11 ms |
23892 KB |
Output is correct |
19 |
Correct |
15 ms |
23924 KB |
Output is correct |
20 |
Correct |
13 ms |
23928 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
11 ms |
23900 KB |
Output is correct |
23 |
Correct |
11 ms |
23876 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
11 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
24296 KB |
Output is correct |
27 |
Correct |
13 ms |
24276 KB |
Output is correct |
28 |
Correct |
13 ms |
24532 KB |
Output is correct |
29 |
Correct |
14 ms |
24328 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
12 ms |
23892 KB |
Output is correct |
32 |
Correct |
15 ms |
24580 KB |
Output is correct |
33 |
Correct |
13 ms |
23888 KB |
Output is correct |
34 |
Correct |
13 ms |
24268 KB |
Output is correct |
35 |
Correct |
13 ms |
24336 KB |
Output is correct |
36 |
Correct |
13 ms |
24248 KB |
Output is correct |
37 |
Correct |
14 ms |
24400 KB |
Output is correct |
38 |
Correct |
12 ms |
24020 KB |
Output is correct |
39 |
Correct |
14 ms |
24400 KB |
Output is correct |
40 |
Correct |
14 ms |
24380 KB |
Output is correct |
41 |
Correct |
13 ms |
24416 KB |
Output is correct |
42 |
Correct |
14 ms |
24404 KB |
Output is correct |
43 |
Correct |
14 ms |
24532 KB |
Output is correct |
44 |
Correct |
11 ms |
23992 KB |
Output is correct |
45 |
Correct |
13 ms |
24364 KB |
Output is correct |
46 |
Correct |
13 ms |
24368 KB |
Output is correct |
47 |
Correct |
12 ms |
23916 KB |
Output is correct |
48 |
Correct |
13 ms |
24388 KB |
Output is correct |
49 |
Correct |
14 ms |
24344 KB |
Output is correct |
50 |
Correct |
13 ms |
24528 KB |
Output is correct |
51 |
Correct |
16 ms |
24348 KB |
Output is correct |
52 |
Correct |
15 ms |
24276 KB |
Output is correct |
53 |
Correct |
14 ms |
24360 KB |
Output is correct |
54 |
Correct |
15 ms |
24296 KB |
Output is correct |
55 |
Correct |
12 ms |
23896 KB |
Output is correct |
56 |
Correct |
61 ms |
36712 KB |
Output is correct |
57 |
Correct |
88 ms |
36700 KB |
Output is correct |
58 |
Correct |
13 ms |
24276 KB |
Output is correct |
59 |
Correct |
12 ms |
23904 KB |
Output is correct |
60 |
Correct |
12 ms |
23892 KB |
Output is correct |
61 |
Correct |
85 ms |
36760 KB |
Output is correct |
62 |
Correct |
13 ms |
24276 KB |
Output is correct |
63 |
Correct |
124 ms |
41360 KB |
Output is correct |
64 |
Correct |
82 ms |
36856 KB |
Output is correct |
65 |
Correct |
14 ms |
24276 KB |
Output is correct |
66 |
Correct |
92 ms |
37432 KB |
Output is correct |
67 |
Correct |
16 ms |
24384 KB |
Output is correct |
68 |
Correct |
13 ms |
24320 KB |
Output is correct |
69 |
Correct |
59 ms |
36628 KB |
Output is correct |
70 |
Correct |
13 ms |
24532 KB |
Output is correct |
71 |
Correct |
75 ms |
36812 KB |
Output is correct |
72 |
Correct |
110 ms |
39676 KB |
Output is correct |
73 |
Correct |
12 ms |
23892 KB |
Output is correct |
74 |
Correct |
125 ms |
40784 KB |
Output is correct |
75 |
Correct |
105 ms |
40620 KB |
Output is correct |
76 |
Correct |
13 ms |
24388 KB |
Output is correct |
77 |
Correct |
14 ms |
24348 KB |
Output is correct |
78 |
Correct |
61 ms |
36700 KB |
Output is correct |
79 |
Correct |
65 ms |
39604 KB |
Output is correct |
80 |
Correct |
14 ms |
24308 KB |
Output is correct |
81 |
Correct |
14 ms |
24276 KB |
Output is correct |
82 |
Correct |
13 ms |
23916 KB |
Output is correct |
83 |
Correct |
12 ms |
23944 KB |
Output is correct |
84 |
Correct |
16 ms |
24276 KB |
Output is correct |
85 |
Correct |
104 ms |
37876 KB |
Output is correct |
86 |
Correct |
14 ms |
24288 KB |
Output is correct |
87 |
Correct |
86 ms |
36904 KB |
Output is correct |
88 |
Correct |
12 ms |
23904 KB |
Output is correct |
89 |
Correct |
85 ms |
36828 KB |
Output is correct |
90 |
Correct |
97 ms |
37780 KB |
Output is correct |
91 |
Correct |
86 ms |
39416 KB |
Output is correct |
92 |
Correct |
57 ms |
36608 KB |
Output is correct |
93 |
Correct |
13 ms |
24328 KB |
Output is correct |
94 |
Correct |
11 ms |
23892 KB |
Output is correct |
95 |
Correct |
68 ms |
39360 KB |
Output is correct |
96 |
Correct |
98 ms |
43424 KB |
Output is correct |
97 |
Correct |
15 ms |
24440 KB |
Output is correct |
98 |
Correct |
12 ms |
23892 KB |
Output is correct |
99 |
Correct |
83 ms |
38076 KB |
Output is correct |
100 |
Correct |
13 ms |
24380 KB |
Output is correct |
101 |
Correct |
80 ms |
37056 KB |
Output is correct |
102 |
Correct |
84 ms |
42448 KB |
Output is correct |
103 |
Correct |
14 ms |
24420 KB |
Output is correct |
104 |
Correct |
14 ms |
24540 KB |
Output is correct |
105 |
Correct |
13 ms |
24384 KB |
Output is correct |
106 |
Correct |
13 ms |
24276 KB |
Output is correct |
107 |
Correct |
13 ms |
24404 KB |
Output is correct |
108 |
Correct |
455 ms |
94864 KB |
Output is correct |
109 |
Correct |
909 ms |
103244 KB |
Output is correct |
110 |
Correct |
963 ms |
117572 KB |
Output is correct |
111 |
Correct |
801 ms |
132652 KB |
Output is correct |
112 |
Correct |
695 ms |
120320 KB |
Output is correct |
113 |
Correct |
444 ms |
102552 KB |
Output is correct |
114 |
Correct |
598 ms |
95152 KB |
Output is correct |
115 |
Correct |
629 ms |
95168 KB |
Output is correct |
116 |
Correct |
739 ms |
97168 KB |
Output is correct |
117 |
Correct |
625 ms |
107700 KB |
Output is correct |
118 |
Correct |
653 ms |
95352 KB |
Output is correct |
119 |
Correct |
658 ms |
107652 KB |
Output is correct |
120 |
Correct |
675 ms |
125260 KB |
Output is correct |
121 |
Correct |
658 ms |
107704 KB |
Output is correct |
122 |
Correct |
706 ms |
101808 KB |
Output is correct |
123 |
Correct |
449 ms |
109160 KB |
Output is correct |
124 |
Correct |
726 ms |
106248 KB |
Output is correct |