#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0) ; cin.tie(0);cout.tie(0)
#define ll long long
#define foru(i,l,r) for(int i=l;i<=r;i++)
#define ford(i,l,r) for(int i=l;i>=r;i--)
#define pb push_back
using namespace std;
const int N=3e5+10;
vector<int> d;
vector<vector<int>> ke;
int dp[N],dem,kt[N];
int n,m;
void dfs(int k,int u,int fa)
{
int mx1=-1e9,mx2=-1e9;
if(d[u]) mx1=0;
//if(k>m) return;
for(auto v:ke[u])
{
if(v==fa) continue;
//cout<<v<<' ';
dfs(k,v,u);
if(kt[v]==1) mx1=max(mx1,dp[v]+1);
if(kt[v]==2) mx2=max(mx2,dp[v]-1);
if(kt[v]==-1||dem>m) {kt[u]==-1;return;}
}
if(mx1>mx2)
{
if(mx1>=k)
{
++dem;
kt[u]=2;
dp[u]=k;
if(dem>m) kt[u]=-1;
}
else kt[u]=1,dp[u]=mx1;
}
else kt[u]=2,dp[u]=mx2;
}
bool check(int k)
{
dem=0;
dfs(k,1,-1);
if(kt[1]==-1) return 0;
if(kt[1]==1&&dp[1]>=0) ++dem;
return dem<=m;
}
void solve()
{
cin>>n>>m;
d.resize(n+1);
ke.resize(n+1);
foru(i,1,n) cin>>d[i];
foru(i,1,n-1)
{
int u,v;
cin>>u>>v;
ke[u].pb(v);
ke[v].pb(u);
}
//for(auto v:ke[5]) cout<<v<<' ';
int l=0,r=n,ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans;
}
int main()
{
//freopen("main.inp","r",stdin);
//freopen("main.out","w",stdout);
fast;
solve();
}
Compilation message
dyn.cpp: In function 'void dfs(int, int, int)':
dyn.cpp:25:36: warning: statement has no effect [-Wunused-value]
25 | if(kt[v]==-1||dem>m) {kt[u]==-1;return;}
| ~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
284 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
972 KB |
Output is correct |
2 |
Correct |
26 ms |
1876 KB |
Output is correct |
3 |
Correct |
24 ms |
2252 KB |
Output is correct |
4 |
Correct |
38 ms |
4388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
3916 KB |
Output is correct |
2 |
Correct |
68 ms |
5196 KB |
Output is correct |
3 |
Correct |
179 ms |
5692 KB |
Output is correct |
4 |
Correct |
137 ms |
9084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
7200 KB |
Output is correct |
2 |
Correct |
128 ms |
7300 KB |
Output is correct |
3 |
Correct |
207 ms |
7004 KB |
Output is correct |
4 |
Correct |
290 ms |
13280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
497 ms |
13964 KB |
Output is correct |
2 |
Correct |
585 ms |
16364 KB |
Output is correct |
3 |
Correct |
990 ms |
25900 KB |
Output is correct |
4 |
Correct |
1025 ms |
25924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1230 ms |
23436 KB |
Output is correct |
2 |
Correct |
841 ms |
21192 KB |
Output is correct |
3 |
Correct |
1210 ms |
26752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1120 ms |
31008 KB |
Output is correct |
2 |
Correct |
811 ms |
21192 KB |
Output is correct |
3 |
Correct |
1298 ms |
33412 KB |
Output is correct |
4 |
Correct |
354 ms |
21328 KB |
Output is correct |