답안 #727424

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
727424 2023-04-20T15:49:31 Z model_code Cat Exercise (JOI23_ho_t4) C++17
100 / 100
404 ms 59888 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)

#define MAX_N 200010

ll n,p[MAX_N],a[MAX_N],b[MAX_N];
vll g[MAX_N];

struct LCA{
    #define D 20
    ll depth[MAX_N],par[MAX_N][D];
    void dfs(ll x,ll from,ll d){
        depth[x]=d;
        par[x][0]=from;
        for(auto y:g[x])if(y!=from){
            dfs(y,x,d+1);
        }
    }
    void init(){
        dfs(0,0,0);
        rep(d,D-1){
            rep(i,n){
                par[i][d+1]=par[par[i][d]][d];
            }
        }
    }
    ll lca(ll a,ll b){
        if(depth[a]>depth[b])swap(a,b);
        assert(depth[a]<=depth[b]);
        for(ll d=D-1;d>=0;d--){
            if(depth[a]<=depth[b]-(1<<d)){
                b=par[b][d];
            }
        }
        assert(depth[a]==depth[b]);
        if(a==b)return a;
        for(ll d=D-1;d>=0;d--){
            if(par[a][d]!=par[b][d]){
                a=par[a][d];
                b=par[b][d];
            }
        }
        assert(a!=b);
        return par[a][0];
    }
    ll dist(ll a,ll b){
        ll c=lca(a,b);
        return depth[a]+depth[b]-2*depth[c];
    }
};
LCA lca;

struct UnionFind{
    ll par[MAX_N];
    void init(){
        rep(i,n){
            par[i]=i;
        }
    }
    ll root(ll x){
        return par[x]=(x==par[x]?x:root(par[x]));
    }
    void unite(ll a,ll b){
        a=root(a);
        b=root(b);
        if(a!=b){
            if(p[a]>p[b])swap(a,b);
            assert(p[a]<p[b]);
            par[a]=b;
        }
    }
};
UnionFind uf;

ll rp[MAX_N],f[MAX_N];
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cin>>n;
    rep(i,n){
        cin>>p[i];
        rp[p[i]]=i;
    }
    rep(i,n-1){
        cin>>a[i]>>b[i];
        a[i]--,b[i]--;
        g[a[i]].pb(b[i]);
        g[b[i]].pb(a[i]);
    }

    lca.init();
    uf.init();

    for(int i=1;i<=n;i++){
        ll x=rp[i];
        ll ans=0;
        for(auto y:g[x])if(p[y]<p[x]){
            ll v=uf.root(y);
            chmax(ans,f[v]+lca.dist(v,x));
            uf.unite(x,y);
        }
        f[x]=ans;
    }

    ll root=rp[n];
    ll ans=f[root];
    cout<<ans<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 5 ms 5076 KB Output is correct
7 Correct 5 ms 5052 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 5 ms 5076 KB Output is correct
7 Correct 5 ms 5052 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5036 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 5 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 5 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 5 ms 5076 KB Output is correct
7 Correct 5 ms 5052 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5036 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 5 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 5 ms 5076 KB Output is correct
18 Correct 6 ms 6448 KB Output is correct
19 Correct 8 ms 6428 KB Output is correct
20 Correct 7 ms 6356 KB Output is correct
21 Correct 8 ms 6340 KB Output is correct
22 Correct 8 ms 6356 KB Output is correct
23 Correct 7 ms 6356 KB Output is correct
24 Correct 8 ms 6408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 5 ms 5076 KB Output is correct
7 Correct 5 ms 5052 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5036 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 5 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 5 ms 5076 KB Output is correct
18 Correct 6 ms 6448 KB Output is correct
19 Correct 8 ms 6428 KB Output is correct
20 Correct 7 ms 6356 KB Output is correct
21 Correct 8 ms 6340 KB Output is correct
22 Correct 8 ms 6356 KB Output is correct
23 Correct 7 ms 6356 KB Output is correct
24 Correct 8 ms 6408 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 6 ms 6344 KB Output is correct
27 Correct 6 ms 6356 KB Output is correct
28 Correct 9 ms 6252 KB Output is correct
29 Correct 6 ms 6420 KB Output is correct
30 Correct 7 ms 6228 KB Output is correct
31 Correct 7 ms 6228 KB Output is correct
32 Correct 10 ms 6236 KB Output is correct
33 Correct 7 ms 6228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 5 ms 5076 KB Output is correct
7 Correct 5 ms 5052 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5036 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 5 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 5 ms 5076 KB Output is correct
18 Correct 6 ms 6448 KB Output is correct
19 Correct 8 ms 6428 KB Output is correct
20 Correct 7 ms 6356 KB Output is correct
21 Correct 8 ms 6340 KB Output is correct
22 Correct 8 ms 6356 KB Output is correct
23 Correct 7 ms 6356 KB Output is correct
24 Correct 8 ms 6408 KB Output is correct
25 Correct 179 ms 59752 KB Output is correct
26 Correct 178 ms 59796 KB Output is correct
27 Correct 184 ms 59888 KB Output is correct
28 Correct 261 ms 59748 KB Output is correct
29 Correct 243 ms 59760 KB Output is correct
30 Correct 251 ms 59800 KB Output is correct
31 Correct 262 ms 59740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 5 ms 5076 KB Output is correct
3 Correct 291 ms 55100 KB Output is correct
4 Correct 288 ms 55096 KB Output is correct
5 Correct 292 ms 55112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 5 ms 5076 KB Output is correct
7 Correct 5 ms 5052 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5036 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 5 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 5 ms 5076 KB Output is correct
18 Correct 6 ms 6448 KB Output is correct
19 Correct 8 ms 6428 KB Output is correct
20 Correct 7 ms 6356 KB Output is correct
21 Correct 8 ms 6340 KB Output is correct
22 Correct 8 ms 6356 KB Output is correct
23 Correct 7 ms 6356 KB Output is correct
24 Correct 8 ms 6408 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 6 ms 6344 KB Output is correct
27 Correct 6 ms 6356 KB Output is correct
28 Correct 9 ms 6252 KB Output is correct
29 Correct 6 ms 6420 KB Output is correct
30 Correct 7 ms 6228 KB Output is correct
31 Correct 7 ms 6228 KB Output is correct
32 Correct 10 ms 6236 KB Output is correct
33 Correct 7 ms 6228 KB Output is correct
34 Correct 179 ms 59752 KB Output is correct
35 Correct 178 ms 59796 KB Output is correct
36 Correct 184 ms 59888 KB Output is correct
37 Correct 261 ms 59748 KB Output is correct
38 Correct 243 ms 59760 KB Output is correct
39 Correct 251 ms 59800 KB Output is correct
40 Correct 262 ms 59740 KB Output is correct
41 Correct 4 ms 5076 KB Output is correct
42 Correct 5 ms 5076 KB Output is correct
43 Correct 291 ms 55100 KB Output is correct
44 Correct 288 ms 55096 KB Output is correct
45 Correct 292 ms 55112 KB Output is correct
46 Correct 385 ms 57484 KB Output is correct
47 Correct 358 ms 56920 KB Output is correct
48 Correct 350 ms 57760 KB Output is correct
49 Correct 370 ms 56932 KB Output is correct
50 Correct 351 ms 54540 KB Output is correct
51 Correct 404 ms 54572 KB Output is correct
52 Correct 392 ms 54660 KB Output is correct
53 Correct 386 ms 54568 KB Output is correct