답안 #954616

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
954616 2024-03-28T08:27:43 Z sleepntsheep Cat Exercise (JOI23_ho_t4) C
100 / 100
326 ms 40632 KB
#include<stdio.h>

#define N 200001

int n,p[N],tin[N],tout[N],aux[N],paux[N],dd[N],pp[N],jj[N];
struct { int to,prev; } e[N<<1]; int ee,h[N];

int timer=1;
void dfs(int u,int p)
{
    aux[tin[u]=timer++]=u;
    for(int j=h[u];j;j=e[j].prev)if(e[j].to-p)
    {
        int v=e[j].to;
        pp[v]=u;
        jj[v]=(dd[u]-dd[jj[u]]==dd[jj[u]]-dd[jj[jj[u]]])?jj[jj[u]]:u;
        dd[v]=dd[u]+1;
        dfs(e[j].to,u);
    }
    tout[u]=timer-1;
}

long long min_(long long a,long long b){return a<b?a:b;}
long long max_(long long a,long long b){return a>b?a:b;}

long long t[N<<2],lz[N<<2];

void build(int v,int l,int r)
{
    if(l==r){t[v]=p[aux[l]];return;}
    build(v<<1,l,(l+r)/2);
    build(v<<1|1,(l+r)/2+1,r);
    t[v]=max_(t[v<<1],t[v<<1|1]);
}
void upd(int v,int l,int r,int x,int y,int k)
{
    if(r<x||y<l)return;
    if(x<=l&&r<=y) { lz[v]+=k; return; }
    upd(v<<1,l,(l+r)/2,x,y,k);
    upd(v<<1|1,(l+r)/2+1,r,x,y,k);
    t[v]=max_(t[v<<1]+lz[v<<1],t[v<<1|1]+lz[v<<1|1]);
}
long long get(){return t[1]+lz[1];}

int lca(int u,int v)
{
    if(dd[u]>dd[v])return lca(v,u);
    while(dd[v]>dd[u])
        v=dd[jj[v]]>=dd[u]?jj[v]:pp[v];
    while(u-v)
        if(jj[v]!=jj[u])u=jj[u],v=jj[v];
        else u=pp[u],v=pp[v];
    return u;
}

int dist(int u,int v){return dd[u]+dd[v]-2*dd[lca(u,v)];}

long long dfs1(int u)
{
    long long ans=0,next;

    upd(1,1,n,tin[u],tout[u],-1e9);
    next=get();
    if(next>=1)ans=max_(ans,dfs1(paux[next])+dist(paux[next],u));
    upd(1,1,n,1,tin[u]-1,-1e9);
    upd(1,1,n,tout[u]+1,n,-1e9);

    for(int j=h[u];j;j=e[j].prev)
    {
        int v=e[j].to;
        if(v!=pp[u])
        {
            upd(1,1,n,tin[v],tout[v],1e9);
            next=get();
            if(next>=1)ans=max_(ans,dfs1(paux[next])+dist(paux[next],u));
            upd(1,1,n,tin[v],tout[v],-1e9);
        }
    }

    lz[1]+=1e9;
    return ans;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%d",p+i),paux[p[i]]=i;
    for(int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),e[++ee].to=v,e[ee].prev=h[u],h[u]=ee,e[++ee].to=u,e[ee].prev=h[v],h[v]=ee;

    pp[1]=jj[1]=1;
    dfs(1,1);
    build(1,1,n);

    printf("%lld\n",dfs1(paux[n]));
    return 0;
}

Compilation message

Main.c: In function 'main':
Main.c:86:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |     scanf("%d",&n);
      |     ^~~~~~~~~~~~~~
Main.c:87:26: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     for(int i=1;i<=n;++i)scanf("%d",p+i),paux[p[i]]=i;
      |                          ^~~~~~~~~~~~~~~
Main.c:88:29: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     for(int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),e[++ee].to=v,e[ee].prev=h[u],h[u]=ee,e[++ee].to=u,e[ee].prev=h[v],h[v]=ee;
      |                             ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 5 ms 9304 KB Output is correct
