Submission #1095791

# Submission time Handle Problem Language Result Execution time Memory
1095791 2024-10-03T07:32:02 Z lampooppp Cat Exercise (JOI23_ho_t4) C++17
100 / 100
225 ms 66896 KB
#include <bits/stdc++.h>
#define int long long
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]);
}

void 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;
}

void 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];
}

int32_t 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 'long long int lca(long long int, long long int)':
Main.cpp:34:9: warning: statement has no effect [-Wunused-value]
   34 |     for(k;k>=0;--k)
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 3 ms 5280 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5208 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 3 ms 5280 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5208 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 5 ms 6488 KB Output is correct
19 Correct 4 ms 6492 KB Output is correct
20 Correct 4 ms 6420 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 4 ms 6492 KB Output is correct
23 Correct 4 ms 6488 KB Output is correct
24 Correct 4 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 3 ms 5280 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5208 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 5 ms 6488 KB Output is correct
19 Correct 4 ms 6492 KB Output is correct
20 Correct 4 ms 6420 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 4 ms 6492 KB Output is correct
23 Correct 4 ms 6488 KB Output is correct
24 Correct 4 ms 6492 KB Output is correct
25 Correct 2 ms 4956 KB Output is correct
26 Correct 5 ms 6336 KB Output is correct
27 Correct 4 ms 6492 KB Output is correct
28 Correct 5 ms 6492 KB Output is correct
29 Correct 4 ms 6492 KB Output is correct
30 Correct 4 ms 6236 KB Output is correct
31 Correct 4 ms 6236 KB Output is correct
32 Correct 5 ms 6344 KB Output is correct
33 Correct 5 ms 6236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 3 ms 5280 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5208 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 5 ms 6488 KB Output is correct
19 Correct 4 ms 6492 KB Output is correct
20 Correct 4 ms 6420 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 4 ms 6492 KB Output is correct
23 Correct 4 ms 6488 KB Output is correct
24 Correct 4 ms 6492 KB Output is correct
25 Correct 108 ms 56904 KB Output is correct
26 Correct 105 ms 66648 KB Output is correct
27 Correct 107 ms 66896 KB Output is correct
28 Correct 178 ms 66116 KB Output is correct
29 Correct 183 ms 60924 KB Output is correct
30 Correct 160 ms 64640 KB Output is correct
31 Correct 225 ms 63612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 158 ms 52048 KB Output is correct
4 Correct 157 ms 52124 KB Output is correct
5 Correct 196 ms 52272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 3 ms 5280 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5208 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 5 ms 6488 KB Output is correct
19 Correct 4 ms 6492 KB Output is correct
20 Correct 4 ms 6420 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 4 ms 6492 KB Output is correct
23 Correct 4 ms 6488 KB Output is correct
24 Correct 4 ms 6492 KB Output is correct
25 Correct 2 ms 4956 KB Output is correct
26 Correct 5 ms 6336 KB Output is correct
27 Correct 4 ms 6492 KB Output is correct
28 Correct 5 ms 6492 KB Output is correct
29 Correct 4 ms 6492 KB Output is correct
30 Correct 4 ms 6236 KB Output is correct
31 Correct 4 ms 6236 KB Output is correct
32 Correct 5 ms 6344 KB Output is correct
33 Correct 5 ms 6236 KB Output is correct
34 Correct 108 ms 56904 KB Output is correct
35 Correct 105 ms 66648 KB Output is correct
36 Correct 107 ms 66896 KB Output is correct
37 Correct 178 ms 66116 KB Output is correct
38 Correct 183 ms 60924 KB Output is correct
39 Correct 160 ms 64640 KB Output is correct
40 Correct 225 ms 63612 KB Output is correct
41 Correct 2 ms 4956 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 158 ms 52048 KB Output is correct
44 Correct 157 ms 52124 KB Output is correct
45 Correct 196 ms 52272 KB Output is correct
46 Correct 155 ms 63764 KB Output is correct
47 Correct 156 ms 63752 KB Output is correct
48 Correct 171 ms 63596 KB Output is correct
49 Correct 141 ms 63584 KB Output is correct
50 Correct 181 ms 55380 KB Output is correct
51 Correct 220 ms 55380 KB Output is correct
52 Correct 209 ms 55380 KB Output is correct
53 Correct 185 ms 55504 KB Output is correct