Submission #319202

# Submission time Handle Problem Language Result Execution time Memory
319202 2020-11-04T13:46:26 Z VodkaInTheJar Dynamite (POI11_dyn) C++14
100 / 100
1521 ms 32696 KB
#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 time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 7916 KB Output is correct
2 Correct 29 ms 8428 KB Output is correct
3 Correct 34 ms 8684 KB Output is correct
4 Correct 41 ms 10468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 9444 KB Output is correct
2 Correct 135 ms 10596 KB Output is correct
3 Correct 171 ms 10468 KB Output is correct
4 Correct 190 ms 13412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 11236 KB Output is correct
2 Correct 210 ms 11620 KB Output is correct
3 Correct 286 ms 11364 KB Output is correct
4 Correct 397 ms 16612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 689 ms 14568 KB Output is correct
2 Correct 781 ms 15932 KB Output is correct
3 Correct 1167 ms 26740 KB Output is correct
4 Correct 1159 ms 26748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1402 ms 20068 KB Output is correct
2 Correct 1102 ms 22628 KB Output is correct
3 Correct 1521 ms 27108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1291 ms 26464 KB Output is correct
2 Correct 1078 ms 18404 KB Output is correct
3 Correct 1509 ms 32696 KB Output is correct
4 Correct 439 ms 23012 KB Output is correct