Submission #319185

#TimeUsernameProblemLanguageResultExecution timeMemory
319185VodkaInTheJarUntitled (POI11_dyn)C++14
0 / 100
144 ms612 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") #define endl '\n' using namespace std; const int maxn = 1003; int n, m; bool is[maxn]; vector <pair <int, 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({0, b}); adj[b].push_back({0, a}); } } int depth[maxn], max_depth[maxn]; void pre_dfs(int ver, int par) { max_depth[ver] = -1; if (is[ver]) max_depth[ver] = depth[ver]; for (auto i: adj[ver]) if (i.second != par) { depth[i.second] = depth[ver] + 1; pre_dfs(i.second, ver); max_depth[ver] = max(max_depth[ver], max_depth[i.second]); } } int cnt; int dfs(int ver, int par, int cover, int x) { if (max_depth[ver] - depth[ver] <= cover) return 0; if (max_depth[ver] - depth[ver] <= x) { cnt++; return x; } int curr_cover = cover; for (auto i: adj[ver]) if (i.second != par) curr_cover = max(curr_cover, dfs(i.second, ver, max(0, curr_cover - 1), x)); if (curr_cover == 0 && is[ver]) { cnt++; return x; } return curr_cover - 1; } bool can(int x) { cnt = 0; dfs(1, -1, 0, x); return cnt <= m; } void solve() { depth[1] = 1; pre_dfs(1, -1); for (int i = 1; i <= n; i++) { for (auto &j: adj[i]) j.first = max_depth[j.second]; sort (adj[i].begin(), adj[i].end()); reverse(adj[i].begin(), adj[i].end()); } 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...