Submission #871054

# Submission time Handle Problem Language Result Execution time Memory
871054 2023-11-09T19:22:58 Z Maite_Morale Cat Exercise (JOI23_ho_t4) C++14
100 / 100
542 ms 157372 KB
#include<bits/stdc++.h>
#define F first
#define S second
#define MAX 500005
#define oo 1e18
#define mod 1000000007
#define fast_in ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);cout.setf(ios::fixed);cout.precision(0);
using namespace std;
typedef long long ll;
#define pll pair<ll , ll>
#define vll vector<ll>
#define vvll vector<vll>
#define vpll vector<pll>

struct ST{
    vll tree;ll sz;
    ST(ll _sz){
        sz=_sz*2;
        tree.assign(sz*2+100,0);
    }
    void update(ll a,ll b,ll x,ll y,ll n){
        if(x-y==0){tree[n]=b;return;}
        ll m=(x+y)/2;
        if(a<=m)update(a,b,x,m,n*2);
        else    update(a,b,m+1,y,n*2+1);
        tree[n]=max(tree[n*2],tree[n*2+1]);
    }
    ll query(ll a,ll b,ll x,ll y,ll n){
        if(y<a || x>b)return 0;
        if(a<=x && y<=b)return tree[n];
        ll m=(x+y)/2;
    return max(query(a,b,x,m,n*2),query(a,b,m+1,y,n*2+1));
    }
    void update(ll a,ll b){
        update(a,b,0,sz,1);
    }
    ll query(ll a,ll b){
        if(a>b)swap(a,b);
        return query(a,b,0,sz,1);
    }
} eu(MAX);
ll n,d[MAX],p[MAX],f[MAX],a[MAX],s[30][MAX],pass[MAX],c=0;
vll v[MAX];
void dfs(ll x,ll y){
    c++;eu.update(c,x);p[x]=c;
    d[x]=d[y]+1;s[0][x]=y;
    for(ll w : v[x]){
        if(w==y)continue;
        dfs(w,x);
    }
    f[x]=c;
}//4 3 1 2                     
ll lca(ll x,ll y){
   if(d[x]<d[y])swap(x,y);
   for(int i=20;i>=0;i--)if(d[x]-(1<<i)>=d[y])x=s[i][x];
   if(x==y)return y;
   for(int i=20;i>=0;i--)if(s[i][x]!=s[i][y]){x=s[i][x];y=s[i][y];}
return s[0][y];
}
ll dist(ll x,ll y){
    return d[x]+d[y]-2*d[lca(x,y)];
}
ll ans(ll x,ll y,ll z){//cout<<z<<" "<<x<<"-"<<y<<":";
    ll r=0;eu.update(p[z],-1);
    pass[z]=-1;
    for(int w: v[z]){
        if(pass[w]==-1 || s[0][z]==w)continue;
        ll mx=eu.query(p[w],f[w]);
       // cout<<mx<<" "<<p[w]<<" "<<f[w]<<"\n";
        if(mx<=0)continue;
        r=max(r,dist(z,mx)+ans(p[w],f[w],mx));
    }//cout<<"\n";
    ll mx=eu.query(x,y);//cout<<"mx:"<<mx<<"\n";
    if(mx<=0){/*cout<<r<<"\n";*/return r;}
    r=max(r,dist(z,mx)+ans(x,y,mx));
return r;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }ll a1,a2;
    for(int i=1;i<n;i++){
        cin>>a1>>a2;a1=a[a1];a2=a[a2];
        v[a1].push_back(a2);
        v[a2].push_back(a1);
    }dfs(n,n);
    for(int i=1;i<20;i++){
        for(int j=1;j<=n;j++){
            s[i][j]=s[i-1][s[i-1][j]];
        }
    }//cout<<-1<<"\n\n";
    cout<<ans(p[n],f[n],n);
