Submission #922577

# Submission time Handle Problem Language Result Execution time Memory
922577 2024-02-05T17:10:40 Z vjudge1 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
249 ms 52860 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll L = 20;
const ll N = 2e5 + 50;
const ll Mod = 1e9 + 7;

int n, p[N], ver[N], h[N], par[N][L], pr[N], sz[N], mxh[N]; ll val[N];

vector<int> adj[N];

void dfs(int v, int p = 0){
    par[v][0] = p;
    for(int i = 1; i < L; i++){
        if(!par[v][i-1]) break;
        par[v][i] = par[par[v][i-1]][i-1];
    }

    for(int u: adj[v]){
        if(u == p) continue;
        h[u] = h[v] + 1; dfs(u, v);
    }
}

int getkth(int v, int k){
    for(int i = 0; i < L; i++){
        if(k >> i & 1) v = par[v][i];
    }
    return v;
}

int lca(int u, int v){
    if(h[v] < h[u]) swap(u, v);
    v = getkth(v, h[v] - h[u]);
    if(u == v) return v;

    for(int i = L-1; i >= 0; i--){
        if(par[v][i] != par[u][i]){
            v = par[v][i]; u = par[u][i];
        }
    }
    return par[v][0];
}

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

int getPar(int v){
    return (pr[v] == v ? v : pr[v] = getPar(pr[v]));
}

void Union(int u, int v, int a, ll b){
    u = getPar(u);
    v = getPar(v);
    if(u == v) return;
    if(sz[v] < sz[u]) swap(u, v);

    sz[v] += sz[u];
    pr[u] = v;
    mxh[v] = a;
    val[v] = b;
}

int main(){
    fast_io;
    
    cin >> n;

    for(int i = 1; i <= n; i++){
        cin >> p[i]; ver[p[i]] = i;
    }

    int u, v;
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    dfs(1);

    for(int i = 1; i <= n; i++){
        pr[i] = i; sz[i] = 1; mxh[i] = p[i];
    }

    for(int i = 1; i <= n; i++){
        v = ver[i];
        ll ans = 0;
        for(int u: adj[v]){
            if(p[u] > p[v]) continue;
            int up = getPar(u);
            ans = max(ans, val[up] + dist(ver[mxh[up]], v));
        }
        for(int u: adj[v]){
            if(p[u] > p[v]) continue;
            Union(u, v, i, ans);
        }

        if(i == n) cout << ans << endl;
    }

}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 3 ms 10896 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 3 ms 10896 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10840 KB Output is correct
16 Correct 2 ms 10840 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 3 ms 10896 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10840 KB Output is correct
16 Correct 2 ms 10840 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
18 Correct 5 ms 11356 KB Output is correct
19 Correct 4 ms 11356 KB Output is correct
20 Correct 5 ms 11584 KB Output is correct
21 Correct 6 ms 11356 KB Output is correct
22 Correct 4 ms 11356 KB Output is correct
23 Correct 5 ms 11520 KB Output is correct
24 Correct 5 ms 11356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 3 ms 10896 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10840 KB Output is correct
16 Correct 2 ms 10840 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
18 Correct 5 ms 11356 KB Output is correct
19 Correct 4 ms 11356 KB Output is correct
20 Correct 5 ms 11584 KB Output is correct
21 Correct 6 ms 11356 KB Output is correct
22 Correct 4 ms 11356 KB Output is correct
23 Correct 5 ms 11520 KB Output is correct
24 Correct 5 ms 11356 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 4 ms 11356 KB Output is correct
28 Correct 4 ms 11356 KB Output is correct
29 Correct 5 ms 11352 KB Output is correct
30 Correct 4 ms 11416 KB Output is correct
31 Correct 4 ms 11100 KB Output is correct
32 Correct 4 ms 11100 KB Output is correct
33 Correct 4 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 3 ms 10896 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10840 KB Output is correct
16 Correct 2 ms 10840 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
18 Correct 5 ms 11356 KB Output is correct
19 Correct 4 ms 11356 KB Output is correct
20 Correct 5 ms 11584 KB Output is correct
21 Correct 6 ms 11356 KB Output is correct
22 Correct 4 ms 11356 KB Output is correct
23 Correct 5 ms 11520 KB Output is correct
24 Correct 5 ms 11356 KB Output is correct
25 Correct 124 ms 52560 KB Output is correct
26 Correct 98 ms 52708 KB Output is correct
27 Correct 111 ms 52860 KB Output is correct
28 Correct 168 ms 52760 KB Output is correct
29 Correct 144 ms 52760 KB Output is correct
30 Correct 150 ms 52764 KB Output is correct
31 Correct 158 ms 52768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 131 ms 37008 KB Output is correct
4 Correct 133 ms 36996 KB Output is correct
5 Correct 134 ms 36760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 3 ms 10896 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10840 KB Output is correct
16 Correct 2 ms 10840 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
18 Correct 5 ms 11356 KB Output is correct
19 Correct 4 ms 11356 KB Output is correct
20 Correct 5 ms 11584 KB Output is correct
21 Correct 6 ms 11356 KB Output is correct
22 Correct 4 ms 11356 KB Output is correct
23 Correct 5 ms 11520 KB Output is correct
24 Correct 5 ms 11356 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 4 ms 11356 KB Output is correct
28 Correct 4 ms 11356 KB Output is correct
29 Correct 5 ms 11352 KB Output is correct
30 Correct 4 ms 11416 KB Output is correct
31 Correct 4 ms 11100 KB Output is correct
32 Correct 4 ms 11100 KB Output is correct
33 Correct 4 ms 11100 KB Output is correct
34 Correct 124 ms 52560 KB Output is correct
35 Correct 98 ms 52708 KB Output is correct
36 Correct 111 ms 52860 KB Output is correct
37 Correct 168 ms 52760 KB Output is correct
38 Correct 144 ms 52760 KB Output is correct
39 Correct 150 ms 52764 KB Output is correct
40 Correct 158 ms 52768 KB Output is correct
41 Correct 3 ms 10844 KB Output is correct
42 Correct 2 ms 10844 KB Output is correct
43 Correct 131 ms 37008 KB Output is correct
44 Correct 133 ms 36996 KB Output is correct
45 Correct 134 ms 36760 KB Output is correct
46 Correct 249 ms 45588 KB Output is correct
47 Correct 234 ms 44356 KB Output is correct
48 Correct 242 ms 46436 KB Output is correct
49 Correct 232 ms 44380 KB Output is correct
50 Correct 191 ms 37456 KB Output is correct
51 Correct 185 ms 37024 KB Output is correct
52 Correct 192 ms 37224 KB Output is correct
53 Correct 225 ms 37320 KB Output is correct