#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=1e5+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;
}
assert(1==2);
}
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 |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
2 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
2 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5460 KB |
Output is correct |
27 |
Correct |
4 ms |
5460 KB |
Output is correct |
28 |
Correct |
4 ms |
5676 KB |
Output is correct |
29 |
Correct |
4 ms |
5584 KB |
Output is correct |
30 |
Correct |
6 ms |
5460 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5716 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5460 KB |
Output is correct |
35 |
Correct |
4 ms |
5588 KB |
Output is correct |
36 |
Correct |
4 ms |
5404 KB |
Output is correct |
37 |
Correct |
4 ms |
5428 KB |
Output is correct |
38 |
Correct |
2 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5460 KB |
Output is correct |
40 |
Correct |
4 ms |
5492 KB |
Output is correct |
41 |
Correct |
4 ms |
5444 KB |
Output is correct |
42 |
Correct |
4 ms |
5460 KB |
Output is correct |
43 |
Correct |
4 ms |
5604 KB |
Output is correct |
44 |
Correct |
3 ms |
5184 KB |
Output is correct |
45 |
Correct |
5 ms |
5588 KB |
Output is correct |
46 |
Correct |
5 ms |
5460 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
6 ms |
5460 KB |
Output is correct |
49 |
Correct |
4 ms |
5528 KB |
Output is correct |
50 |
Correct |
5 ms |
5588 KB |
Output is correct |
51 |
Correct |
4 ms |
5460 KB |
Output is correct |
52 |
Correct |
4 ms |
5460 KB |
Output is correct |
53 |
Correct |
4 ms |
5460 KB |
Output is correct |
54 |
Correct |
4 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
2 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
48 ms |
17720 KB |
Output is correct |
27 |
Correct |
71 ms |
17768 KB |
Output is correct |
28 |
Correct |
4 ms |
5460 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
102 ms |
17768 KB |
Output is correct |
32 |
Correct |
4 ms |
5460 KB |
Output is correct |
33 |
Correct |
105 ms |
22452 KB |
Output is correct |
34 |
Correct |
71 ms |
17816 KB |
Output is correct |
35 |
Correct |
4 ms |
5460 KB |
Output is correct |
36 |
Correct |
74 ms |
18388 KB |
Output is correct |
37 |
Correct |
5 ms |
5460 KB |
Output is correct |
38 |
Correct |
6 ms |
5460 KB |
Output is correct |
39 |
Correct |
56 ms |
17736 KB |
Output is correct |
40 |
Correct |
4 ms |
5588 KB |
Output is correct |
41 |
Correct |
66 ms |
17856 KB |
Output is correct |
42 |
Correct |
92 ms |
20776 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
120 ms |
21784 KB |
Output is correct |
45 |
Correct |
94 ms |
21632 KB |
Output is correct |
46 |
Correct |
4 ms |
5460 KB |
Output is correct |
47 |
Correct |
4 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
17728 KB |
Output is correct |
2 |
Correct |
56 ms |
20704 KB |
Output is correct |
3 |
Correct |
4 ms |
5460 KB |
Output is correct |
4 |
Correct |
4 ms |
5460 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5460 KB |
Output is correct |
8 |
Correct |
94 ms |
18860 KB |
Output is correct |
9 |
Correct |
4 ms |
5460 KB |
Output is correct |
10 |
Correct |
78 ms |
19456 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
12 |
Correct |
86 ms |
19396 KB |
Output is correct |
13 |
Correct |
99 ms |
20536 KB |
Output is correct |
14 |
Correct |
82 ms |
22096 KB |
Output is correct |
15 |
Correct |
50 ms |
19076 KB |
Output is correct |
16 |
Correct |
4 ms |
5588 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
66 ms |
22208 KB |
Output is correct |
19 |
Correct |
92 ms |
26300 KB |
Output is correct |
20 |
Correct |
4 ms |
5552 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 KB |
Output is correct |
22 |
Correct |
89 ms |
20752 KB |
Output is correct |
23 |
Correct |
5 ms |
5556 KB |
Output is correct |
24 |
Correct |
74 ms |
19692 KB |
Output is correct |
25 |
Correct |
79 ms |
25304 KB |
Output is correct |
26 |
Correct |
4 ms |
5588 KB |
Output is correct |
27 |
Correct |
5 ms |
5716 KB |
Output is correct |
28 |
Correct |
4 ms |
5464 KB |
Output is correct |
29 |
Correct |
4 ms |
5464 KB |
Output is correct |
30 |
Correct |
5 ms |
5532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
2 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5460 KB |
Output is correct |
27 |
Correct |
4 ms |
5460 KB |
Output is correct |
28 |
Correct |
4 ms |
5676 KB |
Output is correct |
29 |
Correct |
4 ms |
5584 KB |
Output is correct |
30 |
Correct |
6 ms |
5460 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5716 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5460 KB |
Output is correct |
35 |
Correct |
4 ms |
5588 KB |
Output is correct |
36 |
Correct |
4 ms |
5404 KB |
Output is correct |
37 |
Correct |
4 ms |
5428 KB |
Output is correct |
38 |
Correct |
2 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5460 KB |
Output is correct |
40 |
Correct |
4 ms |
5492 KB |
Output is correct |
41 |
Correct |
4 ms |
5444 KB |
Output is correct |
42 |
Correct |
4 ms |
5460 KB |
Output is correct |
43 |
Correct |
4 ms |
5604 KB |
Output is correct |
44 |
Correct |
3 ms |
5184 KB |
Output is correct |
45 |
Correct |
5 ms |
5588 KB |
Output is correct |
46 |
Correct |
5 ms |
5460 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
6 ms |
5460 KB |
Output is correct |
49 |
Correct |
4 ms |
5528 KB |
Output is correct |
50 |
Correct |
5 ms |
5588 KB |
Output is correct |
51 |
Correct |
4 ms |
5460 KB |
Output is correct |
52 |
Correct |
4 ms |
5460 KB |
Output is correct |
53 |
Correct |
4 ms |
5460 KB |
Output is correct |
54 |
Correct |
4 ms |
5460 KB |
Output is correct |
55 |
Correct |
2 ms |
5076 KB |
Output is correct |
56 |
Correct |
48 ms |
17720 KB |
Output is correct |
57 |
Correct |
71 ms |
17768 KB |
Output is correct |
58 |
Correct |
4 ms |
5460 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5076 KB |
Output is correct |
61 |
Correct |
102 ms |
17768 KB |
Output is correct |
62 |
Correct |
4 ms |
5460 KB |
Output is correct |
63 |
Correct |
105 ms |
22452 KB |
Output is correct |
64 |
Correct |
71 ms |
17816 KB |
Output is correct |
65 |
Correct |
4 ms |
5460 KB |
Output is correct |
66 |
Correct |
74 ms |
18388 KB |
Output is correct |
67 |
Correct |
5 ms |
5460 KB |
Output is correct |
68 |
Correct |
6 ms |
5460 KB |
Output is correct |
69 |
Correct |
56 ms |
17736 KB |
Output is correct |
70 |
Correct |
4 ms |
5588 KB |
Output is correct |
71 |
Correct |
66 ms |
17856 KB |
Output is correct |
72 |
Correct |
92 ms |
20776 KB |
Output is correct |
73 |
Correct |
3 ms |
5076 KB |
Output is correct |
74 |
Correct |
120 ms |
21784 KB |
Output is correct |
75 |
Correct |
94 ms |
21632 KB |
Output is correct |
76 |
Correct |
4 ms |
5460 KB |
Output is correct |
77 |
Correct |
4 ms |
5460 KB |
Output is correct |
78 |
Correct |
51 ms |
17728 KB |
Output is correct |
79 |
Correct |
56 ms |
20704 KB |
Output is correct |
80 |
Correct |
4 ms |
5460 KB |
Output is correct |
81 |
Correct |
4 ms |
5460 KB |
Output is correct |
82 |
Correct |
3 ms |
5076 KB |
Output is correct |
83 |
Correct |
3 ms |
5076 KB |
Output is correct |
84 |
Correct |
4 ms |
5460 KB |
Output is correct |
85 |
Correct |
94 ms |
18860 KB |
Output is correct |
86 |
Correct |
4 ms |
5460 KB |
Output is correct |
87 |
Correct |
78 ms |
19456 KB |
Output is correct |
88 |
Correct |
4 ms |
5076 KB |
Output is correct |
89 |
Correct |
86 ms |
19396 KB |
Output is correct |
90 |
Correct |
99 ms |
20536 KB |
Output is correct |
91 |
Correct |
82 ms |
22096 KB |
Output is correct |
92 |
Correct |
50 ms |
19076 KB |
Output is correct |
93 |
Correct |
4 ms |
5588 KB |
Output is correct |
94 |
Correct |
3 ms |
5076 KB |
Output is correct |
95 |
Correct |
66 ms |
22208 KB |
Output is correct |
96 |
Correct |
92 ms |
26300 KB |
Output is correct |
97 |
Correct |
4 ms |
5552 KB |
Output is correct |
98 |
Correct |
3 ms |
5024 KB |
Output is correct |
99 |
Correct |
89 ms |
20752 KB |
Output is correct |
100 |
Correct |
5 ms |
5556 KB |
Output is correct |
101 |
Correct |
74 ms |
19692 KB |
Output is correct |
102 |
Correct |
79 ms |
25304 KB |
Output is correct |
103 |
Correct |
4 ms |
5588 KB |
Output is correct |
104 |
Correct |
5 ms |
5716 KB |
Output is correct |
105 |
Correct |
4 ms |
5464 KB |
Output is correct |
106 |
Correct |
4 ms |
5464 KB |
Output is correct |
107 |
Correct |
5 ms |
5532 KB |
Output is correct |
108 |
Runtime error |
9 ms |
9944 KB |
Execution killed with signal 11 |
109 |
Halted |
0 ms |
0 KB |
- |