Submission #593180

# Submission time Handle Problem Language Result Execution time Memory
593180 2022-07-10T14:21:37 Z SuffixAutomata Meetings 2 (JOI21_meetings2) C++17
100 / 100
370 ms 45772 KB
#include <bits/stdc++.h>
using namespace std;

#define all(a) a.begin(), a.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair

using ll = long long;
using pii = pair<int, int>;
//#define int ll
const int MOD = 1000000007;

vector<int> elist[200005];

int fa[200005], sz[200005], dep[200005], mxs[200005];

int dfss(int u, int f) {
  fa[u] = f, sz[u] = 1;
  dep[u] = dep[f] + 1;
  for (int v : elist[u])
    if (v != f)
      sz[u] += dfss(v, u), mxs[u] = max(mxs[u], sz[v]);
  return sz[u];
}

int ans[100005];

int fa2[200005][20];
int be[200005], en[200005], clo;

void dfs3(int u, int f) {
  fa2[u][0] = f;
  for (int i = 1; i < 20; i++)
    fa2[u][i] = fa2[fa2[u][i - 1]][i - 1];
  be[u] = clo++;
  for (int v : elist[u])
    if (v != f)
      dfs3(v, u);
  en[u] = clo;
}

int lca(int u, int v) {
  if (be[u] <= be[v] && be[v] < en[u])
    return u;
  for (int i = 19; i >= 0; i--) {
    int x = fa2[u][i];
    if (!(be[x] <= be[v] && be[v] < en[x]))
      u = x;
  }
  return fa[u];
}

int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int a, b;
    cin >> a >> b;
    elist[a].pb(b);
    elist[b].pb(a);
  }
  if (n == 1) {
    cout << "1\n";
    return 0;
  }
  if (n == 2) {
    cout << "1\n2\n";
    return 0;
  }
  dfss(1, 0);
  int rt = 1, rtv = mxs[1];
  for (int i = 1; i <= n; i++)
    if (max(mxs[i], n - sz[i]) < rtv) {
      rtv = max(mxs[i], n - sz[i]);
      rt = i;
    }
  dfss(rt, 0);
  dfs3(rt, 0);
  en[0] = 1e9;
  vector<pii> ps;
  for (int i = 1; i <= n; i++)
    if (i != rt) {
      ps.pb({sz[i], i});
      assert(sz[i] <= n / 2);
    }
  sort(all(ps));
  int u = rt, v = rt, ca = 0;
  for (int i = n / 2; i >= 1; i--) {
    while (ps.size() && ps.back().fi >= i) {
      int d;
      d = dis(u, ps.back().se);
      if (d > ca)
        v = ps.back().se, ca = d;
      d = dis(ps.back().se, v);
      if (d > ca)
        u = ps.back().se, ca = d;
      ps.pop_back();
    }
    ans[i] = ca + 1;
  }
  for (int i = n / 2; i >= 1; i--)
    ans[i] = max(ans[i], ans[i + 1]);
  for (int i = 1; i <= n; i++) {
    if (i % 2)
      cout << "1\n";
    else
      cout << ans[i / 2] << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 5020 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 5024 KB Output is correct
10 Correct 4 ms 4948 KB Output is correct
11 Correct 3 ms 5016 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5024 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5028 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5028 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 5020 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 5024 KB Output is correct
10 Correct 4 ms 4948 KB Output is correct
11 Correct 3 ms 5016 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5024 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5028 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5028 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 7 ms 5588 KB Output is correct
23 Correct 7 ms 5672 KB Output is correct
24 Correct 7 ms 5716 KB Output is correct
25 Correct 6 ms 5676 KB Output is correct
26 Correct 6 ms 5588 KB Output is correct
27 Correct 6 ms 5672 KB Output is correct
28 Correct 6 ms 5676 KB Output is correct
29 Correct 7 ms 5716 KB Output is correct
30 Correct 6 ms 5668 KB Output is correct
31 Correct 6 ms 5716 KB Output is correct
32 Correct 6 ms 5804 KB Output is correct
33 Correct 7 ms 5844 KB Output is correct
34 Correct 6 ms 5716 KB Output is correct
35 Correct 6 ms 5676 KB Output is correct
36 Correct 6 ms 5716 KB Output is correct
37 Correct 6 ms 5680 KB Output is correct
38 Correct 5 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 5020 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 5024 KB Output is correct
10 Correct 4 ms 4948 KB Output is correct
11 Correct 3 ms 5016 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5024 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5028 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5028 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 7 ms 5588 KB Output is correct
23 Correct 7 ms 5672 KB Output is correct
24 Correct 7 ms 5716 KB Output is correct
25 Correct 6 ms 5676 KB Output is correct
26 Correct 6 ms 5588 KB Output is correct
27 Correct 6 ms 5672 KB Output is correct
28 Correct 6 ms 5676 KB Output is correct
29 Correct 7 ms 5716 KB Output is correct
30 Correct 6 ms 5668 KB Output is correct
31 Correct 6 ms 5716 KB Output is correct
32 Correct 6 ms 5804 KB Output is correct
33 Correct 7 ms 5844 KB Output is correct
34 Correct 6 ms 5716 KB Output is correct
35 Correct 6 ms 5676 KB Output is correct
36 Correct 6 ms 5716 KB Output is correct
37 Correct 6 ms 5680 KB Output is correct
38 Correct 5 ms 5716 KB Output is correct
39 Correct 314 ms 36200 KB Output is correct
40 Correct 285 ms 35768 KB Output is correct
41 Correct 274 ms 36332 KB Output is correct
42 Correct 295 ms 36972 KB Output is correct
43 Correct 276 ms 36768 KB Output is correct
44 Correct 288 ms 36804 KB Output is correct
45 Correct 370 ms 41804 KB Output is correct
46 Correct 290 ms 45772 KB Output is correct
47 Correct 240 ms 37208 KB Output is correct
48 Correct 176 ms 36556 KB Output is correct
49 Correct 274 ms 37440 KB Output is correct
50 Correct 226 ms 37140 KB Output is correct
51 Correct 218 ms 44668 KB Output is correct