답안 #1040614

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1040614 2024-08-01T07:47:39 Z erray 참나무 (IOI23_beechtree) C++17
53 / 100
240 ms 80308 KB
#include "beechtree.h"
#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG 
  #include "/home/ioi/contests/ioi23_d2/debug.h"
#else 
  #define debug(...) void(37)
#endif

/*
template<typename T, using F = function<T(const T&, const T&)>>
struct SegTree {
  F op;
  vector<T> tree; T def;
  int n;
  SegTree(int _n, T _def, F _op) : n(_n), op(_op), def(_def) {
    tree.resize(2 * n);
  }
  T get(int l, int r) {
    T res = def;
    for (l += n, r += n + 1; l < r; l >> 1, r >> 1) {
      if (l % 2) res = op(res, tree[l++]);
      if (r % 2) res = op(res, tree[--r]);
    }
    return res;
  } 
  void modify(int p, T x) {
    for (tree[p += n] = x; p > 1; p >>= 1) {
      tree[p >> 1] = op(tree[p], tree[p ^ 1]);
    }
  }
};
*/
struct Fenwick {
  vector<int> sum;
  int n;
  Fenwick() { } 
  Fenwick(int _n) : n(_n) {
    sum.resize(n + 1);
  }
  int get(int x) {
    x += 1;
    int res = 0;
    while (x > 0) {
      res += sum[x];
      x -= x & -x;
    } 
    return res;
  }
  void modify(int x, int v) {
    x += 1;
    while (x <= n) {
      sum[x] += v;
      x += x & -x;
    }
  }
};

