#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;
for(auto v:ke[u])
if(v!=fa)
{
//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(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;
}
}
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;
int 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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 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 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
972 KB |
Output is correct |
2 |
Correct |
22 ms |
1868 KB |
Output is correct |
3 |
Incorrect |
21 ms |
2244 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
3912 KB |
Output is correct |
2 |
Correct |
66 ms |
5208 KB |
Output is correct |
3 |
Incorrect |
109 ms |
5692 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
7204 KB |
Output is correct |
2 |
Correct |
175 ms |
7300 KB |
Output is correct |
3 |
Incorrect |
149 ms |
6964 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
644 ms |
13948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1202 ms |
22936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1185 ms |
29232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |