Submission #1095403

# Submission time Handle Problem Language Result Execution time Memory
1095403 2024-10-02T03:53:53 Z sitablechair Cat Exercise (JOI23_ho_t4) C++17
100 / 100
164 ms 47312 KB
#include<bits/stdc++.h>
#define ll long long
#define sz(x) (signed)x.size()
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,l,r) for(int i=r;i>=l;i--)

using namespace std;

const int N=2e5+3;

int n,p[N],d[N],par[20][N];
int mxChild[N];
ll f[N],dp[N];
vector<int> g[N];
int find_set(int a) {
    return (f[a]<0?a:f[a]=find_set(f[a]));
}
void unite(int u,int v) {
    int a=find_set(u),b=find_set(v);
    if (a==b) return;
    if (f[a]>f[b]) swap(a,b);
    f[a]+=f[b];
    f[b]=a;
}
void predfs(int u,int p=0) {
    for(auto v: g[u])
        if (v!=p) {
            d[v]=d[u]+1;
            par[0][v]=u;
            For(i,1,18) par[i][v]=par[i-1][par[i-1][v]];
            predfs(v,u);
        }
}
int lca(int u,int v) {
    if (d[v]>d[u]) swap(u,v);
    int dd=d[u]-d[v];
    For(i,0,18)
        if (dd>>i&1) u=par[i][u];
    if (u==v) return u;
    ForD(i,0,18)
        if (par[i][u]!=par[i][v]) u=par[i][u],v=par[i][v];
    return par[0][u];
}
int dis(int u,int v) {
    return d[u]+d[v]-2*d[lca(u,v)];
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    For(i,1,n) cin >> p[i];
    For(i,1,n-1) {
        int u,v;
        cin >> u >> v;
        g[p[u]].pb(p[v]);
        g[p[v]].pb(p[u]);
    }
    f[1]=-1;
    dp[1]=0;
    mxChild[1]=1;
    predfs(1);
    For(i,2,n) {
        dp[i]=0;
        for(auto u: g[i]) {
            if (u>i) continue;
            dp[i]=max(dp[i],dp[mxChild[find_set(u)]]+dis(mxChild[find_set(u)],i));
        }
        f[i]=-1;
        for(auto u: g[i])
            if (u<=i) unite(i,u);
        mxChild[find_set(i)]=i;
    }
    cout << dp[n] << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5216 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5172 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5216 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5172 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5272 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5160 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5276 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 3 ms 5216 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5172 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5272 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5160 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5276 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 5980 KB Output is correct
19 Correct 4 ms 6236 KB Output is correct
20 Correct 4 ms 6236 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 4 ms 6236 KB Output is correct
23 Correct 4 ms 5980 KB Output is correct
24 Correct 4 ms 6308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5216 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5172 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5272 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5160 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5276 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 5980 KB Output is correct
19 Correct 4 ms 6236 KB Output is correct
20 Correct 4 ms 6236 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 4 ms 6236 KB Output is correct
23 Correct 4 ms 5980 KB Output is correct
24 Correct 4 ms 6308 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 4 ms 5980 KB Output is correct
27 Correct 4 ms 5976 KB Output is correct
28 Correct 4 ms 5980 KB Output is correct
29 Correct 4 ms 6172 KB Output is correct
30 Correct 4 ms 5976 KB Output is correct
31 Correct 4 ms 5980 KB Output is correct
32 Correct 4 ms 5936 KB Output is correct
33 Correct 4 ms 5976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5216 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5172 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5272 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5160 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5276 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 5980 KB Output is correct
19 Correct 4 ms 6236 KB Output is correct
20 Correct 4 ms 6236 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 4 ms 6236 KB Output is correct
23 Correct 4 ms 5980 KB Output is correct
24 Correct 4 ms 6308 KB Output is correct
25 Correct 76 ms 40824 KB Output is correct
26 Correct 83 ms 47180 KB Output is correct
27 Correct 80 ms 47132 KB Output is correct
28 Correct 132 ms 47312 KB Output is correct
29 Correct 137 ms 41808 KB Output is correct
30 Correct 164 ms 45652 KB Output is correct
31 Correct 138 ms 44628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5212 KB Output is correct
2 Correct 2 ms 5208 KB Output is correct
3 Correct 119 ms 35412 KB Output is correct
4 Correct 113 ms 35360 KB Output is correct
5 Correct 116 ms 35416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5216 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5172 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5272 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5160 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5276 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 5980 KB Output is correct
19 Correct 4 ms 6236 KB Output is correct
20 Correct 4 ms 6236 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 4 ms 6236 KB Output is correct
23 Correct 4 ms 5980 KB Output is correct
24 Correct 4 ms 6308 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 4 ms 5980 KB Output is correct
27 Correct 4 ms 5976 KB Output is correct
28 Correct 4 ms 5980 KB Output is correct
29 Correct 4 ms 6172 KB Output is correct
30 Correct 4 ms 5976 KB Output is correct
31 Correct 4 ms 5980 KB Output is correct
32 Correct 4 ms 5936 KB Output is correct
33 Correct 4 ms 5976 KB Output is correct
34 Correct 76 ms 40824 KB Output is correct
35 Correct 83 ms 47180 KB Output is correct
36 Correct 80 ms 47132 KB Output is correct
37 Correct 132 ms 47312 KB Output is correct
38 Correct 137 ms 41808 KB Output is correct
39 Correct 164 ms 45652 KB Output is correct
40 Correct 138 ms 44628 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 2 ms 5208 KB Output is correct
43 Correct 119 ms 35412 KB Output is correct
44 Correct 113 ms 35360 KB Output is correct
45 Correct 116 ms 35416 KB Output is correct
46 Correct 98 ms 43672 KB Output is correct
47 Correct 111 ms 43576 KB Output is correct
48 Correct 96 ms 43600 KB Output is correct
49 Correct 92 ms 43856 KB Output is correct
50 Correct 136 ms 35584 KB Output is correct
51 Correct 145 ms 35408 KB Output is correct
52 Correct 136 ms 35636 KB Output is correct
53 Correct 136 ms 35624 KB Output is correct