return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 75608 KB Output is correct
2 Correct 11 ms 75612 KB Output is correct
3 Correct 12 ms 75612 KB Output is correct
4 Correct 10 ms 75612 KB Output is correct
5 Correct 10 ms 75724 KB Output is correct
6 Correct 10 ms 75612 KB Output is correct
7 Correct 10 ms 75612 KB Output is correct
8 Correct 10 ms 75612 KB Output is correct
9 Correct 11 ms 75608 KB Output is correct
10 Correct 10 ms 75612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 75608 KB Output is correct
2 Correct 11 ms 75612 KB Output is correct
3 Correct 12 ms 75612 KB Output is correct
4 Correct 10 ms 75612 KB Output is correct
5 Correct 10 ms 75724 KB Output is correct
6 Correct 10 ms 75612 KB Output is correct
7 Correct 10 ms 75612 KB Output is correct
8 Correct 10 ms 75612 KB Output is correct
9 Correct 11 ms 75608 KB Output is correct
10 Correct 10 ms 75612 KB Output is correct
11 Correct 11 ms 75664 KB Output is correct
12 Correct 11 ms 75608 KB Output is correct
13 Correct 11 ms 75612 KB Output is correct
14 Correct 11 ms 75612 KB Output is correct
15 Correct 10 ms 75612 KB Output is correct
16 Correct 11 ms 75612 KB Output is correct
17 Correct 11 ms 75612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 75608 KB Output is correct
2 Correct 11 ms 75612 KB Output is correct
3 Correct 12 ms 75612 KB Output is correct
4 Correct 10 ms 75612 KB Output is correct
5 Correct 10 ms 75724 KB Output is correct
6 Correct 10 ms 75612 KB Output is correct
7 Correct 10 ms 75612 KB Output is correct
8 Correct 10 ms 75612 KB Output is correct
9 Correct 11 ms 75608 KB Output is correct
10 Correct 10 ms 75612 KB Output is correct
11 Correct 11 ms 75664 KB Output is correct
12 Correct 11 ms 75608 KB Output is correct
13 Correct 11 ms 75612 KB Output is correct
14 Correct 11 ms 75612 KB Output is correct
15 Correct 10 ms 75612 KB Output is correct
16 Correct 11 ms 75612 KB Output is correct
17 Correct 11 ms 75612 KB Output is correct
18 Correct 17 ms 78684 KB Output is correct
19 Correct 17 ms 78756 KB Output is correct
20 Correct 16 ms 78684 KB Output is correct
21 Correct 17 ms 78108 KB Output is correct
22 Correct 16 ms 78168 KB Output is correct
23 Correct 16 ms 78172 KB Output is correct
24 Correct 18 ms 78424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 75608 KB Output is correct
2 Correct 11 ms 75612 KB Output is correct
3 Correct 12 ms 75612 KB Output is correct
4 Correct 10 ms 75612 KB Output is correct
5 Correct 10 ms 75724 KB Output is correct
6 Correct 10 ms 75612 KB Output is correct
7 Correct 10 ms 75612 KB Output is correct
8 Correct 10 ms 75612 KB Output is correct
9 Correct 11 ms 75608 KB Output is correct
10 Correct 10 ms 75612 KB Output is correct
11 Correct 11 ms 75664 KB Output is correct
12 Correct 11 ms 75608 KB Output is correct
13 Correct 11 ms 75612 KB Output is correct
14 Correct 11 ms 75612 KB Output is correct
15 Correct 10 ms 75612 KB Output is correct
16 Correct 11 ms 75612 KB Output is correct
17 Correct 11 ms 75612 KB Output is correct
18 Correct 17 ms 78684 KB Output is correct
19 Correct 17 ms 78756 KB Output is correct
20 Correct 16 ms 78684 KB Output is correct
21 Correct 17 ms 78108 KB Output is correct
22 Correct 16 ms 78168 KB Output is correct
23 Correct 16 ms 78172 KB Output is correct
24 Correct 18 ms 78424 KB Output is correct
25 Correct 11 ms 75612 KB Output is correct
26 Correct 17 ms 78428 KB Output is correct
27 Correct 17 ms 78488 KB Output is correct
28 Correct 16 ms 78424 KB Output is correct
29 Correct 17 ms 78428 KB Output is correct
30 Correct 19 ms 78016 KB Output is correct
31 Correct 17 ms 78112 KB Output is correct
32 Correct 17 ms 78168 KB Output is correct
33 Correct 17 ms 78364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 75608 KB Output is correct
2 Correct 11 ms 75612 KB Output is correct
3 Correct 12 ms 75612 KB Output is correct
4 Correct 10 ms 75612 KB Output is correct
5 Correct 10 ms 75724 KB Output is correct
6 Correct 10 ms 75612 KB Output is correct
7 Correct 10 ms 75612 KB Output is correct
8 Correct 10 ms 75612 KB Output is correct
9 Correct 11 ms 75608 KB Output is correct
10 Correct 10 ms 75612 KB Output is correct
11 Correct 11 ms 75664 KB Output is correct
12 Correct 11 ms 75608 KB Output is correct
13 Correct 11 ms 75612 KB Output is correct
14 Correct 11 ms 75612 KB Output is correct
15 Correct 10 ms 75612 KB Output is correct
16 Correct 11 ms 75612 KB Output is correct
17 Correct 11 ms 75612 KB Output is correct
18 Correct 17 ms 78684 KB Output is correct
19 Correct 17 ms 78756 KB Output is correct
20 Correct 16 ms 78684 KB Output is correct
21 Correct 17 ms 78108 KB Output is correct
22 Correct 16 ms 78168 KB Output is correct
23 Correct 16 ms 78172 KB Output is correct
24 Correct 18 ms 78424 KB Output is correct
25 Correct 292 ms 157372 KB Output is correct
26 Correct 292 ms 154896 KB Output is correct
27 Correct 273 ms 154816 KB Output is correct
28 Correct 337 ms 136276 KB Output is correct
29 Correct 318 ms 137556 KB Output is correct
30 Correct 309 ms 137300 KB Output is correct
31 Correct 320 ms 138064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 75608 KB Output is correct
2 Correct 14 ms 75696 KB Output is correct
3 Correct 524 ms 133204 KB Output is correct
4 Correct 451 ms 133268 KB Output is correct
5 Correct 508 ms 132948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 75608 KB Output is correct
2 Correct 11 ms 75612 KB Output is correct
3 Correct 12 ms 75612 KB Output is correct
4 Correct 10 ms 75612 KB Output is correct
5 Correct 10 ms 75724 KB Output is correct
6 Correct 10 ms 75612 KB Output is correct
7 Correct 10 ms 75612 KB Output is correct
8 Correct 10 ms 75612 KB Output is correct
9 Correct 11 ms 75608 KB Output is correct
10 Correct 10 ms 75612 KB Output is correct
11 Correct 11 ms 75664 KB Output is correct
12 Correct 11 ms 75608 KB Output is correct
13 Correct 11 ms 75612 KB Output is correct
14 Correct 11 ms 75612 KB Output is correct
15 Correct 10 ms 75612 KB Output is correct
16 Correct 11 ms 75612 KB Output is correct
17 Correct 11 ms 75612 KB Output is correct
18 Correct 17 ms 78684 KB Output is correct
19 Correct 17 ms 78756 KB Output is correct
20 Correct 16 ms 78684 KB Output is correct
21 Correct 17 ms 78108 KB Output is correct
22 Correct 16 ms 78168 KB Output is correct
23 Correct 16 ms 78172 KB Output is correct
24 Correct 18 ms 78424 KB Output is correct
25 Correct 11 ms 75612 KB Output is correct
26 Correct 17 ms 78428 KB Output is correct
27 Correct 17 ms 78488 KB Output is correct
28 Correct 16 ms 78424 KB Output is correct
29 Correct 17 ms 78428 KB Output is correct
30 Correct 19 ms 78016 KB Output is correct
31 Correct 17 ms 78112 KB Output is correct
32 Correct 17 ms 78168 KB Output is correct
33 Correct 17 ms 78364 KB Output is correct
34 Correct 292 ms 157372 KB Output is correct
35 Correct 292 ms 154896 KB Output is correct
36 Correct 273 ms 154816 KB Output is correct
37 Correct 337 ms 136276 KB Output is correct
38 Correct 318 ms 137556 KB Output is correct
39 Correct 309 ms 137300 KB Output is correct
40 Correct 320 ms 138064 KB Output is correct
41 Correct 10 ms 75608 KB Output is correct
42 Correct 14 ms 75696 KB Output is correct
43 Correct 524 ms 133204 KB Output is correct
44 Correct 451 ms 133268 KB Output is correct
45 Correct 508 ms 132948 KB Output is correct
46 Correct 297 ms 149396 KB Output is correct
47 Correct 302 ms 149640 KB Output is correct
48 Correct 299 ms 149492 KB Output is correct
49 Correct 304 ms 149608 KB Output is correct
50 Correct 468 ms 130844 KB Output is correct
51 Correct 542 ms 131100 KB Output is correct
52 Correct 512 ms 130928 KB Output is correct
53 Correct 476 ms 130912 KB Output is correct