Submission #1095784

# Submission time Handle Problem Language Result Execution time Memory
1095784 2024-10-03T07:24:52 Z lampooppp Cat Exercise (JOI23_ho_t4) C++17
0 / 100
8 ms 10328 KB
#include <bits/stdc++.h>
using namespace std;

const int N=2e5+10;

int up[N+1][20], h[N+1];
vector<int> adj[N+1];
bool vis[N+1];

void dfs(int u)
{
    vis[u]=1;
    for(int v : adj[u])
    {
        if(vis[v]) continue;
        h[v]=h[u]+1;
        up[v][0]=u;
        for(int i=1;i<20;++i) up[v][i] = up[up[v][i-1]][i-1];
        dfs(v);
    }
}

int lca(int u,int v)
{
    if(h[u]<h[v]) swap(u,v);
    int k = h[u]-h[v];
    for(int i=0;i<20;++i)
    {
        if(k>>i&1) u=up[u][i];
    }
    if(u==v) return u;
    k=__lg(h[u]);
    for(k;k>=0;--k)
    {
        if(up[u][k]!=up[v][k])
        {
            u=up[u][k];
            v=up[v][k];
        }
    }
    return up[u][0];
}

int a[N+1];
int dp[N+1];

int lab[N+1];
int mx[N+1];

int findset(int u)
{
    if(lab[u]<0) return u;
    else return lab[u]=findset(lab[u]);
}

int Merge(int s,int t)
{
    if(lab[s]>lab[t]) swap(s,t);
    lab[s]+=lab[t];
    mx[s]=max(mx[s],mx[t]);
    lab[t]=s;
}

int unite(int a,int b)
{
    int s=findset(a);
    int t=findset(b);
    if(s!=t) Merge(s,t);
}

int dis(int u,int v)
{
    int c = lca(u,v);
    return h[u]+h[v]-2*h[c];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i=1;i<=n;++i)
    {
        cin >> a[i];
        lab[i]=-1;
        mx[i]=i;
    }
    for(int i=1;i<n;++i)
    {
        int u,v;
        cin >> u >> v;
        adj[a[u]].push_back(a[v]);
        adj[a[v]].push_back(a[u]);
    }
    h[1]=1;
    dfs(1);
    dp[1]=0;
    for(int i=2;i<=n;++i)
    {
        for(int v : adj[i])
        {
            if(v<i)
            {
                int k = mx[findset(v)];
                dp[i]=max(dp[i],dp[k]+dis(k,i));
                unite(v,i);
            }
        }
    }
    cout << dp[n];
}

Compilation message

Main.cpp: In function 'int lca(int, int)':
Main.cpp:33:9: warning: statement has no effect [-Wunused-value]
   33 |     for(k;k>=0;--k)
      |         ^
Main.cpp: In function 'int Merge(int, int)':
Main.cpp:62:1: warning: no return statement in function returning non-void [-Wreturn-type]
   62 | }
      | ^
Main.cpp: In function 'int unite(int, int)':
Main.cpp:69:1: warning: no return statement in function returning non-void [-Wreturn-type]
   69 | }
      | ^
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10328 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10328 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10328 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10328 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10328 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10328 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10328 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -