Submission #792227

# Submission time Handle Problem Language Result Execution time Memory
792227 2023-07-24T19:32:50 Z WLZ Cat Exercise (JOI23_ho_t4) C++17
100 / 100
219 ms 59228 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "templates/debug.h"
#else
#define debug(...) 0
#endif

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int) x.size()

using ll = long long;
using ull = unsigned ll;
using lll = __int128_t;
using ulll = __uint128_t;
using ld = long double;
using ii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
template<typename T> using mx_pq = priority_queue<T>;
template<typename T> using mn_pq = priority_queue<T, vector<T>, greater<T>>;

template<typename T> void cmax(T &a, const T &b) { a = max(a, b); }
template<typename T> void cmin(T &a, const T &b) { a = min(a, b); }

const int INF = 0x3f3f3f3f;
const ll LINF = (ll) 1e18;
const double DINF = 1.0 / 0.0;
const double pi = acos(-1);
const double EPS = 1e-9;

void solve(int current_case);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  //cin >> t;
  for (int q = 1; q <= t; q++) solve(q);
  return 0;
}

class least_common_ancestor {
  private:
    int n;
    vector<vi> g;
    vi p, jump, d;

    void dfs(int u) {
      if (d[p[u]] - d[jump[p[u]]] == d[jump[p[u]]] - d[jump[jump[p[u]]]]) jump[u] = jump[jump[p[u]]];
      else jump[u] = p[u];
      for (auto &v : g[u]) {
        if (v != p[u]) {
          d[v] = d[u] + 1;
          p[v] = u;
          dfs(v);
        }
      }
    }
  public:
    least_common_ancestor(const vector<vi> &_g, int root = 1) : g(_g) {
      n = SZ(g);
      p.resize(n); jump.resize(n); d.resize(n);
      p[root] = root; jump[root] = root; d[root] = 0;
      dfs(root);
    }
  
    int kth_ancestor(int u, int k) {
      int v = u;
      while (d[v] > d[u] - k) {
        if (d[jump[v]] >= d[u] - k) v = jump[v];
        else v = p[v];
      }
      return v;
    }

    int query(int u, int v) {
      if (d[u] < d[v]) swap(u, v);
      u = kth_ancestor(u, d[u] - d[v]);
      if (u == v) return u;
      while (p[u] != p[v]) {
        if (jump[u] != jump[v]) u = jump[u], v = jump[v];
        else u = p[u], v = p[v];
      }
      return p[u];
    }

    int depth(int u) {
      return d[u];
    }

    int dist(int u, int v) {
      return d[u] + d[v] - 2 * d[query(u, v)];
    }
};

class dsu {
  private: vector<int> p, rank, a, mx;
  public:
    dsu(const vector<int> &_a) : a(_a) {
      int n = SZ(a);
      mx.resize(n);
      iota(all(mx), 0);
      p.assign(n, -1);
      rank.assign(n, 0);
    }

    int root(int x) {
      if (p[x] == -1) return x;
      return p[x] = root(p[x]);
    }

    bool connected(int x, int y) {
      return root(x) == root(y);
    }

    void connect(int x, int y) {
      x = root(x); y = root(y);
      if (x != y) {
        if (rank[x] > rank[y]) {
          p[y] = x;
          if (a[mx[y]] > a[mx[x]]) mx[x] = mx[y];
        }
        else {
          p[x] = y;
          if (a[mx[x]] > a[mx[y]]) mx[y] = mx[x];
          if (rank[x] == rank[y]) rank[y]++;
        }
      }
    }

    int max(int x) {
      return mx[root(x)];
    }
};

int n;
vector<vi> g, g2;
vi h;

ll dfs(int u, least_common_ancestor &lca) {
  ll ans = 0;
  for (auto &v : g2[u]) cmax(ans, lca.dist(u, v) + dfs(v, lca));
  return ans;
}

