Submission #1038030

# Submission time Handle Problem Language Result Execution time Memory
1038030 2024-07-29T12:17:41 Z juicy Cat Exercise (JOI23_ho_t4) C++17
100 / 100
156 ms 47460 KB
#include <bits/stdc++.h>

using namespace std;

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

const int N = 2e5 + 5, LG = 18;

int n;
int a[N], lab[N], top[N], dep[N], pr[LG][N];
long long dp[N];
vector<int> g[N];

void dfs(int u) {
  for (int v : g[u]) {
    if (v != pr[0][u]) {
      pr[0][v] = u;
      for (int j = 1; j < LG; ++j) {
        pr[j][v] = pr[j - 1][pr[j - 1][v]];
      }
      dep[v] = dep[u] + 1; 
      dfs(v);
    }
  }
}

int lca(int u, int v) {
  if (dep[u] < dep[v]) {
    swap(u, v);
  }
  for (int j = LG - 1; ~j; --j) {
    if (dep[u] - (1 << j) >= dep[v]) {
      u = pr[j][u];
    }
  }
  if (u == v) {
    return u;
  }
  for (int j = LG - 1; ~j; --j) {
    if (pr[j][u] != pr[j][v]) {
      u = pr[j][u], v = pr[j][v];
    }
  }
  return pr[0][u];
}

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

int find(int u) {
  return lab[u] < 0 ? u : lab[u] = find(lab[u]);
}

