답안 #769674

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
769674 2023-06-30T02:18:35 Z Ozy Cat Exercise (JOI23_ho_t4) C++17
100 / 100
372 ms 68412 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define lli long long int
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define pll pair<lli,lli>

#define MAX 200000
//para el orden
#define id second

lli n,raiz,a,b,cont;
lli alt[MAX+2],anc[MAX+2][22];
vector<lli> hijos[MAX+2];
pll euler[MAX+2];
vector<pll> orden;
lli dp[MAX+2],uf[MAX+2];

lli sube(lli pos) {
    if (uf[pos] == pos) return pos;
    uf[pos] = sube(uf[pos]);
    return uf[pos];
}

void dfs(lli pos, lli padre) {

    anc[pos][0] = padre;
    rep(i,1,20) {
        anc[pos][i] = anc[padre][i-1];
        padre = anc[pos][i];
    }

    padre = anc[pos][0];
    euler[pos].first = cont++;
    for(auto h : hijos[pos]) {
        if (h==padre) continue;
        dfs(h,pos);
    }
    euler[pos].second = cont++;
}

lli dis(lli a, lli b) {

    //debugsl(a);
    //debug(b);

    lli x,res = 0;
    repa(i,20,0) {
        x = anc[a][i];
        if (x == 0) continue;
        if (euler[x].first < euler[b].first && euler[x].second > euler[b].second) continue;
        a = x;
        res += (1<<i);
    }
    if (euler[a].first <= euler[b].first && euler[a].second >= euler[b].second) x = a;
    else {
        a = anc[a][0];
        res++;
    }

    //debugsl(a);
    //debugsl(res);

    repa(i,20,0) {
        x = anc[b][i];
        if (x == 0) continue;
        if (euler[x].first < euler[a].first && euler[x].second > euler[a].second) continue;
        b = x;
        res += (1<<i);
    }

    //debugsl(b);
    //debug(res);

    return res;
}

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

    cin >> n;
    rep(i,1,n) {
        cin >> alt[i];
        if (alt[i] == n) raiz = i;
        orden.push_back({alt[i],i});
    }

    rep(i,1,n-1) {
        cin >> a >> b;
        hijos[a].push_back(b);
        hijos[b].push_back(a);
    }

    //crea el lca
    cont = 1;
    dfs(raiz,0);

    //empieza con el union find
    sort(orden.begin(), orden.end());
    rep(i,1,n) uf[i] = i;

    for (auto act : orden) {

        for (auto h : hijos[act.id]) {
            if (alt[h] > act.first) continue;

            b = sube(h);
            a = dis(act.id,b) + dp[b];
            dp[act.id] = max(dp[act.id], a);
            uf[b] = act.id;
        }

    }

    cout << dp[raiz];

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4996 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4996 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 5 ms 6596 KB Output is correct
19 Correct 5 ms 6484 KB Output is correct
20 Correct 7 ms 6484 KB Output is correct
21 Correct 5 ms 6536 KB Output is correct
22 Correct 5 ms 6484 KB Output is correct
23 Correct 5 ms 6484 KB Output is correct
24 Correct 5 ms 6356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4996 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 5 ms 6596 KB Output is correct
19 Correct 5 ms 6484 KB Output is correct
20 Correct 7 ms 6484 KB Output is correct
21 Correct 5 ms 6536 KB Output is correct
22 Correct 5 ms 6484 KB Output is correct
23 Correct 5 ms 6484 KB Output is correct
24 Correct 5 ms 6356 KB Output is correct
25 Correct 2 ms 4948 KB Output is correct
26 Correct 8 ms 6612 KB Output is correct
27 Correct 5 ms 6484 KB Output is correct
28 Correct 6 ms 6452 KB Output is correct
29 Correct 7 ms 6488 KB Output is correct
30 Correct 6 ms 6368 KB Output is correct
31 Correct 6 ms 6356 KB Output is correct
32 Correct 6 ms 6356 KB Output is correct
33 Correct 6 ms 6392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4996 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 5 ms 6596 KB Output is correct
19 Correct 5 ms 6484 KB Output is correct
20 Correct 7 ms 6484 KB Output is correct
21 Correct 5 ms 6536 KB Output is correct
22 Correct 5 ms 6484 KB Output is correct
23 Correct 5 ms 6484 KB Output is correct
24 Correct 5 ms 6356 KB Output is correct
25 Correct 156 ms 66056 KB Output is correct
26 Correct 165 ms 66040 KB Output is correct
27 Correct 158 ms 65988 KB Output is correct
28 Correct 213 ms 63636 KB Output is correct
29 Correct 203 ms 64892 KB Output is correct
30 Correct 215 ms 64636 KB Output is correct
31 Correct 208 ms 65004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5060 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 206 ms 60420 KB Output is correct
4 Correct 231 ms 60408 KB Output is correct
5 Correct 204 ms 60400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4996 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 5 ms 6596 KB Output is correct
19 Correct 5 ms 6484 KB Output is correct
20 Correct 7 ms 6484 KB Output is correct
21 Correct 5 ms 6536 KB Output is correct
22 Correct 5 ms 6484 KB Output is correct
23 Correct 5 ms 6484 KB Output is correct
24 Correct 5 ms 6356 KB Output is correct
25 Correct 2 ms 4948 KB Output is correct
26 Correct 8 ms 6612 KB Output is correct
27 Correct 5 ms 6484 KB Output is correct
28 Correct 6 ms 6452 KB Output is correct
29 Correct 7 ms 6488 KB Output is correct
30 Correct 6 ms 6368 KB Output is correct
31 Correct 6 ms 6356 KB Output is correct
32 Correct 6 ms 6356 KB Output is correct
33 Correct 6 ms 6392 KB Output is correct
34 Correct 156 ms 66056 KB Output is correct
35 Correct 165 ms 66040 KB Output is correct
36 Correct 158 ms 65988 KB Output is correct
37 Correct 213 ms 63636 KB Output is correct
38 Correct 203 ms 64892 KB Output is correct
39 Correct 215 ms 64636 KB Output is correct
40 Correct 208 ms 65004 KB Output is correct
41 Correct 2 ms 5060 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 206 ms 60420 KB Output is correct
44 Correct 231 ms 60408 KB Output is correct
45 Correct 204 ms 60400 KB Output is correct
46 Correct 372 ms 65800 KB Output is correct
47 Correct 294 ms 67492 KB Output is correct
48 Correct 358 ms 68412 KB Output is correct
49 Correct 310 ms 67464 KB Output is correct
50 Correct 314 ms 61320 KB Output is correct
51 Correct 290 ms 61368 KB Output is correct
52 Correct 287 ms 61372 KB Output is correct
53 Correct 306 ms 61452 KB Output is correct