Submission #782827

# Submission time Handle Problem Language Result Execution time Memory
782827 2023-07-14T10:03:40 Z thimote75 Cat Exercise (JOI23_ho_t4) C++14
54 / 100
319 ms 51140 KB
#include <bits/stdc++.h>

using namespace std;

using idata = vector<int>;
using igrid = vector<idata>;

struct UFD {
    idata parents;

    UFD (int size) {
        parents.resize(size);
        iota(parents.begin(), parents.end(), 0);
    }
    int boss (int x) {
        if (parents[x] != x)
            parents[x] = boss(parents[x]);
        return parents[x];
    }
    void merge (int a, int b) {
        a = boss(a);
        b = boss(b);
        if (a < b) swap (a, b);

        parents[b] = a;
    }
};

const int MAXK = 20;
int N;

idata depth;
idata parents;
igrid parents_2k;

igrid roads;

void dfs (int node, int parent, int _depth) {
    depth     [node]    = _depth;
    parents   [node]    = parent;
    parents_2k[node][0] = parent;

    for (int next : roads[node]) if (next != parent)
        dfs(next, node, _depth + 1);
}
void init_lca () {
    for (int k = 0; k + 1 < MAXK; k ++) {
        for (int i = 0; i < N; i ++) {
            if (parents_2k[i][k] == -1) continue ;

            parents_2k[i][k + 1]
             = parents_2k[parents_2k[i][k]][k];
        }
    }
}
int jump (int node, int k) {
    for (int i = 0; i < MAXK; i ++)
        if ((1 << i) & k)
            node = parents_2k[node][i];
    return node;
}
int lca (int a, int b) {
    if (depth[a] > depth[b]) swap(a, b);

    b = jump(b, depth[b] - depth[a]);

    if (a == b) return a;

    for (int i = MAXK - 1; i >= 0; i --) {
        if (parents_2k[a][i] == parents_2k[b][i]) continue ;

        a = parents_2k[a][i];
        b = parents_2k[b][i];
    }

    return parents[a];
}
int dist (int a, int b) {
    int l = lca(a, b);
    return depth[a] + depth[b] - 2 * depth[l];
}

idata height;
idata antiheight;

int main () {
    cin >> N;

    height.resize(N); antiheight.resize(N);
    for (int i = 0; i < N; i ++) {
        cin >> height[i]; height[i] --;
        antiheight[height[i]] = i;
    }

    roads.resize(N);
    for (int i = 1; i < N; i ++) {
        int a, b;
        cin >> a >> b;
        a --; b --;

        roads[a].push_back(b);
        roads[b].push_back(a);
    }

    depth  .resize(N);
    parents.resize(N);
    parents_2k.resize(N, idata(MAXK, -1));
    dfs(0, -1, 0);

    init_lca();

    idata dp(N);

    UFD ufd(N);

    for (int i = 0; i < N; i ++) {
        int pos = antiheight[i];
        int mdp = 0;

        for (int next : roads[pos]) {
            if (height[next] > i) continue ;

            int local = ufd.boss(height[next]);
            int ldp   = dp [antiheight[local]] + dist(antiheight[local], pos);
            mdp = max(ldp, mdp);
            ufd.merge(i, height[next]);
        }

        dp[pos] = mdp;
    }

    cout << dp[antiheight[N - 1]] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 5 ms 1492 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 5 ms 1464 KB Output is correct
21 Correct 5 ms 1492 KB Output is correct
22 Correct 5 ms 1492 KB Output is correct
23 Correct 5 ms 1464 KB Output is correct
24 Correct 5 ms 1460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 5 ms 1492 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 5 ms 1464 KB Output is correct
21 Correct 5 ms 1492 KB Output is correct
22 Correct 5 ms 1492 KB Output is correct
23 Correct 5 ms 1464 KB Output is correct
24 Correct 5 ms 1460 KB Output is correct
25 Correct 1 ms 296 KB Output is correct
26 Correct 5 ms 1464 KB Output is correct
27 Correct 5 ms 1492 KB Output is correct
28 Correct 5 ms 1436 KB Output is correct
29 Correct 7 ms 1464 KB Output is correct
30 Correct 6 ms 1236 KB Output is correct
31 Correct 5 ms 1340 KB Output is correct
32 Correct 5 ms 1336 KB Output is correct
33 Correct 6 ms 1248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 5 ms 1492 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 5 ms 1464 KB Output is correct
21 Correct 5 ms 1492 KB Output is correct
22 Correct 5 ms 1492 KB Output is correct
23 Correct 5 ms 1464 KB Output is correct
24 Correct 5 ms 1460 KB Output is correct
25 Incorrect 232 ms 51140 KB Output isn't correct
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 319 ms 41704 KB Output is correct
4 Correct 291 ms 43088 KB Output is correct
5 Correct 293 ms 43064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 5 ms 1492 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 5 ms 1464 KB Output is correct
21 Correct 5 ms 1492 KB Output is correct
22 Correct 5 ms 1492 KB Output is correct
23 Correct 5 ms 1464 KB Output is correct
24 Correct 5 ms 1460 KB Output is correct
25 Correct 1 ms 296 KB Output is correct
26 Correct 5 ms 1464 KB Output is correct
27 Correct 5 ms 1492 KB Output is correct
28 Correct 5 ms 1436 KB Output is correct
29 Correct 7 ms 1464 KB Output is correct
30 Correct 6 ms 1236 KB Output is correct
31 Correct 5 ms 1340 KB Output is correct
32 Correct 5 ms 1336 KB Output is correct
33 Correct 6 ms 1248 KB Output is correct
34 Incorrect 232 ms 51140 KB Output isn't correct
35 Halted 0 ms 0 KB -