void mrg(int u, int v) {
  u = find(u), v = find(v);
  if (lab[u] > lab[v]) {
    swap(u, v);
    swap(top[u], top[v]);
  }
  lab[u] += lab[v];
  lab[v] = u;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i < n; ++i) {
    int u, v; cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);
  vector<int> ord(n); iota(ord.begin(), ord.end(), 1);
  sort(ord.begin(), ord.end(), [&](int i, int j) {
    return a[i] < a[j];
  });
  iota(top + 1, top + n + 1, 1);
  fill(lab + 1, lab + n + 1, -1);
  for (int u : ord) {
    for (int v : g[u]) {
      if (a[v] < a[u]) {
        auto x = top[find(v)];
        dp[u] = max(dp[u], dp[x] + dis(x, u));
        mrg(u, v);
      }
    }
  }
  cout << dp[ord.back()];
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 2 ms 23132 KB Output is correct
3 Correct 3 ms 23132 KB Output is correct
4 Correct 2 ms 23132 KB Output is correct
5 Correct 2 ms 23132 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 2 ms 23132 KB Output is correct
8 Correct 2 ms 23132 KB Output is correct
9 Correct 3 ms 23132 KB Output is correct
10 Correct 3 ms 23132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 2 ms 23132 KB Output is correct
3 Correct 3 ms 23132 KB Output is correct
4 Correct 2 ms 23132 KB Output is correct
5 Correct 2 ms 23132 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 2 ms 23132 KB Output is correct
8 Correct 2 ms 23132 KB Output is correct
9 Correct 3 ms 23132 KB Output is correct
10 Correct 3 ms 23132 KB Output is correct
11 Correct 3 ms 23132 KB Output is correct
12 Correct 3 ms 23132 KB Output is correct
13 Correct 3 ms 23212 KB Output is correct
14 Correct 3 ms 23132 KB Output is correct
15 Correct 3 ms 23132 KB Output is correct
16 Correct 3 ms 23128 KB Output is correct
17 Correct 2 ms 23132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 2 ms 23132 KB Output is correct
3 Correct 3 ms 23132 KB Output is correct
4 Correct 2 ms 23132 KB Output is correct
5 Correct 2 ms 23132 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 2 ms 23132 KB Output is correct
8 Correct 2 ms 23132 KB Output is correct
9 Correct 3 ms 23132 KB Output is correct
10 Correct 3 ms 23132 KB Output is correct
11 Correct 3 ms 23132 KB Output is correct
12 Correct 3 ms 23132 KB Output is correct
13 Correct 3 ms 23212 KB Output is correct
14 Correct 3 ms 23132 KB Output is correct
15 Correct 3 ms 23132 KB Output is correct
16 Correct 3 ms 23128 KB Output is correct
17 Correct 2 ms 23132 KB Output is correct
18 Correct 4 ms 23644 KB Output is correct
19 Correct 4 ms 23644 KB Output is correct
20 Correct 5 ms 23644 KB Output is correct
21 Correct 4 ms 23612 KB Output is correct
22 Correct 5 ms 23644 KB Output is correct
23 Correct 4 ms 23700 KB Output is correct
24 Correct 7 ms 23644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 2 ms 23132 KB Output is correct
3 Correct 3 ms 23132 KB Output is correct
4 Correct 2 ms 23132 KB Output is correct
5 Correct 2 ms 23132 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 2 ms 23132 KB Output is correct
8 Correct 2 ms 23132 KB Output is correct
9 Correct 3 ms 23132 KB Output is correct
10 Correct 3 ms 23132 KB Output is correct
11 Correct 3 ms 23132 KB Output is correct
12 Correct 3 ms 23132 KB Output is correct
13 Correct 3 ms 23212 KB Output is correct
14 Correct 3 ms 23132 KB Output is correct
15 Correct 3 ms 23132 KB Output is correct
16 Correct 3 ms 23128 KB Output is correct
17 Correct 2 ms 23132 KB Output is correct
18 Correct 4 ms 23644 KB Output is correct
19 Correct 4 ms 23644 KB Output is correct
20 Correct 5 ms 23644 KB Output is correct
21 Correct 4 ms 23612 KB Output is correct
22 Correct 5 ms 23644 KB Output is correct
23 Correct 4 ms 23700 KB Output is correct
24 Correct 7 ms 23644 KB Output is correct
25 Correct 3 ms 23132 KB Output is correct
26 Correct 5 ms 23388 KB Output is correct
27 Correct 5 ms 23388 KB Output is correct
28 Correct 4 ms 23364 KB Output is correct
29 Correct 5 ms 23448 KB Output is correct
30 Correct 4 ms 23388 KB Output is correct
31 Correct 5 ms 23388 KB Output is correct
32 Correct 5 ms 23388 KB Output is correct
33 Correct 5 ms 23388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 2 ms 23132 KB Output is correct
3 Correct 3 ms 23132 KB Output is correct
4 Correct 2 ms 23132 KB Output is correct
5 Correct 2 ms 23132 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 2 ms 23132 KB Output is correct
8 Correct 2 ms 23132 KB Output is correct
9 Correct 3 ms 23132 KB Output is correct
10 Correct 3 ms 23132 KB Output is correct
11 Correct 3 ms 23132 KB Output is correct
12 Correct 3 ms 23132 KB Output is correct
13 Correct 3 ms 23212 KB Output is correct
14 Correct 3 ms 23132 KB Output is correct
15 Correct 3 ms 23132 KB Output is correct
16 Correct 3 ms 23128 KB Output is correct
17 Correct 2 ms 23132 KB Output is correct
18 Correct 4 ms 23644 KB Output is correct
19 Correct 4 ms 23644 KB Output is correct
20 Correct 5 ms 23644 KB Output is correct
21 Correct 4 ms 23612 KB Output is correct
22 Correct 5 ms 23644 KB Output is correct
23 Correct 4 ms 23700 KB Output is correct
24 Correct 7 ms 23644 KB Output is correct
25 Correct 90 ms 46156 KB Output is correct
26 Correct 94 ms 47080 KB Output is correct
27 Correct 83 ms 47244 KB Output is correct
28 Correct 104 ms 47276 KB Output is correct
29 Correct 98 ms 47188 KB Output is correct
30 Correct 97 ms 47460 KB Output is correct
31 Correct 102 ms 47184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 3 ms 23132 KB Output is correct
3 Correct 101 ms 33512 KB Output is correct
4 Correct 106 ms 34640 KB Output is correct
5 Correct 97 ms 34388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 2 ms 23132 KB Output is correct
3 Correct 3 ms 23132 KB Output is correct
4 Correct 2 ms 23132 KB Output is correct
5 Correct 2 ms 23132 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 2 ms 23132 KB Output is correct
8 Correct 2 ms 23132 KB Output is correct
9 Correct 3 ms 23132 KB Output is correct
10 Correct 3 ms 23132 KB Output is correct
11 Correct 3 ms 23132 KB Output is correct
12 Correct 3 ms 23132 KB Output is correct
13 Correct 3 ms 23212 KB Output is correct
14 Correct 3 ms 23132 KB Output is correct
15 Correct 3 ms 23132 KB Output is correct
16 Correct 3 ms 23128 KB Output is correct
17 Correct 2 ms 23132 KB Output is correct
18 Correct 4 ms 23644 KB Output is correct
19 Correct 4 ms 23644 KB Output is correct
20 Correct 5 ms 23644 KB Output is correct
21 Correct 4 ms 23612 KB Output is correct
22 Correct 5 ms 23644 KB Output is correct
23 Correct 4 ms 23700 KB Output is correct
24 Correct 7 ms 23644 KB Output is correct
25 Correct 3 ms 23132 KB Output is correct
26 Correct 5 ms 23388 KB Output is correct
27 Correct 5 ms 23388 KB Output is correct
28 Correct 4 ms 23364 KB Output is correct
29 Correct 5 ms 23448 KB Output is correct
30 Correct 4 ms 23388 KB Output is correct
31 Correct 5 ms 23388 KB Output is correct
32 Correct 5 ms 23388 KB Output is correct
33 Correct 5 ms 23388 KB Output is correct
34 Correct 90 ms 46156 KB Output is correct
35 Correct 94 ms 47080 KB Output is correct
36 Correct 83 ms 47244 KB Output is correct
37 Correct 104 ms 47276 KB Output is correct
38 Correct 98 ms 47188 KB Output is correct
39 Correct 97 ms 47460 KB Output is correct
40 Correct 102 ms 47184 KB Output is correct
41 Correct 3 ms 23132 KB Output is correct
42 Correct 3 ms 23132 KB Output is correct
43 Correct 101 ms 33512 KB Output is correct
44 Correct 106 ms 34640 KB Output is correct
45 Correct 97 ms 34388 KB Output is correct
46 Correct 145 ms 41488 KB Output is correct
47 Correct 156 ms 40560 KB Output is correct
48 Correct 154 ms 42068 KB Output is correct
49 Correct 155 ms 40344 KB Output is correct
50 Correct 144 ms 34828 KB Output is correct
51 Correct 132 ms 34836 KB Output is correct
52 Correct 128 ms 34808 KB Output is correct
53 Correct 123 ms 34640 KB Output is correct