Submission #1040236

# Submission time Handle Problem Language Result Execution time Memory
1040236 2024-07-31T19:14:19 Z TAhmed33 Cat Exercise (JOI23_ho_t4) C++
100 / 100
264 ms 73808 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 25;
const int B = 19;
typedef long long ll;
int a[MAXN], n;
vector <int> adj[MAXN];
struct DSU {
    int par[MAXN];
    void init () {
        for (int i = 1; i <= n; i++) {
            par[i] = i; 
        }
    }
    int find (int x) {
        return par[x] == x ? x : par[x] = find(par[x]);
    }
    void merge (int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return;
        if (a[x] > a[y]) {
            par[y] = x;
        } else {
            par[x] = y;
        }
    }
} cur;
vector <int> adj2[MAXN];
int dp[B][MAXN], dep[MAXN];
int jump (int x, int y) {
    for (int i = B - 1; i >= 0; i--) {
        if (y & (1 << i)) {
            x = dp[i][x];
        }
    }
    return x;
}
int lca (int x, int y) {
    if (dep[x] > dep[y]) swap(x, y);
    y = jump(y, dep[y] - dep[x]);
    if (x == y) return x ;
    for (int i = B - 1; i >= 0; i--) {
        if (dp[i][x] != dp[i][y]) {
            x = dp[i][x], y = dp[i][y];
        }
    }
    return dp[0][x];
}
ll dist (int x, int y) {
    return dep[x] + dep[y] - 2 * dep[lca(x, y)];
}
ll dfs (int pos) {
    if (adj2[pos].empty()) return 0;
    ll mx = 0;
    for (auto j : adj2[pos]) {
        mx = max(mx, dist(j, pos) + dfs(j));
    }
    return mx;
}
void dfs2 (int pos, int par) {
    for (auto j : adj[pos]) {
        if (j != par) {
            dp[0][j] = pos;
            for (int i = 1; i < B; i++) {
                dp[i][j] = dp[i - 1][dp[i - 1][j]];
            }
            dep[j] = 1 + dep[pos];
            dfs2(j, pos);
        }
    }
}
void solve () {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        int x, y; cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    dfs2(1, 0);
    vector <int> ii;
    for (int i = 1; i <= n; i++) {
        ii.push_back(i);
    }
    sort(ii.begin(), ii.end(), [&] (int x, int y) {
        return a[x] < a[y];
    });
    cur.init();
    for (auto i : ii) {
        for (auto j : adj[i]) {
            if (a[j] > a[i]) {
                continue;
            }
            adj2[i].push_back(cur.find(j));
            cur.merge(i, cur.find(j));
        }
    }
    int root = -1;
    for (int i = 1; i <= n; i++) {
        if (a[i] == n) {
            root = i;
        }
    }
    cout << dfs(root) << '\n';
}       
signed main () {
    ios::sync_with_stdio(0); cin.tie(0);
    int tc = 1; //cin >> tc;
    while (tc--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 5 ms 10332 KB Output is correct
3 Correct 3 ms 10332 KB Output is correct
4 Correct 3 ms 10332 KB Output is correct
5 Correct 3 ms 10332 KB Output is correct
6 Correct 3 ms 10332 KB Output is correct
7 Correct 3 ms 10332 KB Output is correct
8 Correct 4 ms 10332 KB Output is correct
9 Correct 3 ms 10332 KB Output is correct
10 Correct 3 ms 10332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 5 ms 10332 KB Output is correct
3 Correct 3 ms 10332 KB Output is correct
4 Correct 3 ms 10332 KB Output is correct
5 Correct 3 ms 10332 KB Output is correct
6 Correct 3 ms 10332 KB Output is correct
7 Correct 3 ms 10332 KB Output is correct
8 Correct 4 ms 10332 KB Output is correct
9 Correct 3 ms 10332 KB Output is correct
10 Correct 3 ms 10332 KB Output is correct
11 Correct 4 ms 10332 KB Output is correct
12 Correct 3 ms 10332 KB Output is correct
13 Correct 3 ms 10372 KB Output is correct
14 Correct 3 ms 10332 KB Output is correct
15 Correct 3 ms 10332 KB Output is correct
16 Correct 3 ms 10332 KB Output is correct
17 Correct 4 ms 10332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 5 ms 10332 KB Output is correct
3 Correct 3 ms 10332 KB Output is correct
4 Correct 3 ms 10332 KB Output is correct
5 Correct 3 ms 10332 KB Output is correct
6 Correct 3 ms 10332 KB Output is correct
7 Correct 3 ms 10332 KB Output is correct
8 Correct 4 ms 10332 KB Output is correct
9 Correct 3 ms 10332 KB Output is correct
10 Correct 3 ms 10332 KB Output is correct
11 Correct 4 ms 10332 KB Output is correct
12 Correct 3 ms 10332 KB Output is correct
13 Correct 3 ms 10372 KB Output is correct
14 Correct 3 ms 10332 KB Output is correct
15 Correct 3 ms 10332 KB Output is correct
16 Correct 3 ms 10332 KB Output is correct
17 Correct 4 ms 10332 KB Output is correct
18 Correct 5 ms 11920 KB Output is correct
19 Correct 6 ms 11868 KB Output is correct
20 Correct 5 ms 11868 KB Output is correct
21 Correct 5 ms 11868 KB Output is correct
22 Correct 5 ms 11868 KB Output is correct
23 Correct 5 ms 11868 KB Output is correct
24 Correct 5 ms 11868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 5 ms 10332 KB Output is correct
3 Correct 3 ms 10332 KB Output is correct
4 Correct 3 ms 10332 KB Output is correct
5 Correct 3 ms 10332 KB Output is correct
6 Correct 3 ms 10332 KB Output is correct
7 Correct 3 ms 10332 KB Output is correct
8 Correct 4 ms 10332 KB Output is correct
9 Correct 3 ms 10332 KB Output is correct
10 Correct 3 ms 10332 KB Output is correct
11 Correct 4 ms 10332 KB Output is correct
12 Correct 3 ms 10332 KB Output is correct
13 Correct 3 ms 10372 KB Output is correct
14 Correct 3 ms 10332 KB Output is correct
15 Correct 3 ms 10332 KB Output is correct
16 Correct 3 ms 10332 KB Output is correct
17 Correct 4 ms 10332 KB Output is correct
18 Correct 5 ms 11920 KB Output is correct
19 Correct 6 ms 11868 KB Output is correct
20 Correct 5 ms 11868 KB Output is correct
21 Correct 5 ms 11868 KB Output is correct
22 Correct 5 ms 11868 KB Output is correct
23 Correct 5 ms 11868 KB Output is correct
24 Correct 5 ms 11868 KB Output is correct
25 Correct 3 ms 10332 KB Output is correct
26 Correct 6 ms 11868 KB Output is correct
27 Correct 5 ms 11868 KB Output is correct
28 Correct 6 ms 11892 KB Output is correct
29 Correct 6 ms 11868 KB Output is correct
30 Correct 6 ms 11612 KB Output is correct
31 Correct 5 ms 11740 KB Output is correct
32 Correct 6 ms 11612 KB Output is correct
33 Correct 6 ms 11612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 5 ms 10332 KB Output is correct
3 Correct 3 ms 10332 KB Output is correct
4 Correct 3 ms 10332 KB Output is correct
5 Correct 3 ms 10332 KB Output is correct
6 Correct 3 ms 10332 KB Output is correct
7 Correct 3 ms 10332 KB Output is correct
8 Correct 4 ms 10332 KB Output is correct
9 Correct 3 ms 10332 KB Output is correct
10 Correct 3 ms 10332 KB Output is correct
11 Correct 4 ms 10332 KB Output is correct
12 Correct 3 ms 10332 KB Output is correct
13 Correct 3 ms 10372 KB Output is correct
14 Correct 3 ms 10332 KB Output is correct
15 Correct 3 ms 10332 KB Output is correct
16 Correct 3 ms 10332 KB Output is correct
17 Correct 4 ms 10332 KB Output is correct
18 Correct 5 ms 11920 KB Output is correct
19 Correct 6 ms 11868 KB Output is correct
20 Correct 5 ms 11868 KB Output is correct
21 Correct 5 ms 11868 KB Output is correct
22 Correct 5 ms 11868 KB Output is correct
23 Correct 5 ms 11868 KB Output is correct
24 Correct 5 ms 11868 KB Output is correct
25 Correct 100 ms 73808 KB Output is correct
26 Correct 92 ms 71788 KB Output is correct
27 Correct 89 ms 71876 KB Output is correct
28 Correct 111 ms 68796 KB Output is correct
29 Correct 112 ms 68800 KB Output is correct
30 Correct 114 ms 68796 KB Output is correct
31 Correct 109 ms 68620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 3 ms 10332 KB Output is correct
3 Correct 137 ms 62788 KB Output is correct
4 Correct 131 ms 62920 KB Output is correct
5 Correct 130 ms 62764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 5 ms 10332 KB Output is correct
3 Correct 3 ms 10332 KB Output is correct
4 Correct 3 ms 10332 KB Output is correct
5 Correct 3 ms 10332 KB Output is correct
6 Correct 3 ms 10332 KB Output is correct
7 Correct 3 ms 10332 KB Output is correct
8 Correct 4 ms 10332 KB Output is correct
9 Correct 3 ms 10332 KB Output is correct
10 Correct 3 ms 10332 KB Output is correct
11 Correct 4 ms 10332 KB Output is correct
12 Correct 3 ms 10332 KB Output is correct
13 Correct 3 ms 10372 KB Output is correct
14 Correct 3 ms 10332 KB Output is correct
15 Correct 3 ms 10332 KB Output is correct
16 Correct 3 ms 10332 KB Output is correct
17 Correct 4 ms 10332 KB Output is correct
18 Correct 5 ms 11920 KB Output is correct
19 Correct 6 ms 11868 KB Output is correct
20 Correct 5 ms 11868 KB Output is correct
21 Correct 5 ms 11868 KB Output is correct
22 Correct 5 ms 11868 KB Output is correct
23 Correct 5 ms 11868 KB Output is correct
24 Correct 5 ms 11868 KB Output is correct
25 Correct 3 ms 10332 KB Output is correct
26 Correct 6 ms 11868 KB Output is correct
27 Correct 5 ms 11868 KB Output is correct
28 Correct 6 ms 11892 KB Output is correct
29 Correct 6 ms 11868 KB Output is correct
30 Correct 6 ms 11612 KB Output is correct
31 Correct 5 ms 11740 KB Output is correct
32 Correct 6 ms 11612 KB Output is correct
33 Correct 6 ms 11612 KB Output is correct
34 Correct 100 ms 73808 KB Output is correct
35 Correct 92 ms 71788 KB Output is correct
36 Correct 89 ms 71876 KB Output is correct
37 Correct 111 ms 68796 KB Output is correct
38 Correct 112 ms 68800 KB Output is correct
39 Correct 114 ms 68796 KB Output is correct
40 Correct 109 ms 68620 KB Output is correct
41 Correct 4 ms 10332 KB Output is correct
42 Correct 3 ms 10332 KB Output is correct
43 Correct 137 ms 62788 KB Output is correct
44 Correct 131 ms 62920 KB Output is correct
45 Correct 130 ms 62764 KB Output is correct
46 Correct 244 ms 72648 KB Output is correct
47 Correct 235 ms 72640 KB Output is correct
48 Correct 216 ms 72648 KB Output is correct
49 Correct 264 ms 72616 KB Output is correct
50 Correct 197 ms 61380 KB Output is correct
51 Correct 193 ms 61380 KB Output is correct
52 Correct 195 ms 61136 KB Output is correct
53 Correct 199 ms 61380 KB Output is correct