Submission #747568

#TimeUsernameProblemLanguageResultExecution timeMemory
747568MilosMilutinovicUntitled (POI11_dyn)C++14
100 / 100
946 ms33008 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, m, a[N], d[N][2];
vector <int> g[N], ord;
bool was[N];
bool check(int f) {
    for (int i = 1; i <= n; i++) was[i] = false;
    int cnt = 0;
    for (int x : ord) {
        was[x] = true;
        d[x][0] = 1e9;
        d[x][1] = (a[x] == 1 ? 0 : -1e9);
        for (int y : g[x]) {
            if (!was[y]) continue;
            d[x][0] = min(d[x][0], d[y][0] + 1);
            d[x][1] = max(d[x][1], d[y][1] + 1);
        }
        if (d[x][0] + d[x][1] <= f) d[x][1] = -1e9;
        if (d[x][1] == f) cnt++, d[x][0] = 0, d[x][1] = -1e9;
    }
    if (d[1][1] >= 0) cnt++;
    return cnt <= m;
}
void dfs(int x, int fa) {
    for (int y : g[x]) if (y != fa) dfs(y, x);
    ord.push_back(x);
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i < n; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 1);
    int l = 0, r = 1e9, ans = 0;
    while (l <= r) {
        int mid = l + r >> 1;
        if (check(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    printf("%d\n", ans);
}

Compilation message (stderr)

dyn.cpp: In function 'int main()':
dyn.cpp:41:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   41 |         int mid = l + r >> 1;
      |                   ~~^~~
dyn.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
dyn.cpp:31:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |     for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
      |                                  ~~~~~^~~~~~~~~~~~~
dyn.cpp:34:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         scanf("%d%d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~
#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...