#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) {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++;
if(dem<=m) return 1;
return 0;
}
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;
//cout<<check(mid)<<' ';
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;
int t=1;
//cin>>t;
while(t--)
solve();
}
Compilation message
dyn.cpp: In function 'void dfs(int, int, int)':
dyn.cpp:25:29: warning: statement has no effect [-Wunused-value]
25 | if(kt[v]==-1) {kt[u]==-1;return;}
| ~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 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 |
1100 KB |
Output is correct |
2 |
Correct |
26 ms |
2120 KB |
Output is correct |
3 |
Correct |
24 ms |
2640 KB |
Output is correct |
4 |
Correct |
43 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4488 KB |
Output is correct |
2 |
Correct |
111 ms |
6144 KB |
Output is correct |
3 |
Correct |
122 ms |
6716 KB |
Output is correct |
4 |
Correct |
117 ms |
10104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
8228 KB |
Output is correct |
2 |
Correct |
149 ms |
8304 KB |
Output is correct |
3 |
Correct |
170 ms |
8032 KB |
Output is correct |
4 |
Correct |
276 ms |
14436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
537 ms |
14996 KB |
Output is correct |
2 |
Correct |
579 ms |
17480 KB |
Output is correct |
3 |
Correct |
983 ms |
26608 KB |
Output is correct |
4 |
Correct |
1004 ms |
26488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1196 ms |
24464 KB |
Output is correct |
2 |
Correct |
775 ms |
21780 KB |
Output is correct |
3 |
Correct |
1208 ms |
27340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1085 ms |
32040 KB |
Output is correct |
2 |
Correct |
746 ms |
21836 KB |
Output is correct |
3 |
Correct |
1303 ms |
34016 KB |
Output is correct |
4 |
Correct |
306 ms |
21952 KB |
Output is correct |