답안 #559303

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
559303 2022-05-09T15:11:31 Z Sweezy Cat in a tree (BOI17_catinatree) C++17
100 / 100
922 ms 69052 KB
#include <bits/stdc++.h>
 
using namespace std;
// #define int long long

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, s, n) for (int i = s; i < (n); ++i)
#define pb push_back
#define sz(a) (int) (a.size())

const int N = 2e5 + 5;
const int inf = 1e9;

vector<int> g[N];
vector<pair<int, int>> centroids[N];
int s[N], mdist[N], used[N], res, d, m;

void sizes(int v, int p) {
  s[v] = 1;
  for (auto &u : g[v]) {
    if (u != p && !used[u]) {
      sizes(u, v);
      s[v] += s[u];
    }
  }
}

int centroid(int v, int p, int n) {
  for (auto &u : g[v]) {
    if (u != p && !used[u] && s[u] > n / 2) {
      return centroid(u, v, n);
    } 
  }
  return v;
}

int vis[N], dis[N], q[N], from, to, vs[N];

void decomposition1(int v) {
  used[v] = 1;
  sizes(v, -1);

  m = 0;
  from = 0;
  to = 1;
  dis[v] = 0;
  q[0] = v;
  while (from != to) {
    int ver = q[from++];
    vs[m++] = ver;
    centroids[ver].emplace_back(v, dis[ver]);
    vis[ver] = 1;
    for (auto &u : g[ver]) {
      if (!used[u] && !vis[u]) {
        dis[u] = dis[ver] + 1;
        q[to++] = u;
        vis[u] = 1;
      }
    }
  }

  for (int i = 0; i < m; i++) {
    vis[vs[i]] = 0;
  }

  for (auto &u : g[v]) {
    if (!used[u]) {
      decomposition1(centroid(u, v, s[u]));
    }
  }
}


void decomposition2(int v) {
  used[v] = 1;
  sizes(v, -1);

  m = 0;
  from = 0;
  to = 1;
  dis[v] = 0;
  q[0] = v;
  while (from != to) {
    int ver = q[from++];
    vs[m++] = ver;
    vis[ver] = 1;
    for (auto &u : g[ver]) {
      if (!used[u] && !vis[u]) {
        dis[u] = dis[ver] + 1;
        q[to++] = u;
        vis[u] = 1;
      }
    }
  }

  auto can = [&] (int ver) -> bool {
    for (auto &[centroid, dist_to_centroid] : centroids[ver]) {
      if (dist_to_centroid + mdist[centroid] < d) {
        return false;
      }
    }
    return true;
  };

  int r = m, cnt = 0;
  rep(l, m) {
    while (r - 1 >= l && dis[vs[r - 1]] >= max(dis[vs[l]], d - dis[vs[l]])) {
      r--;
      int ver = vs[r];
      if (can(ver)) {
        cnt++;
        for (auto &[centroid, dist_to_centroid] : centroids[ver]) {
          mdist[centroid] = min(mdist[centroid], dist_to_centroid);
        }
      }
    }
    if (l == r) {
      res = max(res, cnt);
      break;
    } else {
      res = max(res, cnt + can(vs[l]));
    }
  }

  for (int i = 0; i < m; i++) {
    // vis[vs[i]] = 0;
    for (auto &[centroid, dist_to_centroid] : centroids[vs[i]]) {
      mdist[centroid] = inf;
    }
  }

  for (auto &u : g[v]) {
    if (!used[u]) {
      decomposition2(centroid(u, v, s[u]));
    }
  }
}

