답안 #895605

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895605 2023-12-30T11:22:27 Z vjudge1 Tax Evasion (LMIO19_mokesciai) C++17
32 / 100
122 ms 52480 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5+5, inf = INT_MAX;

int n, m, sz, ans, dol[N], up[N], dep[N];
vector<int> path, adj[N];
bitset<N> vis;

void dfs3(int x, set<pair<int, int>>& st, int d) {
  if (d) {
    if (vis[x]) return;
    vis[x] = 1;

    auto it = st.find(make_pair(-dep[x], x));
    if (it != st.end()) st.erase(it);
  }

  for (int& y : adj[x]) {
    if (y == up[x]) continue;
    dfs3(y, st, d+1);
  }
}

set<pair<int, int>> dfs2(int x) {
  set<pair<int, int>> st;
  if (adj[x].size() == 1 && x) {
    st.insert(make_pair(-dep[x], x));
  }

  for (int& y : adj[x]) {
    if (y == up[x]) continue;

    set<pair<int, int>> st2 = dfs2(y);
    if (st2.size() > st.size()) swap(st, st2);
    
    for (pair<int, int> p : st2) {
      st.insert(p);
    }
    st2.clear();
  }

  while (dol[x] > 0) {
    dol[x]--;    
    
    auto it = st.begin();
    int d = it->first;
    int y = it->second;
    st.erase(it);

    ans = min(ans, -d);
    dfs3(up[y], st, 0);
    st.insert(make_pair(d+1, up[y]));
  }
  return st;
}

void dfs(int x, int u, int d) {
  up[x] = u;
  dep[x] = d;

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

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

  for (int& y : adj[x]) {
    if (y == u) continue;
    dfs(y, x, d+1);
  }

  path.pop_back();
}

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);
  dfs2(0);

  cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 3 ms 8540 KB Output is correct
6 Correct 51 ms 52040 KB Output is correct
7 Correct 52 ms 52168 KB Output is correct
8 Correct 3 ms 8536 KB Output is correct
9 Correct 68 ms 52016 KB Output is correct
10 Correct 75 ms 52268 KB Output is correct
11 Correct 69 ms 52480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 52040 KB Output is correct
2 Correct 52 ms 52168 KB Output is correct
3 Correct 3 ms 8536 KB Output is correct
4 Correct 1 ms 7260 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 3 ms 7772 KB Output is correct
8 Correct 3 ms 8028 KB Output is correct
9 Correct 3 ms 7800 KB Output is correct
10 Correct 64 ms 34880 KB Output is correct
11 Correct 51 ms 31312 KB Output is correct
12 Correct 90 ms 20420 KB Output is correct
13 Correct 58 ms 35532 KB Output is correct
14 Correct 82 ms 20048 KB Output is correct
15 Correct 91 ms 20348 KB Output is correct
16 Correct 56 ms 35528 KB Output is correct
17 Correct 122 ms 19112 KB Output is correct
18 Correct 46 ms 36732 KB Output is correct
19 Correct 47 ms 16148 KB Output is correct
20 Correct 34 ms 14168 KB Output is correct
21 Correct 34 ms 13916 KB Output is correct
22 Correct 34 ms 14172 KB Output is correct
23 Correct 40 ms 14084 KB Output is correct
24 Correct 34 ms 13828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 3 ms 8540 KB Output is correct
6 Correct 1 ms 7260 KB Output is correct
7 Correct 3 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 3 ms 7772 KB Output is correct
10 Correct 3 ms 8028 KB Output is correct
11 Correct 3 ms 7800 KB Output is correct
12 Correct 3 ms 8536 KB Output is correct
13 Correct 2 ms 7256 KB Output is correct
14 Correct 2 ms 7260 KB Output is correct
15 Incorrect 1 ms 7260 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7256 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Incorrect 1 ms 7260 KB Output isn't correct
4 Halted 0 ms 0 KB -