Submission #1111118

# Submission time Handle Problem Language Result Execution time Memory
1111118 2024-11-11T14:31:55 Z Luvidi Cat Exercise (JOI23_ho_t4) C++17
0 / 100
3 ms 5712 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int maxn=2e5;
vector<int> adj[maxn];
int sp[maxn][20],dt[maxn],par[maxn];

void dfs(int v,int p){
    for(int u:adj[v])if(u!=p){
        dt[u]=dt[v]+1;
        sp[u][0]=v;
        dfs(u,v);
    }
}

int lca(int v1,int v2){
    for(int i=19;i>-1;i--){
        if(dt[v1]-(1<<i)>=dt[v2])v1=sp[v1][i];
        if(dt[v2]-(1<<i)>=dt[v1])v2=sp[v2][i];
    }
    for(int i=19;i>-1;i--){
        if(sp[v1][i]!=sp[v2][i]){
            v1=sp[v1][i];
            v2=sp[v2][i];
        }
    }
    if(v1!=v2)v1=sp[v1][0];
    return v1;
}

ll dist(int v1,int v2){
    int x=lca(v1,v2);
    return dt[v1]+dt[v2]-2*dt[x];
}

int rep(int v){
    if(v==par[v])return v;
    return par[v]=rep(par[v]);
}

void join(int x,int y){
    x=rep(x);y=rep(y);
    par[x]=y;
}

int main(){
    freopen("in","r",stdin);
    freopen("out","w",stdout);
    int n;
    cin>>n;
    int p[n];
    for(int i=0;i<n;i++)cin>>p[i];
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        adj[--u].push_back(--v);
        adj[v].push_back(u);
    }
    int idx[n+1];
    for(int i=0;i<n;i++)idx[p[i]]=i;
    dfs(0,0);
    for(int i=1;i<20;i++){
        for(int j=0;j<n;j++){
            sp[j][i]=sp[sp[j][i-1]][i-1];
        }
    }
    iota(par,par+n,0);
    ll dp[n+1];
    for(int i=1;i<=n;i++){
        dp[i]=0;
        int v=idx[i];
        for(int u:adj[v])if(p[u]<p[v])join(u,v);
        for(int u=0;u<n;u++){
            if(u!=v&&rep(u)==rep(v)){
                // cout<<u<<' '<<v<<'\n';
                dp[i]=max(dp[i],dp[p[u]]+dist(u,v));
            }
        }
        // cout<<i<<' '<<dp[i]<<'\n';
    }
    cout<<dp[n]<<'\n';
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:49:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |     freopen("in","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:50:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |     freopen("out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5712 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5712 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5712 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5712 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5712 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5712 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5712 KB Output isn't correct
2 Halted 0 ms 0 KB -