19 Correct 5 ms 9308 KB Output is correct
20 Correct 6 ms 9308 KB Output is correct
21 Correct 5 ms 9052 KB Output is correct
22 Correct 4 ms 9048 KB Output is correct
23 Correct 5 ms 9052 KB Output is correct
24 Correct 5 ms 9052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 5 ms 9304 KB Output is correct
19 Correct 5 ms 9308 KB Output is correct
20 Correct 6 ms 9308 KB Output is correct
21 Correct 5 ms 9052 KB Output is correct
22 Correct 4 ms 9048 KB Output is correct
23 Correct 5 ms 9052 KB Output is correct
24 Correct 5 ms 9052 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 6 ms 9052 KB Output is correct
27 Correct 5 ms 9052 KB Output is correct
28 Correct 6 ms 9052 KB Output is correct
29 Correct 5 ms 9052 KB Output is correct
30 Correct 6 ms 8796 KB Output is correct
31 Correct 6 ms 8796 KB Output is correct
32 Correct 5 ms 8796 KB Output is correct
33 Correct 5 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 5 ms 9304 KB Output is correct
19 Correct 5 ms 9308 KB Output is correct
20 Correct 6 ms 9308 KB Output is correct
21 Correct 5 ms 9052 KB Output is correct
22 Correct 4 ms 9048 KB Output is correct
23 Correct 5 ms 9052 KB Output is correct
24 Correct 5 ms 9052 KB Output is correct
25 Correct 206 ms 40632 KB Output is correct
26 Correct 209 ms 39044 KB Output is correct
27 Correct 199 ms 39060 KB Output is correct
28 Correct 210 ms 37388 KB Output is correct
29 Correct 213 ms 37556 KB Output is correct
30 Correct 177 ms 37584 KB Output is correct
31 Correct 179 ms 37460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 220 ms 23376 KB Output is correct
4 Correct 260 ms 23564 KB Output is correct
5 Correct 235 ms 23320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 5 ms 9304 KB Output is correct
19 Correct 5 ms 9308 KB Output is correct
20 Correct 6 ms 9308 KB Output is correct
21 Correct 5 ms 9052 KB Output is correct
22 Correct 4 ms 9048 KB Output is correct
23 Correct 5 ms 9052 KB Output is correct
24 Correct 5 ms 9052 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 6 ms 9052 KB Output is correct
27 Correct 5 ms 9052 KB Output is correct
28 Correct 6 ms 9052 KB Output is correct
29 Correct 5 ms 9052 KB Output is correct
30 Correct 6 ms 8796 KB Output is correct
31 Correct 6 ms 8796 KB Output is correct
32 Correct 5 ms 8796 KB Output is correct
33 Correct 5 ms 8796 KB Output is correct
34 Correct 206 ms 40632 KB Output is correct
35 Correct 209 ms 39044 KB Output is correct
36 Correct 199 ms 39060 KB Output is correct
37 Correct 210 ms 37388 KB Output is correct
38 Correct 213 ms 37556 KB Output is correct
39 Correct 177 ms 37584 KB Output is correct
40 Correct 179 ms 37460 KB Output is correct
41 Correct 1 ms 8536 KB Output is correct
42 Correct 1 ms 8540 KB Output is correct
43 Correct 220 ms 23376 KB Output is correct
44 Correct 260 ms 23564 KB Output is correct
45 Correct 235 ms 23320 KB Output is correct
46 Correct 298 ms 34892 KB Output is correct
47 Correct 272 ms 35032 KB Output is correct
48 Correct 326 ms 35120 KB Output is correct
49 Correct 287 ms 34972 KB Output is correct
50 Correct 270 ms 22464 KB Output is correct
51 Correct 279 ms 22324 KB Output is correct
52 Correct 269 ms 22208 KB Output is correct
53 Correct 283 ms 22148 KB Output is correct