답안 #835040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
835040 2023-08-23T06:41:23 Z isaachew Cat Exercise (JOI23_ho_t4) C++17
100 / 100
300 ms 46380 KB
#include <bits/stdc++.h>
/*
 When you are at node k, you cannot go to any node > k as that is unreachable
 */
struct dsu{
    std::vector<int> parents;
    std::vector<int> maxes;
    dsu(int n){
        parents.resize(n,-1);
        for(int i=0;i<n;i++){
            maxes.push_back(i);
        }
    }
    void combine(int a,int b){
        a=find(a),b=find(b);
        parents[a]=b;
        maxes[b]=std::max(maxes[a],maxes[b]);
    }
    int find(int x){
        if(parents[x]==-1)return x;
        else{
            return (parents[x]=find(parents[x]));
        }
    }
};
std::vector<std::vector<int>> edges;
std::vector<int> depths;
std::vector<int> parents;
std::vector<std::vector<int>> binlifts;
void dfs(int cur,int parent){
    parents[cur]=parent;
    for(int i:edges[cur]){
        if(i==parent)continue;
        depths[i]=depths[cur]+1;
        dfs(i,cur);
    }
}
int lca(int a,int b){
    if(depths[a]<depths[b])std::swap(a,b);
    for(int i=20;i--;){
        if(depths[a]-(1<<i)>=depths[b]){
            a=binlifts[i][a];
        }
    }
    if(a==b)return a;//a is an ancestor/descendant of b
    for(int i=20;i--;){
        int na=binlifts[i][a],nb=binlifts[i][b];
        if(na!=nb){
            a=na,b=nb;
        }
    }
    return parents[a];
}
int getdist(int a,int b){
    int clca=lca(a,b);
    return depths[a]+depths[b]-2*depths[clca];
}
int main(){
    int n;
    std::cin>>n;
    std::vector<int> theights;
    std::vector<long long> scores(n);
    depths.resize(n);
    edges.resize(n);
    binlifts.resize(20);
    parents.resize(n);
    dsu conncs(n);
    for(int i=0;i<n;i++){
        int x;
        std::cin>>x;
        x--;
        theights.push_back(x);
    }
    for(int i=1;i<n;i++){
        int a,b;
        std::cin>>a>>b;
        a=theights[a-1],b=theights[b-1];
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    dfs(0,0);
    binlifts[0]=parents;
    for(int i=0;i<19;i++){
        for(int j=0;j<n;j++){
            binlifts[i+1].push_back(binlifts[i][binlifts[i][j]]);
        }
    }
    for(int i=0;i<n;i++){
        long long cscore=0;
        for(int j:edges[i]){
            if(j>i)continue;
            int jpcc=conncs.maxes[conncs.find(j)];
            cscore=std::max(cscore,scores[jpcc]+getdist(i,jpcc));
            conncs.combine(i,jpcc);
        }
        scores[i]=cscore;
    }
    std::cout<<scores.back()<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 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 304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 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 304 KB Output is correct
18 Correct 5 ms 1236 KB Output is correct
19 Correct 4 ms 1460 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
21 Correct 5 ms 1324 KB Output is correct
22 Correct 4 ms 1364 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 4 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 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 304 KB Output is correct
18 Correct 5 ms 1236 KB Output is correct
19 Correct 4 ms 1460 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
21 Correct 5 ms 1324 KB Output is correct
22 Correct 4 ms 1364 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 4 ms 1364 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 5 ms 1364 KB Output is correct
27 Correct 5 ms 1364 KB Output is correct
28 Correct 4 ms 1364 KB Output is correct
29 Correct 6 ms 1276 KB Output is correct
30 Correct 6 ms 1196 KB Output is correct
31 Correct 4 ms 1108 KB Output is correct
32 Correct 5 ms 1108 KB Output is correct
33 Correct 5 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 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 304 KB Output is correct
18 Correct 5 ms 1236 KB Output is correct
19 Correct 4 ms 1460 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
21 Correct 5 ms 1324 KB Output is correct
22 Correct 4 ms 1364 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 4 ms 1364 KB Output is correct
25 Correct 193 ms 37872 KB Output is correct
26 Correct 199 ms 46268 KB Output is correct
27 Correct 195 ms 46380 KB Output is correct
28 Correct 240 ms 45760 KB Output is correct
29 Correct 236 ms 41916 KB Output is correct
30 Correct 241 ms 44700 KB Output is correct
31 Correct 238 ms 43812 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 252 ms 33168 KB Output is correct
4 Correct 237 ms 33272 KB Output is correct
5 Correct 249 ms 33252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 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 304 KB Output is correct
18 Correct 5 ms 1236 KB Output is correct
19 Correct 4 ms 1460 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
21 Correct 5 ms 1324 KB Output is correct
22 Correct 4 ms 1364 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 4 ms 1364 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 5 ms 1364 KB Output is correct
27 Correct 5 ms 1364 KB Output is correct
28 Correct 4 ms 1364 KB Output is correct
29 Correct 6 ms 1276 KB Output is correct
30 Correct 6 ms 1196 KB Output is correct
31 Correct 4 ms 1108 KB Output is correct
32 Correct 5 ms 1108 KB Output is correct
33 Correct 5 ms 1108 KB Output is correct
34 Correct 193 ms 37872 KB Output is correct
35 Correct 199 ms 46268 KB Output is correct
36 Correct 195 ms 46380 KB Output is correct
37 Correct 240 ms 45760 KB Output is correct
38 Correct 236 ms 41916 KB Output is correct
39 Correct 241 ms 44700 KB Output is correct
40 Correct 238 ms 43812 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 252 ms 33168 KB Output is correct
44 Correct 237 ms 33272 KB Output is correct
45 Correct 249 ms 33252 KB Output is correct
46 Correct 204 ms 43608 KB Output is correct
47 Correct 213 ms 43464 KB Output is correct
48 Correct 217 ms 43640 KB Output is correct
49 Correct 232 ms 43600 KB Output is correct
50 Correct 270 ms 37092 KB Output is correct
51 Correct 300 ms 37276 KB Output is correct
52 Correct 242 ms 37024 KB Output is correct
53 Correct 252 ms 37080 KB Output is correct