Submission #895595

# Submission time Handle Problem Language Result Execution time Memory
895595 2023-12-30T10:47:14 Z d4xn Tax Evasion (LMIO19_mokesciai) C++17
32 / 100
76 ms 47700 KB
#include <bits/stdc++.h>
using namespace std;

//#define int long long

const int N = 2e5+5/*1e6+6*/, inf = INT_MAX;

int n, m, sz, ans, dol[N], up[N], dep[N], par[N], seg[2][N*4];
vector<int> path, leafs, lvl[N], adj[N];
pair<int, int> sg[N];

void build(int p=1, int l=0, int r=sz-1) {
  if (l == r) {
    seg[0][p] = leafs[l];
    seg[1][p] = l;
    return;
  }

  int mid = (l+r)/2;
  build(p*2, l, mid);
  build(p*2+1, mid+1, r);

  seg[0][p] = max(seg[0][p*2], seg[0][p*2+1]);
  seg[1][p] = (seg[0][p] == seg[0][p*2] ? seg[1][p*2] : seg[1][p*2+1]);
}

void update(int x, int p=1, int l=0, int r=sz-1) {
  if (l == r) {
    seg[0][p]--;
    return;
  }

  int mid = (l+r)/2;
  if (x <= mid) update(x, p*2, l, mid);
  else update(x, p*2+1, mid+1, r);

  seg[0][p] = max(seg[0][p*2], seg[0][p*2+1]);
  seg[1][p] = (seg[0][p] == seg[0][p*2] ? seg[1][p*2] : seg[1][p*2+1]);
}

pair<int, int> query(int x, int y, int p=1, int l=0, int r=sz-1) {
  if (x <= l && r <= y) {
    return make_pair(seg[0][p], seg[1][p]);
  }

  int mid = (l+r)/2;

  if (y <= mid) {
    return query(x, y, p*2, l, mid);
  }
  else if (x >= mid+1) {
    return query(x, y, p*2+1, mid+1, r);
  }
  else {
    pair<int, int> pr1 = query(x, y, p*2, l, mid);
    pair<int, int> pr2 = query(x, y, p*2+1, mid+1, r);

    if (pr1.first > pr2.first) return pr1;
    else return pr2;
  }
}

pair<int, int> dfs(int x, int u, int d) {
  up[x] = u;
  dep[x] = d;
  lvl[d].push_back(x);

  path.push_back(x);
  if (dol[x] && x) {
    dol[x]--;

    int sz = path.size();
    dol[path[sz - sz/2]]++;
  }

  if (adj[x].size() == 1 && x) {
    leafs.push_back(dep[x]);
    sg[x] = make_pair(sz, sz);
    sz++;

    path.pop_back();
    return sg[x];
  }

  int mn = inf;
  int mx = -inf;
  for (int& y : adj[x]) {
    if (y == u) continue;
    pair<int, int> child = dfs(y, x, d+1);
    mn = min(mn, child.first);
    mx = max(mx, child.second);
  }

  path.pop_back();
  return sg[x] = make_pair(mn, mx);
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  sz = 0;
  ans = inf;
  memset(dol, 0, sizeof(dol));

  cin >> n >> m;
  for (int i = 1; i < n; i++) {
    int x;
    cin >> x;
    x--;
    adj[x].push_back(i);
    adj[i].push_back(x);
  }

  bool ok = 1;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    x--;
    dol[x]++;

    if (!x) ok = 0;
  }
  if (n == 1 || !ok) {
    cout << "1\n";
    return 0;
  }

  dfs(0, 0, 1);

  build();
  for (int i = n; i >= 1; i--) {
    for (int& x : lvl[i]) {
      while (dol[x] > 0) {
        pair<int, int> pr = query(sg[x].first, sg[x].second);
        dol[x]--;
        ans = min(ans, pr.first);
        update(pr.second);
      }
    }
  }
  cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 17500 KB Output is correct
2 Correct 3 ms 17500 KB Output is correct
3 Correct 4 ms 18268 KB Output is correct
4 Correct 5 ms 18364 KB Output is correct
5 Correct 4 ms 18268 KB Output is correct
6 Correct 52 ms 47560 KB Output is correct
7 Correct 49 ms 47608 KB Output is correct
8 Correct 4 ms 18268 KB Output is correct
9 Correct 60 ms 47572 KB Output is correct
10 Correct 76 ms 47640 KB Output is correct
11 Correct 56 ms 47700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 47560 KB Output is correct
2 Correct 49 ms 47608 KB Output is correct
3 Correct 4 ms 18268 KB Output is correct
4 Correct 3 ms 17496 KB Output is correct
5 Correct 4 ms 18012 KB Output is correct
6 Correct 4 ms 18104 KB Output is correct
7 Correct 4 ms 17956 KB Output is correct
8 Correct 4 ms 18012 KB Output is correct
9 Correct 5 ms 17756 KB Output is correct
10 Correct 43 ms 37384 KB Output is correct
11 Correct 42 ms 37416 KB Output is correct
12 Correct 68 ms 29156 KB Output is correct
13 Correct 50 ms 38604 KB Output is correct
14 Correct 36 ms 28496 KB Output is correct
15 Correct 53 ms 29056 KB Output is correct
16 Correct 56 ms 38600 KB Output is correct
17 Correct 61 ms 28752 KB Output is correct
18 Correct 47 ms 38444 KB Output is correct
19 Correct 41 ms 27484 KB Output is correct
20 Correct 45 ms 26192 KB Output is correct
21 Correct 39 ms 26120 KB Output is correct
22 Correct 40 ms 26124 KB Output is correct
23 Correct 41 ms 26204 KB Output is correct
24 Correct 40 ms 25860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 17500 KB Output is correct
2 Correct 3 ms 17500 KB Output is correct
3 Correct 4 ms 18268 KB Output is correct
4 Correct 5 ms 18364 KB Output is correct
5 Correct 4 ms 18268 KB Output is correct
6 Correct 3 ms 17496 KB Output is correct
7 Correct 4 ms 18012 KB Output is correct
8 Correct 4 ms 18104 KB Output is correct
9 Correct 4 ms 17956 KB Output is correct
10 Correct 4 ms 18012 KB Output is correct
11 Correct 5 ms 17756 KB Output is correct
12 Correct 4 ms 18268 KB Output is correct
13 Correct 3 ms 17500 KB Output is correct
14 Correct 3 ms 17656 KB Output is correct
15 Correct 3 ms 17500 KB Output is correct
16 Correct 3 ms 17500 KB Output is correct
17 Correct 3 ms 17500 KB Output is correct
18 Correct 4 ms 18012 KB Output is correct
19 Correct 4 ms 18012 KB Output is correct
20 Correct 5 ms 18012 KB Output is correct
21 Correct 4 ms 17936 KB Output is correct
22 Correct 4 ms 17756 KB Output is correct
23 Incorrect 4 ms 18012 KB Output isn't correct
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 17500 KB Output is correct
2 Correct 3 ms 17656 KB Output is correct
3 Correct 3 ms 17500 KB Output is correct
4 Correct 3 ms 17500 KB Output is correct
5 Correct 3 ms 17500 KB Output is correct
6 Correct 4 ms 18012 KB Output is correct
7 Correct 4 ms 18012 KB Output is correct
8 Correct 5 ms 18012 KB Output is correct
9 Correct 4 ms 17936 KB Output is correct
10 Correct 4 ms 17756 KB Output is correct
11 Incorrect 4 ms 18012 KB Output isn't correct
12 Halted 0 ms 0 KB -