Submission #828064

# Submission time Handle Problem Language Result Execution time Memory
828064 2023-08-17T03:28:30 Z LeVanThuc Cat Exercise (JOI23_ho_t4) C++17
100 / 100
340 ms 81824 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
    if(x<y)
    {
        x=y;
        return 1;
    }
    return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
    if(x>y)
    {
        x=y;
        return 1;
    }
    return 0;
}
void online()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
}
const ll N=2e5+10;
ll par[N][30],lv[N],dis[N],a[N],r[N],n;
vector<ll> gr[N];
void dfs(ll u)
{
    for(auto v:gr[u])
    {
        if(v==par[u][0]) continue;
        par[v][0]=u;
        lv[v]=lv[u]+1;
        dis[v]=dis[u]+1;
        dfs(v);
    }
}
ll dist(ll u,ll v)
{
    ll res=dis[u]+dis[v];
    if(lv[u]<lv[v]) swap(u,v);
    for(int i=20;i>=0;i--) if(lv[par[u][i]]>=lv[v]) u=par[u][i];
    if(u==v) return res-2*dis[u];
    for(int i=20;i>=0;i--)
    {
        if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i];
    }
    u=par[u][0];
    return res-2*dis[u];
}
struct DSU
{
    ll n;
    vector<ll> best,f,lab;
    DSU(ll _n=0)
    {
        n=_n;
        f.resize(n+2);
        lab.resize(n+2,-1);
        best.resize(n+2);
    }
    ll root(ll x)
    {
        return lab[x]<0?x:lab[x]=root(lab[x]);
    }
    ll join(ll x,ll y)
    {
        if(best[x]==0||best[y]==0) return 0;
        if((x=root(x))==(y=root(y))) return 0;
        if(lab[x]>lab[y]) swap(x,y);
        lab[x]+=lab[y];
        lab[y]=x;
        return 1;
    }
};
int main()
{
    online();
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        r[a[i]]=i;
    }
    for(int i=1;i<n;i++)
    {
        ll u,v;
        cin>>u>>v;
        gr[u].push_back(v);
        gr[v].push_back(u);
    }
    dfs(1);
    for(int i=1;i<=20;i++)
    {
        for(int j=1;j<=n;j++) par[j][i]=par[par[j][i-1]][i-1];
    }
    DSU S(n);
    for(int i=1;i<=n;i++)
    {
        ll u=r[i];
        S.best[u]=i;
        ll c=0;
        for(auto V:gr[u]) 
        {
            ll v=S.root(V);
            if(S.best[v]==0) continue;
            maximize(c,S.f[v]+dist(u,r[S.best[v]]));
        }
        for(auto v:gr[u]) S.join(u,v);
        S.best[S.root(u)]=i;
        S.f[S.root(u)]=c;
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        maximize(ans,S.f[i]);
    }
    cout<<ans;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 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 3 ms 5076 KB Output is correct
16 Correct 3 ms 5096 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 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 3 ms 5076 KB Output is correct
16 Correct 3 ms 5096 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 7 ms 6868 KB Output is correct
19 Correct 5 ms 6972 KB Output is correct
20 Correct 5 ms 6888 KB Output is correct
21 Correct 5 ms 6912 KB Output is correct
22 Correct 5 ms 6868 KB Output is correct
23 Correct 5 ms 6868 KB Output is correct
24 Correct 5 ms 6868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 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 3 ms 5076 KB Output is correct
16 Correct 3 ms 5096 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 7 ms 6868 KB Output is correct
19 Correct 5 ms 6972 KB Output is correct
20 Correct 5 ms 6888 KB Output is correct
21 Correct 5 ms 6912 KB Output is correct
22 Correct 5 ms 6868 KB Output is correct
23 Correct 5 ms 6868 KB Output is correct
24 Correct 5 ms 6868 KB Output is correct
25 Correct 2 ms 4948 KB Output is correct
26 Correct 6 ms 6740 KB Output is correct
27 Correct 5 ms 6868 KB Output is correct
28 Correct 5 ms 6740 KB Output is correct
29 Correct 5 ms 6868 KB Output is correct
30 Correct 5 ms 6668 KB Output is correct
31 Correct 5 ms 6612 KB Output is correct
32 Correct 8 ms 6612 KB Output is correct
33 Correct 5 ms 6612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 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 3 ms 5076 KB Output is correct
16 Correct 3 ms 5096 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 7 ms 6868 KB Output is correct
19 Correct 5 ms 6972 KB Output is correct
20 Correct 5 ms 6888 KB Output is correct
21 Correct 5 ms 6912 KB Output is correct
22 Correct 5 ms 6868 KB Output is correct
23 Correct 5 ms 6868 KB Output is correct
24 Correct 5 ms 6868 KB Output is correct
25 Correct 152 ms 81644 KB Output is correct
26 Correct 155 ms 81724 KB Output is correct
27 Correct 163 ms 81768 KB Output is correct
28 Correct 219 ms 81624 KB Output is correct
29 Correct 225 ms 81672 KB Output is correct
30 Correct 218 ms 81824 KB Output is correct
31 Correct 216 ms 81700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 219 ms 70684 KB Output is correct
4 Correct 231 ms 70668 KB Output is correct
5 Correct 218 ms 70776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 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 3 ms 5076 KB Output is correct
16 Correct 3 ms 5096 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 7 ms 6868 KB Output is correct
19 Correct 5 ms 6972 KB Output is correct
20 Correct 5 ms 6888 KB Output is correct
21 Correct 5 ms 6912 KB Output is correct
22 Correct 5 ms 6868 KB Output is correct
23 Correct 5 ms 6868 KB Output is correct
24 Correct 5 ms 6868 KB Output is correct
25 Correct 2 ms 4948 KB Output is correct
26 Correct 6 ms 6740 KB Output is correct
27 Correct 5 ms 6868 KB Output is correct
28 Correct 5 ms 6740 KB Output is correct
29 Correct 5 ms 6868 KB Output is correct
30 Correct 5 ms 6668 KB Output is correct
31 Correct 5 ms 6612 KB Output is correct
32 Correct 8 ms 6612 KB Output is correct
33 Correct 5 ms 6612 KB Output is correct
34 Correct 152 ms 81644 KB Output is correct
35 Correct 155 ms 81724 KB Output is correct
36 Correct 163 ms 81768 KB Output is correct
37 Correct 219 ms 81624 KB Output is correct
38 Correct 225 ms 81672 KB Output is correct
39 Correct 218 ms 81824 KB Output is correct
40 Correct 216 ms 81700 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 219 ms 70684 KB Output is correct
44 Correct 231 ms 70668 KB Output is correct
45 Correct 218 ms 70776 KB Output is correct
46 Correct 340 ms 76472 KB Output is correct
47 Correct 294 ms 75468 KB Output is correct
48 Correct 302 ms 77100 KB Output is correct
49 Correct 320 ms 75624 KB Output is correct
50 Correct 292 ms 70132 KB Output is correct
51 Correct 284 ms 70256 KB Output is correct
52 Correct 279 ms 70252 KB Output is correct
53 Correct 317 ms 70272 KB Output is correct