답안 #891660

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891660 2023-12-23T13:47:55 Z PagodePaiva Cat Exercise (JOI23_ho_t4) C++17
100 / 100
946 ms 65972 KB
#include<bits/stdc++.h>
#define N 200010
#define LOGN 20
#define int long long

using namespace std;

struct DSU{
    int pai[N];   
    DSU(){
        for(int i = 0;i < N;i++){
            pai[i] = i;
        }
    }
    int find(int x){
        return (x == pai[x] ? x : find(pai[x]));
    }
    void join(int a, int b){
        b = find(b);
        pai[b] = a;
        return;                
    }
} dsu;

int n;
int p[N];
vector <pair <int,int>> ord;
vector <int> g[N];
int dp[N];
int h[N], pai[N][LOGN];

void dfs(int v, int p){
    pai[v][0] = p;
    for(auto x : g[v]){
        if(x == p) continue;
        h[x] = h[v]+1;
        dfs(x, v);
    }
    return;
}

int query(int x, int y){
    if(h[x] > h[y]) swap(x, y);
    int t = h[y] - h[x];
    int res = 0;
    for(int i = 0;i < LOGN;i++){
        if((t & (1 << i))){
            int pp = (1 << i);
            res += pp;
            y = pai[y][i];
        }
    }
    if(x == y) return res;
    for(int i = LOGN-1;i >= 0;i--){
        if(pai[x][i] != pai[y][i]){
            x = pai[x][i];
            y = pai[y][i];
            int pp = (1 << i);
            res += 2*pp;
        }
    }
    return res+2;
}

int32_t main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        int x;
        cin >> x;
        p[i] = x;
        ord.push_back({x, i});
    }
    sort(ord.begin(), ord.end());
    for(int i = 1;i < n;i++){
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 1);
    for(int i = 1;i < LOGN;i++){
        for(int v = 1;v <= n;v++){
            pai[v][i] = pai[pai[v][i-1]][i-1];
        }
    }
    for(auto aa : ord){
        int v = aa.second;
        for(auto x : g[v]){
            if(p[x] > p[v]) continue;
            int t = dsu.find(x);
            dp[p[v]] = max(dp[p[v]], dp[p[t]] + query(v, t));
            dsu.join(v, t);
        }   
    }
    cout << dp[n] << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9688 KB Output is correct
3 Correct 2 ms 9560 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9688 KB Output is correct
3 Correct 2 ms 9560 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9688 KB Output is correct
3 Correct 2 ms 9560 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9704 KB Output is correct
18 Correct 6 ms 12380 KB Output is correct
19 Correct 6 ms 12380 KB Output is correct
20 Correct 6 ms 12376 KB Output is correct
21 Correct 6 ms 12380 KB Output is correct
22 Correct 6 ms 12380 KB Output is correct
23 Correct 7 ms 12380 KB Output is correct
24 Correct 6 ms 12560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9688 KB Output is correct
3 Correct 2 ms 9560 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9704 KB Output is correct
18 Correct 6 ms 12380 KB Output is correct
19 Correct 6 ms 12380 KB Output is correct
20 Correct 6 ms 12376 KB Output is correct
21 Correct 6 ms 12380 KB Output is correct
22 Correct 6 ms 12380 KB Output is correct
23 Correct 7 ms 12380 KB Output is correct
24 Correct 6 ms 12560 KB Output is correct
25 Correct 2 ms 9704 KB Output is correct
26 Correct 6 ms 12380 KB Output is correct
27 Correct 6 ms 12380 KB Output is correct
28 Correct 6 ms 12292 KB Output is correct
29 Correct 6 ms 12380 KB Output is correct
30 Correct 7 ms 12124 KB Output is correct
31 Correct 6 ms 12124 KB Output is correct
32 Correct 6 ms 12248 KB Output is correct
33 Correct 6 ms 12252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9688 KB Output is correct
3 Correct 2 ms 9560 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9704 KB Output is correct
18 Correct 6 ms 12380 KB Output is correct
19 Correct 6 ms 12380 KB Output is correct
20 Correct 6 ms 12376 KB Output is correct
21 Correct 6 ms 12380 KB Output is correct
22 Correct 6 ms 12380 KB Output is correct
23 Correct 7 ms 12380 KB Output is correct
24 Correct 6 ms 12560 KB Output is correct
25 Correct 215 ms 65972 KB Output is correct
26 Correct 215 ms 65128 KB Output is correct
27 Correct 207 ms 65904 KB Output is correct
28 Correct 242 ms 65808 KB Output is correct
29 Correct 245 ms 65216 KB Output is correct
30 Correct 245 ms 65204 KB Output is correct
31 Correct 253 ms 65268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 783 ms 57324 KB Output is correct
4 Correct 946 ms 58036 KB Output is correct
5 Correct 653 ms 57536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9688 KB Output is correct
3 Correct 2 ms 9560 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9704 KB Output is correct
18 Correct 6 ms 12380 KB Output is correct
19 Correct 6 ms 12380 KB Output is correct
20 Correct 6 ms 12376 KB Output is correct
21 Correct 6 ms 12380 KB Output is correct
22 Correct 6 ms 12380 KB Output is correct
23 Correct 7 ms 12380 KB Output is correct
24 Correct 6 ms 12560 KB Output is correct
25 Correct 2 ms 9704 KB Output is correct
26 Correct 6 ms 12380 KB Output is correct
27 Correct 6 ms 12380 KB Output is correct
28 Correct 6 ms 12292 KB Output is correct
29 Correct 6 ms 12380 KB Output is correct
30 Correct 7 ms 12124 KB Output is correct
31 Correct 6 ms 12124 KB Output is correct
32 Correct 6 ms 12248 KB Output is correct
33 Correct 6 ms 12252 KB Output is correct
34 Correct 215 ms 65972 KB Output is correct
35 Correct 215 ms 65128 KB Output is correct
36 Correct 207 ms 65904 KB Output is correct
37 Correct 242 ms 65808 KB Output is correct
38 Correct 245 ms 65216 KB Output is correct
39 Correct 245 ms 65204 KB Output is correct
40 Correct 253 ms 65268 KB Output is correct
41 Correct 2 ms 9560 KB Output is correct
42 Correct 2 ms 9564 KB Output is correct
43 Correct 783 ms 57324 KB Output is correct
44 Correct 946 ms 58036 KB Output is correct
45 Correct 653 ms 57536 KB Output is correct
46 Correct 875 ms 61624 KB Output is correct
47 Correct 871 ms 61364 KB Output is correct
48 Correct 859 ms 62676 KB Output is correct
49 Correct 869 ms 61056 KB Output is correct
50 Correct 327 ms 57824 KB Output is correct
51 Correct 343 ms 56760 KB Output is correct
52 Correct 327 ms 56916 KB Output is correct
53 Correct 332 ms 56912 KB Output is correct