std::vector<int> beechtree(int N, int M, std::vector<int> P, std::vector<int> C) {
  for (auto& x : C) --x;
  vector<vector<int>> g(N);
  for (int i = 1; i < N; ++i) {
    g[P[i]].push_back(i);
  }
  vector<vector<int>> sub(N);
  vector<int> s(N, 1);
  for (int i = N - 1; i > 0; --i) {
    s[P[i]] += s[i];
    sub[P[i]].push_back(C[i]);
  }
  vector<bool> bad(N, false);
  for (int i = 0; i < N; ++i) {
    sort(sub[i].begin(), sub[i].end());
    for (int j = 0; j < int(sub[i].size()) - 1; ++j) {
      if (sub[i][j] == sub[i][j + 1]) bad[i] = true;
    }
  }
  for (int i = 0; i < N; ++i) {
    for (auto u : g[i]) {
      if (int(g[u].size()) > int(g[i].size())) bad[i] = true;
    }
  }
  vector<vector<int>> all(M);
  for (int i = 1; i < N; ++i) {
    all[C[i]].push_back(s[i]);
  }
  debug(all);
  for (int i = 0; i < M; ++i) {
    sort(all[i].begin(), all[i].end());
    all[i].erase(unique(all[i].begin(), all[i].end()), all[i].end());
  }
  vector<int> ind(N, -1);
  for (int i = 1; i < N; ++i) {
    ind[i] = int(all[C[i]].size()) - 1 - int(lower_bound(all[C[i]].begin(), all[C[i]].end(), s[i]) - all[C[i]].begin());
  }
  debug(ind);
  vector<Fenwick> fenws(M);
  for (int i = 0; i < M; ++i) {
    fenws[i] = Fenwick(int(all[i].size()));
  }
  vector<int> ct(M);
  set<array<int, 2>> sizes;
  vector<array<int, 2>> fenwick_updates;
  bool still_good = true;
  
  vector<bool> cur_color(M);
  auto Add = [&](int v) {
    int l = 0, r = N;
    for (auto u : g[v]) {
      cur_color[C[u]] = true;
    }
    {
      auto it = sizes.begin();
      while (it != sizes.end()) {
        if (!cur_color[(*it)[1]]) {
          l = (*it)[0];
          break;
        }
        it = next(it);
      }
    }
    for (auto u : g[v]) {
      r = min(r, fenws[C[u]].get(ind[u]));
      l = max(l, fenws[C[u]].get(ind[u] - 1));
    }
    still_good &= l <= r;
    for (auto u : g[v]) {
      cur_color[C[u]] = false;
      sizes.erase({ct[C[u]], C[u]});
      ct[C[u]]++;
      sizes.insert({ct[C[u]], C[u]});
      fenws[C[u]].modify(ind[u], +1);
      fenwick_updates.push_back({C[u], ind[u]});
    }
  };
  auto Reset = [&] {
    still_good = true;
    sizes.clear();
    for (auto[c, x] : fenwick_updates) {
      fenws[c].modify(x, -1);
      ct[c] = 0;
    }
    fenwick_updates.clear();
  };
  auto Dfs = [&](int v, bool calculating, auto&& Dfs) -> void {
    debug(v, calculating);
    for (int i = 0; i < int(g[v].size()); ++i) {
      if (s[g[v][0]] < s[g[v][i]]) swap(g[v][0], g[v][i]);
    }
    if (calculating) {
      for (int i = int(g[v].size()) - 1; i >= 0; --i) {
        Reset();
        Dfs(g[v][i], true, Dfs);
      }
      for (int i = int(g[v].size()) - 1; i > 0; --i) {
        Dfs(g[v][i], false, Dfs);
      }
      Add(v);
      if (!still_good) bad[v] = true;
    } else {
      Add(v);
      for (auto u : g[v]) {
        Dfs(u, false, Dfs);
      }
    }
  };
  Dfs(0, true, Dfs);
  
  for (int i = N - 1; i > 0; --i) {
    if (bad[i]) bad[P[i]] = true;
  }
  vector<int> res(N);
  for (int i = 0; i < N; ++i) res[i] = 1 ^ bool(bad[i]);
  return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 436 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 240 ms 80308 KB Output is correct
8 Correct 161 ms 76584 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 14 ms 18780 KB Output is correct
14 Correct 13 ms 18780 KB Output is correct
15 Correct 13 ms 19132 KB Output is correct
16 Correct 13 ms 19032 KB Output is correct
17 Correct 98 ms 70008 KB Output is correct
18 Correct 98 ms 70096 KB Output is correct
19 Correct 177 ms 76360 KB Output is correct
20 Correct 216 ms 78544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 240 ms 80308 KB Output is correct
6 Correct 161 ms 76584 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 12 ms 18628 KB Output is correct
10 Correct 13 ms 18520 KB Output is correct
11 Correct 182 ms 53012 KB Output is correct
12 Correct 203 ms 47512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 436 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Incorrect 1 ms 348 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
45 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 436 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 436 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 14 ms 18780 KB Output is correct
43 Correct 13 ms 18780 KB Output is correct
44 Correct 13 ms 19132 KB Output is correct
45 Correct 13 ms 19032 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Incorrect 1 ms 348 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
49 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 436 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 90 ms 51272 KB Output is correct
40 Correct 84 ms 51280 KB Output is correct
41 Correct 83 ms 51400 KB Output is correct
42 Correct 89 ms 51252 KB Output is correct
43 Correct 91 ms 44196 KB Output is correct
44 Correct 104 ms 34240 KB Output is correct
45 Correct 164 ms 28372 KB Output is correct
46 Correct 153 ms 27672 KB Output is correct
47 Correct 131 ms 27480 KB Output is correct
48 Correct 101 ms 27476 KB Output is correct
49 Correct 88 ms 26888 KB Output is correct
50 Correct 165 ms 25404 KB Output is correct
51 Correct 148 ms 25412 KB Output is correct
52 Correct 123 ms 25516 KB Output is correct
53 Correct 101 ms 25496 KB Output is correct
54 Correct 82 ms 24336 KB Output is correct
55 Correct 82 ms 32288 KB Output is correct
56 Correct 80 ms 32624 KB Output is correct
57 Correct 78 ms 26152 KB Output is correct
58 Correct 97 ms 25528 KB Output is correct
59 Correct 77 ms 25456 KB Output is correct
60 Correct 85 ms 25408 KB Output is correct
61 Correct 77 ms 25540 KB Output is correct
62 Correct 77 ms 25408 KB Output is correct
63 Correct 79 ms 30404 KB Output is correct
64 Correct 92 ms 32704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 436 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 240 ms 80308 KB Output is correct
39 Correct 161 ms 76584 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 14 ms 18780 KB Output is correct
45 Correct 13 ms 18780 KB Output is correct
46 Correct 13 ms 19132 KB Output is correct
47 Correct 13 ms 19032 KB Output is correct
48 Correct 98 ms 70008 KB Output is correct
49 Correct 98 ms 70096 KB Output is correct
50 Correct 177 ms 76360 KB Output is correct
51 Correct 216 ms 78544 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Incorrect 1 ms 348 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
55 Halted 0 ms 0 KB -