Submission #834133

# Submission time Handle Problem Language Result Execution time Memory
834133 2023-08-22T11:04:17 Z fdnfksd Cat Exercise (JOI23_ho_t4) C++14
100 / 100
309 ms 62080 KB
#include<bits/stdc++.h>
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
ll lab[maxN],h[maxN],P[maxN][18];

ll findset(ll x)
{
    return lab[x]<0?x:lab[x]=findset(lab[x]);
}
void unite(ll u,ll v)
{
    u=findset(u);
    v=findset(v);
    if(lab[u]>lab[v]) swap(u,v);
    lab[u]+=lab[v];
    lab[v]=u;
}
vector<ll>g[maxN];
void dfs(ll u,ll p)
{
    P[u][0]=p;
    for(int i=1;i<=17;i++) P[u][i]=P[P[u][i-1]][i-1];
    for(int v:g[u])
    {
        if(v!=p)
        {
            h[v]=h[u]+1;
            dfs(v,u);
        }
    }
}
ll lca(ll u,ll v)
{
    if(h[u]<h[v]) swap(u,v);
    for(int i=17;i>=0;i--) if(h[u]-(1<<i)>=h[v]) u=P[u][i];
    if(u==v) return u;
    for(int i=17;i>=0;i--) if(P[u][i]!=P[v][i]) u=P[u][i],v=P[v][i];
    return P[u][0];
}
ll dis(ll u,ll v)
{
    return h[u]+h[v]-2*h[lca(u,v)];
}
ll n,p[maxN],id[maxN],dp[maxN],mx[maxN];
void solve()
{
    cin >> n ;
    for(int i=1;i<=n;i++)
    {
        cin >> p[i];
        id[p[i]]=i;lab[i]=-1;
    }
    for(int i=1;i<n;i++)
    {
        ll u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1,1);
    for(int i=1;i<=n;i++)
    {
        ll u=id[i];
        for(int v:g[u])
        {
            if(p[v]<p[u])
            {
                dp[u]=max(dp[u],dp[findset(v)]+dis(u,mx[findset(v)]));
                unite(u,v);
            }
        }
        dp[findset(u)]=dp[u];
        mx[findset(u)]=u;
    }
    cout << dp[id[n]];
}
int main()
{
    fastio
    //freopen(TASKNAME".INP","r",stdin);
    //freopen(TASKNAME".OUT","w",stdout);
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5040 KB Output is correct
8 Correct 3 ms 4996 KB Output is correct
9 Correct 2 ms 5044 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5040 KB Output is correct
8 Correct 3 ms 4996 KB Output is correct
9 Correct 2 ms 5044 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5036 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5040 KB Output is correct
8 Correct 3 ms 4996 KB Output is correct
9 Correct 2 ms 5044 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5036 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 6 ms 6484 KB Output is correct
19 Correct 5 ms 6356 KB Output is correct
20 Correct 5 ms 6464 KB Output is correct
21 Correct 4 ms 6464 KB Output is correct
22 Correct 4 ms 6356 KB Output is correct
23 Correct 5 ms 6460 KB Output is correct
24 Correct 5 ms 6464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5040 KB Output is correct
8 Correct 3 ms 4996 KB Output is correct
9 Correct 2 ms 5044 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5036 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 6 ms 6484 KB Output is correct
19 Correct 5 ms 6356 KB Output is correct
20 Correct 5 ms 6464 KB Output is correct
21 Correct 4 ms 6464 KB Output is correct
22 Correct 4 ms 6356 KB Output is correct
23 Correct 5 ms 6460 KB Output is correct
24 Correct 5 ms 6464 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 5 ms 6276 KB Output is correct
27 Correct 5 ms 6272 KB Output is correct
28 Correct 5 ms 6332 KB Output is correct
29 Correct 5 ms 6328 KB Output is correct
30 Correct 5 ms 6244 KB Output is correct
31 Correct 5 ms 6208 KB Output is correct
32 Correct 5 ms 6228 KB Output is correct
33 Correct 5 ms 6228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5040 KB Output is correct
8 Correct 3 ms 4996 KB Output is correct
9 Correct 2 ms 5044 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5036 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 6 ms 6484 KB Output is correct
19 Correct 5 ms 6356 KB Output is correct
20 Correct 5 ms 6464 KB Output is correct
21 Correct 4 ms 6464 KB Output is correct
22 Correct 4 ms 6356 KB Output is correct
23 Correct 5 ms 6460 KB Output is correct
24 Correct 5 ms 6464 KB Output is correct
25 Correct 114 ms 60388 KB Output is correct
26 Correct 109 ms 62012 KB Output is correct
27 Correct 115 ms 62080 KB Output is correct
28 Correct 149 ms 61888 KB Output is correct
29 Correct 154 ms 61904 KB Output is correct
30 Correct 153 ms 61912 KB Output is correct
31 Correct 153 ms 61884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5040 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 162 ms 54072 KB Output is correct
4 Correct 190 ms 54076 KB Output is correct
5 Correct 175 ms 53996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5040 KB Output is correct
8 Correct 3 ms 4996 KB Output is correct
9 Correct 2 ms 5044 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5036 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 6 ms 6484 KB Output is correct
19 Correct 5 ms 6356 KB Output is correct
20 Correct 5 ms 6464 KB Output is correct
21 Correct 4 ms 6464 KB Output is correct
22 Correct 4 ms 6356 KB Output is correct
23 Correct 5 ms 6460 KB Output is correct
24 Correct 5 ms 6464 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 5 ms 6276 KB Output is correct
27 Correct 5 ms 6272 KB Output is correct
28 Correct 5 ms 6332 KB Output is correct
29 Correct 5 ms 6328 KB Output is correct
30 Correct 5 ms 6244 KB Output is correct
31 Correct 5 ms 6208 KB Output is correct
32 Correct 5 ms 6228 KB Output is correct
33 Correct 5 ms 6228 KB Output is correct
34 Correct 114 ms 60388 KB Output is correct
35 Correct 109 ms 62012 KB Output is correct
36 Correct 115 ms 62080 KB Output is correct
37 Correct 149 ms 61888 KB Output is correct
38 Correct 154 ms 61904 KB Output is correct
39 Correct 153 ms 61912 KB Output is correct
40 Correct 153 ms 61884 KB Output is correct
41 Correct 2 ms 5040 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 162 ms 54072 KB Output is correct
44 Correct 190 ms 54076 KB Output is correct
45 Correct 175 ms 53996 KB Output is correct
46 Correct 259 ms 58260 KB Output is correct
47 Correct 279 ms 57416 KB Output is correct
48 Correct 309 ms 58772 KB Output is correct
49 Correct 307 ms 57472 KB Output is correct
50 Correct 268 ms 53560 KB Output is correct
51 Correct 284 ms 53660 KB Output is correct
52 Correct 270 ms 53636 KB Output is correct
53 Correct 247 ms 53560 KB Output is correct