Submission #395653

# Submission time Handle Problem Language Result Execution time Memory
395653 2021-04-28T17:41:56 Z usachevd0 Meetings 2 (JOI21_meetings2) C++17
100 / 100
840 ms 29520 KB
/*#pragma gcc optimize("Ofast")
#pragma gcc optimize("O3")
#pragma gcc optimize("fast-math")
#pragma gcc optimize("unroll-loops")
#pragma gcc optimize("no-stack-protector")
#pragma gcc target("avx,avx2,ffa,sse,sse2,sse4.2")*/
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) {
  return y < x ? (x = y, true) : false; 
}
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) {
  return y > x ? (x = y, true) : false;
}
void debug_out() {
  cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
  cerr << ' ' << A;
  debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
  for (int i = 0; i < n; ++i) {
    cerr << a[i] << ' ';
  }
  cerr << endl;
}
#ifdef DEBUG
  #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
  #define debug(...) 1337
  #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T> &v) {
  for (auto& x : v) {
    stream << x << ' ';
  }
  return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
  return stream << "(" << p.first << ' ' << p.second << ")";
}

const int INF32 = 1e9;
const int maxN = 200005;
int n;
vector<int> G[maxN];

int ans[maxN];

bool used[maxN];
int depth[maxN];
int sz[maxN];

int max_depth[maxN];
pii max_depth_1[maxN], max_depth_2[maxN];

int find_centroid(int v, int SZ, int& c, int p = -1) {
  int sz = 1;
  for (int& u : G[v]) {
    if (u != p && !used[u]) {
      sz += find_centroid(u, SZ, c, v);
    }
  }
  if (c == -1 && sz + sz >= SZ) {
    c = v;
  }
  return sz;
}

void rec(int v, int d = 0, int p = -1) {
  depth[v] = d;
  sz[v] = 1;
  for (int& u : G[v]) {
    if (u != p && !used[u]) {
      rec(u, d + 1, v);
      sz[v] += sz[u];
    }
  }
  chkmax(max_depth[sz[v]], depth[v]);
}

void build(int v0, int SZ) {
  int c = -1;
  find_centroid(v0, SZ, c);
  if (c == -1) {
    c = v0;
  }

  //debug(c);

  depth[c] = 0;
  fill(max_depth_1, max_depth_1 + SZ + 1, mp(-INF32, -1));
  fill(max_depth_2, max_depth_2 + SZ + 1, mp(-INF32, -2));
  int subtrees = 0;
  for (int v : G[c]) {
    if (!used[v]) {
      ++subtrees;
      rec(v, 1, c);
      sz[c] += sz[v];

      //debug(v);
      //mdebug(max_depth, sz[v] + 1);

      for (int s = 1; s <= sz[v]; ++s) {
        chkmax(ans[min(s, SZ - sz[v])], max_depth[s]);
      }
      for (int s = 1; s <= sz[v]; ++s) {
        if (max_depth[s] >= max_depth_1[s].fi) {
          max_depth_2[s] = max_depth_1[s];
          max_depth_1[s] = {max_depth[s], v};
        } else chkmax(max_depth_2[s], mp(max_depth[s], v));
      }
      fill(max_depth, max_depth + sz[v] + 1, -INF32);
    }
  }
  //mdebug(max_depth_1, sz[c] + 1);
  //mdebug(max_depth_2, sz[c] + 1);

  if (subtrees >= 2) {
    for (int s = SZ - 1; s >= 0; --s) {
      for (pii A : {max_depth_1[s + 1], max_depth_2[s + 1]}) {
        if (A.fi >= max_depth_1[s].fi) {
          if (max_depth_1[s].se != A.se) {
            max_depth_2[s] = max_depth_1[s];
          }
          max_depth_1[s] = A;
        } else if (A.fi > max_depth_2[s].fi && max_depth_1[s].se != A.se) {
          max_depth_2[s] = A;
        }
      }
    }
    //mdebug(max_depth_1, sz[c] + 1);
    //mdebug(max_depth_2, sz[c] + 1);

    for (int v : G[c]) {
      if (!used[v]) {
        rec(v, 1, c);

        for (int s = 1; s <= sz[v]; ++s) {
          int d1 = max_depth[s];
          int d2 = max_depth_1[s].se != v ? max_depth_1[s].fi : max_depth_2[s].fi;
          //debug(v, s, d1, d2);
          chkmax(ans[s], d1 + d2);
        }

        fill(max_depth, max_depth + sz[v] + 1, -INF32);
      }
    }
  }

  used[c] = true;
  for (int v : G[c]) {
    if (!used[v]) {
      build(v, sz[v]);
    }
  }
}

