Submission #319202

#TimeUsernameProblemLanguageResultExecution timeMemory
319202VodkaInTheJarUntitled (POI11_dyn)C++14
100 / 100
1521 ms32696 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;

const int maxn = 3e5 + 3; 

int n, m; 
bool is[maxn];
vector <int> adj[maxn];
void read()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	cin >> is[i];
	
	for (int i = 1; i <= n-1; i++)
	{
		int a, b;
		cin >> a >> b;
		
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
}

int cnt;
int dfs(int ver, int par, int x)
{
	int cover = 0, need_to_cover = is[ver];
	for (auto i: adj[ver])
	if (i != par)
	{
		int curr = dfs(i, ver, x);
		if (curr > 0)
		cover = max(cover, curr);
		
		else 
		if (curr < 0)
		need_to_cover = max(need_to_cover, -curr);
	}
	
	if (need_to_cover >= x + 1)
	{
		cnt++;
		return x; 
	}

	if (cover >= need_to_cover)
	return max(0, cover - 1);
	
	else
	return -(need_to_cover + 1);
}

bool can(int x)
{
	cnt = 0;
	if (dfs(1, -1, x) < 0)
	cnt++;
    
    return cnt <= m; 	
}

void solve()
{
	int low = 0, high = n; 
	while (low < high)
	{
		int mid = (low + high) / 2;
		if (can(mid))
		high = mid;
		
		else 
		low = mid+1;
	}
	
	cout << low << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...