Submission #1032329

# Submission time Handle Problem Language Result Execution time Memory
1032329 2024-07-23T15:49:35 Z vjudge1 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
178 ms 66644 KB

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=(j);i<=(k);i++)
#define for2(i,j,k) for(int i=(j);i>=(k);i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=2e5+69;
const ll offset=1e9;
const ll block_sz=317;
const ll inf=1e18;
const ll mod=123456789;
int n,p[maxn],par[maxn];
vector<int> adj[maxn];
int Find(int u)
{
    return par[u]<0?u:par[u]=Find(par[u]);
}
int dep[maxn],up[maxn][21],in[maxn],out[maxn],Time,dp[maxn];
void dfs(int u,int pre)
{
    in[u]=++Time;
    up[u][0]=pre;
    for1(i,1,20) up[u][i]=up[up[u][i-1]][i-1];
    for(int v:adj[u])
    {
        if (v==pre) continue;
        dep[v]=dep[u]+1;
        dfs(v,u);
    }
    out[u]=Time;
}
bool is_ancestor(int u,int v)
{
    return in[u]<=in[v] && out[v]<=out[u];
}
int lca(int u,int v)
{
    if (is_ancestor(u,v)) return u;
    if (is_ancestor(v,u)) return v;
    for2(i,20,0) if (!is_ancestor(up[u][i],v)) u=up[u][i];
    return up[u][0];
}
void sol()
{
    cin >> n;
    for1(i,1,n) cin >> p[i];
    for1(i,1,n-1)
    {
        int u,v; cin>> u>> v;
        u=p[u],v=p[v];
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1,1);
    for1(i,1,n) par[i]=-1;
    for1(u,1,n)
    {
        for(int v:adj[u])
        {
            v=Find(v);
            if (v<u)
            {
                par[v]=u;
                dp[u]=max(dp[u],dp[v]+dep[u]+dep[v]-2*dep[lca(u,v)]);
            }
        }
    }
    cout << dp[n];

}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("PAINT.inp","r",stdin);
//    freopen("PAINT.out","w",stdout);
    int t=1;//cin >> t;
    while (t--)
    {
        sol();
    }
}
/*
*/



# 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 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 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 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 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 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 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 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 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 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 6492 KB Output is correct
19 Correct 4 ms 6492 KB Output is correct
20 Correct 4 ms 6672 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 5 ms 6492 KB Output is correct
23 Correct 4 ms 6464 KB Output is correct
24 Correct 5 ms 6476 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 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 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 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 6492 KB Output is correct
19 Correct 4 ms 6492 KB Output is correct
20 Correct 4 ms 6672 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 5 ms 6492 KB Output is correct
23 Correct 4 ms 6464 KB Output is correct
24 Correct 5 ms 6476 KB Output is correct
25 Correct 2 ms 5172 KB Output is correct
26 Correct 5 ms 6496 KB Output is correct
27 Correct 4 ms 6492 KB Output is correct
28 Correct 4 ms 6488 KB Output is correct
29 Correct 4 ms 6652 KB Output is correct
30 Correct 4 ms 6492 KB Output is correct
31 Correct 5 ms 6488 KB Output is correct
32 Correct 5 ms 6492 KB Output is correct
33 Correct 6 ms 6488 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 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 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 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 6492 KB Output is correct
19 Correct 4 ms 6492 KB Output is correct
20 Correct 4 ms 6672 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 5 ms 6492 KB Output is correct
23 Correct 4 ms 6464 KB Output is correct
24 Correct 5 ms 6476 KB Output is correct
25 Correct 103 ms 62048 KB Output is correct
26 Correct 92 ms 66644 KB Output is correct
27 Correct 96 ms 66644 KB Output is correct
28 Correct 147 ms 66128 KB Output is correct
29 Correct 137 ms 62144 KB Output is correct
30 Correct 144 ms 65140 KB Output is correct
31 Correct 154 ms 64316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 5172 KB Output is correct
3 Correct 144 ms 58896 KB Output is correct
4 Correct 151 ms 58712 KB Output is correct
5 Correct 148 ms 58720 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 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 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 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 6492 KB Output is correct
19 Correct 4 ms 6492 KB Output is correct
20 Correct 4 ms 6672 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 5 ms 6492 KB Output is correct
23 Correct 4 ms 6464 KB Output is correct
24 Correct 5 ms 6476 KB Output is correct
25 Correct 2 ms 5172 KB Output is correct
26 Correct 5 ms 6496 KB Output is correct
27 Correct 4 ms 6492 KB Output is correct
28 Correct 4 ms 6488 KB Output is correct
29 Correct 4 ms 6652 KB Output is correct
30 Correct 4 ms 6492 KB Output is correct
31 Correct 5 ms 6488 KB Output is correct
32 Correct 5 ms 6492 KB Output is correct
33 Correct 6 ms 6488 KB Output is correct
34 Correct 103 ms 62048 KB Output is correct
35 Correct 92 ms 66644 KB Output is correct
36 Correct 96 ms 66644 KB Output is correct
37 Correct 147 ms 66128 KB Output is correct
38 Correct 137 ms 62144 KB Output is correct
39 Correct 144 ms 65140 KB Output is correct
40 Correct 154 ms 64316 KB Output is correct
41 Correct 2 ms 4952 KB Output is correct
42 Correct 2 ms 5172 KB Output is correct
43 Correct 144 ms 58896 KB Output is correct
44 Correct 151 ms 58712 KB Output is correct
45 Correct 148 ms 58720 KB Output is correct
46 Correct 120 ms 64336 KB Output is correct
47 Correct 119 ms 64516 KB Output is correct
48 Correct 117 ms 64340 KB Output is correct
49 Correct 117 ms 64304 KB Output is correct
50 Correct 166 ms 58264 KB Output is correct
51 Correct 168 ms 58452 KB Output is correct
52 Correct 178 ms 58536 KB Output is correct
53 Correct 162 ms 58448 KB Output is correct