Submission #747568

# Submission time Handle Problem Language Result Execution time Memory
747568 2023-05-24T10:33:18 Z MilosMilutinovic Dynamite (POI11_dyn) C++14
100 / 100
946 ms 33008 KB
#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

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 time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 5 ms 7464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7360 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7364 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 6 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 8012 KB Output is correct
2 Correct 29 ms 8916 KB Output is correct
3 Correct 33 ms 9320 KB Output is correct
4 Correct 34 ms 10316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 10700 KB Output is correct
2 Correct 95 ms 11896 KB Output is correct
3 Correct 120 ms 12324 KB Output is correct
4 Correct 130 ms 13976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 13760 KB Output is correct
2 Correct 166 ms 13852 KB Output is correct
3 Correct 180 ms 13588 KB Output is correct
4 Correct 208 ms 17000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 575 ms 20244 KB Output is correct
2 Correct 473 ms 22592 KB Output is correct
3 Correct 726 ms 27596 KB Output is correct
4 Correct 725 ms 27664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 897 ms 28036 KB Output is correct
2 Correct 770 ms 27236 KB Output is correct
3 Correct 846 ms 29628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 946 ms 31624 KB Output is correct
2 Correct 713 ms 27256 KB Output is correct
3 Correct 931 ms 33008 KB Output is correct
4 Correct 384 ms 27648 KB Output is correct