int32_t main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  fill(max_depth, max_depth + n + 1, -INF32);
  build(1, n);
  for (int s = n; s >= 0; --s) {
    chkmax(ans[s], ans[s + 1]);
  }
  for (int k = 1; k <= n; ++k) {
    if (k & 1) {
      cout << "1\n";
    } else {
      cout << ans[k / 2] + 1 << '\n';
    }
  }
  
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5020 KB Output is correct
2 Correct 4 ms 5064 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 5024 KB Output is correct
5 Correct 4 ms 5020 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 5024 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 4 ms 5020 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5020 KB Output is correct
2 Correct 4 ms 5064 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 5024 KB Output is correct
5 Correct 4 ms 5020 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 5024 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 4 ms 5020 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 4940 KB Output is correct
22 Correct 7 ms 5248 KB Output is correct
23 Correct 7 ms 5324 KB Output is correct
24 Correct 8 ms 5324 KB Output is correct
25 Correct 8 ms 5324 KB Output is correct
26 Correct 7 ms 5324 KB Output is correct
27 Correct 7 ms 5292 KB Output is correct
28 Correct 7 ms 5324 KB Output is correct
29 Correct 8 ms 5324 KB Output is correct
30 Correct 8 ms 5324 KB Output is correct
31 Correct 8 ms 5324 KB Output is correct
32 Correct 9 ms 5416 KB Output is correct
33 Correct 9 ms 5452 KB Output is correct
34 Correct 6 ms 5324 KB Output is correct
35 Correct 5 ms 5324 KB Output is correct
36 Correct 7 ms 5324 KB Output is correct
37 Correct 6 ms 5324 KB Output is correct
38 Correct 7 ms 5328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5020 KB Output is correct
2 Correct 4 ms 5064 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 5024 KB Output is correct
5 Correct 4 ms 5020 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 5024 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 4 ms 5020 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 4940 KB Output is correct
22 Correct 7 ms 5248 KB Output is correct
23 Correct 7 ms 5324 KB Output is correct
24 Correct 8 ms 5324 KB Output is correct
25 Correct 8 ms 5324 KB Output is correct
26 Correct 7 ms 5324 KB Output is correct
27 Correct 7 ms 5292 KB Output is correct
28 Correct 7 ms 5324 KB Output is correct
29 Correct 8 ms 5324 KB Output is correct
30 Correct 8 ms 5324 KB Output is correct
31 Correct 8 ms 5324 KB Output is correct
32 Correct 9 ms 5416 KB Output is correct
33 Correct 9 ms 5452 KB Output is correct
34 Correct 6 ms 5324 KB Output is correct
35 Correct 5 ms 5324 KB Output is correct
36 Correct 7 ms 5324 KB Output is correct
37 Correct 6 ms 5324 KB Output is correct
38 Correct 7 ms 5328 KB Output is correct
39 Correct 534 ms 20172 KB Output is correct
40 Correct 521 ms 19836 KB Output is correct
41 Correct 534 ms 20164 KB Output is correct
42 Correct 528 ms 20424 KB Output is correct
43 Correct 527 ms 20416 KB Output is correct
44 Correct 551 ms 20300 KB Output is correct
45 Correct 840 ms 25412 KB Output is correct
46 Correct 650 ms 29520 KB Output is correct
47 Correct 194 ms 20412 KB Output is correct
48 Correct 150 ms 20560 KB Output is correct
49 Correct 462 ms 21052 KB Output is correct
50 Correct 182 ms 21052 KB Output is correct
51 Correct 455 ms 28380 KB Output is correct