void solve() {
  fill(mdist, mdist + N, inf);

  int n;
  cin >> n >> d;
 
  if (d > n - 1) {
    cout << 1;
    return;
  }
 
  if (d == 0) {
    cout << n;
    return;
  }
 
  reps(i, 1, n) {
    int p;
    cin >> p;
    g[p].push_back(i);
    g[i].push_back(p);
  }
  
  sizes(0, -1);
  decomposition1(centroid(0, -1, n));

  fill(used, used + N, 0);
  sizes(0, -1);
  decomposition2(centroid(0, -1, n));

  cout << res;
}
 
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11220 KB Output is correct
2 Correct 7 ms 11300 KB Output is correct
3 Correct 7 ms 11220 KB Output is correct
4 Correct 6 ms 11220 KB Output is correct
5 Correct 7 ms 11304 KB Output is correct
6 Correct 8 ms 11220 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 6 ms 11220 KB Output is correct
9 Correct 7 ms 11220 KB Output is correct
10 Correct 6 ms 11300 KB Output is correct
11 Correct 6 ms 11220 KB Output is correct
12 Correct 9 ms 11220 KB Output is correct
13 Correct 7 ms 11220 KB Output is correct
14 Correct 7 ms 11220 KB Output is correct
15 Correct 7 ms 11220 KB Output is correct
16 Correct 6 ms 11220 KB Output is correct
17 Correct 7 ms 11220 KB Output is correct
18 Correct 6 ms 11220 KB Output is correct
19 Correct 6 ms 11304 KB Output is correct
20 Correct 7 ms 11292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11220 KB Output is correct
2 Correct 7 ms 11300 KB Output is correct
3 Correct 7 ms 11220 KB Output is correct
4 Correct 6 ms 11220 KB Output is correct
5 Correct 7 ms 11304 KB Output is correct
6 Correct 8 ms 11220 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 6 ms 11220 KB Output is correct
9 Correct 7 ms 11220 KB Output is correct
10 Correct 6 ms 11300 KB Output is correct
11 Correct 6 ms 11220 KB Output is correct
12 Correct 9 ms 11220 KB Output is correct
13 Correct 7 ms 11220 KB Output is correct
14 Correct 7 ms 11220 KB Output is correct
15 Correct 7 ms 11220 KB Output is correct
16 Correct 6 ms 11220 KB Output is correct
17 Correct 7 ms 11220 KB Output is correct
18 Correct 6 ms 11220 KB Output is correct
19 Correct 6 ms 11304 KB Output is correct
20 Correct 7 ms 11292 KB Output is correct
21 Correct 8 ms 11648 KB Output is correct
22 Correct 8 ms 11376 KB Output is correct
23 Correct 7 ms 11360 KB Output is correct
24 Correct 7 ms 11348 KB Output is correct
25 Correct 10 ms 11340 KB Output is correct
26 Correct 8 ms 11440 KB Output is correct
27 Correct 8 ms 11348 KB Output is correct
28 Correct 8 ms 11476 KB Output is correct
29 Correct 7 ms 11476 KB Output is correct
30 Correct 8 ms 11544 KB Output is correct
31 Correct 8 ms 11524 KB Output is correct
32 Correct 7 ms 11452 KB Output is correct
33 Correct 7 ms 11476 KB Output is correct
34 Correct 7 ms 11476 KB Output is correct
35 Correct 7 ms 11476 KB Output is correct
36 Correct 9 ms 11476 KB Output is correct
37 Correct 8 ms 11476 KB Output is correct
38 Correct 9 ms 11476 KB Output is correct
39 Correct 8 ms 11488 KB Output is correct
40 Correct 8 ms 11688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11220 KB Output is correct
2 Correct 7 ms 11300 KB Output is correct
3 Correct 7 ms 11220 KB Output is correct
4 Correct 6 ms 11220 KB Output is correct
5 Correct 7 ms 11304 KB Output is correct
6 Correct 8 ms 11220 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 6 ms 11220 KB Output is correct
9 Correct 7 ms 11220 KB Output is correct
10 Correct 6 ms 11300 KB Output is correct
11 Correct 6 ms 11220 KB Output is correct
12 Correct 9 ms 11220 KB Output is correct
13 Correct 7 ms 11220 KB Output is correct
14 Correct 7 ms 11220 KB Output is correct
15 Correct 7 ms 11220 KB Output is correct
16 Correct 6 ms 11220 KB Output is correct
17 Correct 7 ms 11220 KB Output is correct
18 Correct 6 ms 11220 KB Output is correct
19 Correct 6 ms 11304 KB Output is correct
20 Correct 7 ms 11292 KB Output is correct
21 Correct 8 ms 11648 KB Output is correct
22 Correct 8 ms 11376 KB Output is correct
23 Correct 7 ms 11360 KB Output is correct
24 Correct 7 ms 11348 KB Output is correct
25 Correct 10 ms 11340 KB Output is correct
26 Correct 8 ms 11440 KB Output is correct
27 Correct 8 ms 11348 KB Output is correct
28 Correct 8 ms 11476 KB Output is correct
29 Correct 7 ms 11476 KB Output is correct
30 Correct 8 ms 11544 KB Output is correct
31 Correct 8 ms 11524 KB Output is correct
32 Correct 7 ms 11452 KB Output is correct
33 Correct 7 ms 11476 KB Output is correct
34 Correct 7 ms 11476 KB Output is correct
35 Correct 7 ms 11476 KB Output is correct
36 Correct 9 ms 11476 KB Output is correct
37 Correct 8 ms 11476 KB Output is correct
38 Correct 9 ms 11476 KB Output is correct
39 Correct 8 ms 11488 KB Output is correct
40 Correct 8 ms 11688 KB Output is correct
41 Correct 169 ms 29640 KB Output is correct
42 Correct 311 ms 28364 KB Output is correct
43 Correct 299 ms 28748 KB Output is correct
44 Correct 322 ms 28780 KB Output is correct
45 Correct 300 ms 28544 KB Output is correct
46 Correct 871 ms 45836 KB Output is correct
47 Correct 922 ms 46648 KB Output is correct
48 Correct 805 ms 46932 KB Output is correct
49 Correct 866 ms 47056 KB Output is correct
50 Correct 187 ms 24972 KB Output is correct
51 Correct 169 ms 25276 KB Output is correct
52 Correct 163 ms 25164 KB Output is correct
53 Correct 593 ms 40228 KB Output is correct
54 Correct 474 ms 39472 KB Output is correct
55 Correct 474 ms 39524 KB Output is correct
56 Correct 8 ms 11860 KB Output is correct
57 Correct 44 ms 16676 KB Output is correct
58 Correct 206 ms 40392 KB Output is correct
59 Correct 822 ms 69052 KB Output is correct
60 Correct 154 ms 30224 KB Output is correct
61 Correct 378 ms 37000 KB Output is correct