답안 #758416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
758416 2023-06-14T15:20:17 Z Jarif_Rahman Cat Exercise (JOI23_ho_t4) C++17
100 / 100
383 ms 52248 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

struct segtree{
    int k;
    vector<int> v;
    segtree(int n){
        k = 1;
        while(k < n) k*=2;
        k*=2;
        v.resize(k, -1);
    }
    void upd(int in, int x){
        in+=k/2;
        v[in] = x;
        in/=2;
        while(in > 0){
            v[in] = max(v[2*in], v[2*in+1]);
            in/=2;
        }
    }
    int get(int l, int r, int nd, int a, int b){
        if(b < l || a > r) return -1;
        if(a >= l && b <= r) return v[nd];
        int c = (a+b)/2;
        return max(get(l, r, 2*nd, a, c), get(l, r, 2*nd+1, c+1, b));
    }
    int get(int l, int r){
        return get(l, r, 1, 0, k/2-1);
    }
};

const int K = 18;
int n;
vector<vector<int>> v;
vector<int> p, pp;
vector<int> anc[K];
vector<int> depth, sz;
vector<int> pos;

int intime = 0;
void pre_dfs(int nd, int ss, int d){
    pos[nd] = intime++;
    depth[nd] = d;
    if(ss != -1) anc[0][nd] = ss;
    for(int x: v[nd]) if(x != ss) pre_dfs(x, nd, d+1), sz[nd]+=sz[x];
}

int get_anc(int nd, int h){
    for(int i = 0; i < K; i++){
        if(h&1) nd = anc[i][nd];
        h>>=1;
    }
    return nd;
}
int lca(int a, int b){
    if(depth[a] > depth[b]) swap(a, b);
    b = get_anc(b, depth[b]-depth[a]);
    if(a == b) return a;

    for(int i = K-1; i >= 0; i--) if(anc[i][a] != anc[i][b])
        a = anc[i][a], b = anc[i][b];

    return anc[0][a];
}
int dis(int a, int b){
    return depth[a]+depth[b]-2*depth[lca(a, b)];
}

ll ans = 0;
segtree seg(0);

void dfs(int nd, int ss, ll cur){
    ans = max(ans, cur);
    seg.upd(pos[nd], -1);

    for(int x: v[nd]) if(x != anc[0][nd]){
        int nxt = seg.get(pos[x], pos[x]+sz[x]-1);
        if(nxt == -1) continue;
        nxt = pp[nxt];
        dfs(nxt, x, cur+dis(nd, nxt));
    }

    int nxt = seg.get(pos[ss], pos[ss]+sz[ss]-1);
    if(nxt == -1) return;
    nxt = pp[nxt];
    dfs(nxt, ss, cur+dis(nd, nxt));
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    v.resize(n);
    p.resize(n), pp.resize(n);
    fill(anc, anc+K, vector<int>(n, 0));
    depth.resize(n), sz.assign(n, 1);
    pos.resize(n);

    for(int &x: p) cin >> x, x--;
    for(int i = 0; i < n; i++) pp[p[i]] = i;

    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[a].pb(b);
        v[b].pb(a);
    }

    pre_dfs(0, -1, 0);
    for(int p = 1; p < K; p++) for(int i = 0; i < n; i++) anc[p][i] = anc[p-1][anc[p-1][i]];

    seg = segtree(n);
    for(int i = 0; i < n; i++) seg.upd(pos[i], p[i]);

    dfs(pp[n-1], 0, 0);
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 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 4 ms 1484 KB Output is correct
21 Correct 4 ms 1480 KB Output is correct
22 Correct 4 ms 1492 KB Output is correct
23 Correct 5 ms 1492 KB Output is correct
24 Correct 6 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 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 4 ms 1484 KB Output is correct
21 Correct 4 ms 1480 KB Output is correct
22 Correct 4 ms 1492 KB Output is correct
23 Correct 5 ms 1492 KB Output is correct
24 Correct 6 ms 1492 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 5 ms 1364 KB Output is correct
27 Correct 4 ms 1348 KB Output is correct
28 Correct 4 ms 1364 KB Output is correct
29 Correct 5 ms 1364 KB Output is correct
30 Correct 5 ms 1100 KB Output is correct
31 Correct 5 ms 1108 KB Output is correct
32 Correct 5 ms 1096 KB Output is correct
33 Correct 5 ms 1084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 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 4 ms 1484 KB Output is correct
21 Correct 4 ms 1480 KB Output is correct
22 Correct 4 ms 1492 KB Output is correct
23 Correct 5 ms 1492 KB Output is correct
24 Correct 6 ms 1492 KB Output is correct
25 Correct 182 ms 52248 KB Output is correct
26 Correct 190 ms 50868 KB Output is correct
27 Correct 188 ms 50748 KB Output is correct
28 Correct 196 ms 50712 KB Output is correct
29 Correct 193 ms 50560 KB Output is correct
30 Correct 185 ms 50584 KB Output is correct
31 Correct 189 ms 50716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 206 ms 35060 KB Output is correct
4 Correct 200 ms 35040 KB Output is correct
5 Correct 199 ms 35044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 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 4 ms 1484 KB Output is correct
21 Correct 4 ms 1480 KB Output is correct
22 Correct 4 ms 1492 KB Output is correct
23 Correct 5 ms 1492 KB Output is correct
24 Correct 6 ms 1492 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 5 ms 1364 KB Output is correct
27 Correct 4 ms 1348 KB Output is correct
28 Correct 4 ms 1364 KB Output is correct
29 Correct 5 ms 1364 KB Output is correct
30 Correct 5 ms 1100 KB Output is correct
31 Correct 5 ms 1108 KB Output is correct
32 Correct 5 ms 1096 KB Output is correct
33 Correct 5 ms 1084 KB Output is correct
34 Correct 182 ms 52248 KB Output is correct
35 Correct 190 ms 50868 KB Output is correct
36 Correct 188 ms 50748 KB Output is correct
37 Correct 196 ms 50712 KB Output is correct
38 Correct 193 ms 50560 KB Output is correct
39 Correct 185 ms 50584 KB Output is correct
40 Correct 189 ms 50716 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 206 ms 35060 KB Output is correct
44 Correct 200 ms 35040 KB Output is correct
45 Correct 199 ms 35044 KB Output is correct
46 Correct 377 ms 43824 KB Output is correct
47 Correct 362 ms 42496 KB Output is correct
48 Correct 353 ms 44404 KB Output is correct
49 Correct 355 ms 42440 KB Output is correct
50 Correct 383 ms 35228 KB Output is correct
51 Correct 361 ms 35316 KB Output is correct
52 Correct 357 ms 35268 KB Output is correct
53 Correct 373 ms 35280 KB Output is correct