void solve(int current_case) {
  cin >> n;
  g.resize(n + 1);
  h.resize(n + 1);
  vii b;
  rep(i, 1, n + 1) cin >> h[i], b.eb(h[i], i);
  rep(i, 1, n) {
    int a, b; cin >> a >> b;
    g[a].pb(b); g[b].pb(a);
  }
  sort(all(b));
  vi used(n + 1, 0);
  g2.resize(n + 1);
  dsu uf(h);
  for (auto &p : b) {
    int u = p.second;
    used[u] = 1;
    for (auto &v : g[u]) if (used[v]) g2[u].pb(uf.max(v));
    for (auto &v : g[u]) if (used[v]) uf.connect(u, v);
  }
  least_common_ancestor lca(g, b[n - 1].second);
  cout << dfs(b[n - 1].second, lca) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 1748 KB Output is correct
19 Correct 3 ms 1620 KB Output is correct
20 Correct 3 ms 1620 KB Output is correct
21 Correct 3 ms 1432 KB Output is correct
22 Correct 4 ms 1364 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 3 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 1748 KB Output is correct
19 Correct 3 ms 1620 KB Output is correct
20 Correct 3 ms 1620 KB Output is correct
21 Correct 3 ms 1432 KB Output is correct
22 Correct 4 ms 1364 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 3 ms 1364 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 3 ms 1532 KB Output is correct
27 Correct 3 ms 1492 KB Output is correct
28 Correct 3 ms 1492 KB Output is correct
29 Correct 3 ms 1492 KB Output is correct
30 Correct 3 ms 1236 KB Output is correct
31 Correct 3 ms 1300 KB Output is correct
32 Correct 3 ms 1236 KB Output is correct
33 Correct 4 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 1748 KB Output is correct
19 Correct 3 ms 1620 KB Output is correct
20 Correct 3 ms 1620 KB Output is correct
21 Correct 3 ms 1432 KB Output is correct
22 Correct 4 ms 1364 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 3 ms 1364 KB Output is correct
25 Correct 127 ms 57448 KB Output is correct
26 Correct 117 ms 59228 KB Output is correct
27 Correct 121 ms 59176 KB Output is correct
28 Correct 145 ms 48428 KB Output is correct
29 Correct 143 ms 49284 KB Output is correct
30 Correct 153 ms 49088 KB Output is correct
31 Correct 145 ms 49588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 146 ms 40648 KB Output is correct
4 Correct 142 ms 40776 KB Output is correct
5 Correct 145 ms 40532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 1748 KB Output is correct
19 Correct 3 ms 1620 KB Output is correct
20 Correct 3 ms 1620 KB Output is correct
21 Correct 3 ms 1432 KB Output is correct
22 Correct 4 ms 1364 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 3 ms 1364 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 3 ms 1532 KB Output is correct
27 Correct 3 ms 1492 KB Output is correct
28 Correct 3 ms 1492 KB Output is correct
29 Correct 3 ms 1492 KB Output is correct
30 Correct 3 ms 1236 KB Output is correct
31 Correct 3 ms 1300 KB Output is correct
32 Correct 3 ms 1236 KB Output is correct
33 Correct 4 ms 1236 KB Output is correct
34 Correct 127 ms 57448 KB Output is correct
35 Correct 117 ms 59228 KB Output is correct
36 Correct 121 ms 59176 KB Output is correct
37 Correct 145 ms 48428 KB Output is correct
38 Correct 143 ms 49284 KB Output is correct
39 Correct 153 ms 49088 KB Output is correct
40 Correct 145 ms 49588 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 146 ms 40648 KB Output is correct
44 Correct 142 ms 40776 KB Output is correct
45 Correct 145 ms 40532 KB Output is correct
46 Correct 212 ms 55568 KB Output is correct
47 Correct 205 ms 55492 KB Output is correct
48 Correct 193 ms 55596 KB Output is correct
49 Correct 204 ms 55572 KB Output is correct
50 Correct 192 ms 43520 KB Output is correct
51 Correct 198 ms 43632 KB Output is correct
52 Correct 187 ms 43460 KB Output is correct
53 Correct 219 ms 43584 KB Output is correct