Submission #1034847

# Submission time Handle Problem Language Result Execution time Memory
1034847 2024-07-25T19:58:25 Z alexdd Meetings 2 (JOI21_meetings2) C++17
100 / 100
714 ms 80156 KB
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e8;
int n;
int parent[200005],siz[200005],depth[200005];
vector<int> con[200005];
int anc[200005][18];
int cine[400005];
int tin[200005],tout[200005],timer;
vector<int> unde[200005];
void dfs(int nod)
{
    tin[nod]=++timer;
    cine[timer]=nod;
    unde[nod].push_back(timer);

    siz[nod]=1;
    for(auto adj:con[nod])
    {
        if(adj!=parent[nod])
        {
            parent[adj]=nod;
            anc[adj][0]=nod;
            depth[adj]=depth[nod]+1;
            dfs(adj);
            cine[++timer]=nod;
            unde[nod].push_back(timer);
            siz[nod]+=siz[adj];
        }
    }
    tout[nod]=timer;
}

struct node
{
    int a,b,ab,bc,abc;
};
node combine(node x, node y)
{
    node aux;
    aux.a = max(x.a, y.a);
    aux.b = max(x.b, y.b);
    aux.ab = max({x.ab, y.ab, x.a+y.b});
    aux.bc = max({x.bc, y.bc, x.b+y.a});
    aux.abc = max({x.abc, y.abc, x.a+y.bc, x.ab+y.a});
    return aux;
}
node aint[1100000];
void build(int nod, int st, int dr)
{
    if(st==dr)
    {
        aint[nod] = {-INF,-INF,-INF,-INF,-INF};
        return;
    }
    int mij=(st+dr)/2;
    build(nod*2,st,mij);
    build(nod*2+1,mij+1,dr);
    aint[nod] = combine(aint[nod*2], aint[nod*2+1]);
}
void upd(int nod, int st, int dr, int poz, int newv)
{
    if(st==dr)
    {
        aint[nod].a = newv;
        aint[nod].b = -2*newv;
        aint[nod].ab = aint[nod].bc = aint[nod].abc = -INF;
        return;
    }
    int mij=(st+dr)/2;
    if(poz<=mij) upd(nod*2,st,mij,poz,newv);
    else upd(nod*2+1,mij+1,dr,poz,newv);
    aint[nod] = combine(aint[nod*2], aint[nod*2+1]);
}
node qry(int nod, int st, int dr, int le, int ri)
{
    if(le>ri)
        return {-INF,-INF,-INF,-INF,-INF};
    if(le==st && dr==ri)
        return aint[nod];
    int mij=(st+dr)/2;
    return combine(qry(nod*2,st,mij,le,min(mij,ri)),
                   qry(nod*2+1,mij+1,dr,max(mij+1,le),ri));
}


int rez[200005];
signed main()
{
    cin>>n;
    int a,b;
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        con[a].push_back(b);
        con[b].push_back(a);
    }
    dfs(1);
    build(1,1,timer);
    anc[1][0]=1;
    for(int p=1;p<18;p++)
        for(int i=1;i<=n;i++)
            anc[i][p] = anc[anc[i][p-1]][p-1];
    vector<pair<pair<int,int>,int>> v;
    for(int i=1;i<=n;i++)
    {
        v.push_back({{-siz[i],0},i});
        v.push_back({{-(n-siz[i]),1},i});
    }
    sort(v.begin(),v.end());
    for(auto x:v)
    {
        int i=x.second;
        int tip=x.first.second;
        //cout<<i<<" "<<tip<<" zzz\n";
        if(tip==0)
        {
            for(auto x:unde[i])
                upd(1,1,timer,x,depth[i]);

            int cap=i;
            for(int p=17;p>=0;p--)
                if(n-siz[anc[cap][p]]>=siz[i])
                    cap = anc[cap][p];
            if(cap!=1 && n-siz[cap]>=siz[i])
                cap = parent[cap];
            rez[siz[i]] = max(rez[siz[i]], qry(1,1,timer,tin[cap],tout[cap]).abc+1);
        }
        else
        {
            rez[n-siz[i]] = max(rez[n-siz[i]], qry(1,1,timer,tin[i],tout[i]).a-depth[i]+2);
        }
    }
    for(int i=n-1;i>0;i--)
        rez[i] = max({1, rez[i], rez[i+1]});
    for(int i=1;i<=n;i++)
    {
        if(i%2==1)
        {
            cout<<1<<"\n";
        }
        else
        {
            cout<<rez[i/2]<<"\n";
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9816 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 4 ms 9816 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 4 ms 9816 KB Output is correct
7 Correct 4 ms 10072 KB Output is correct
8 Correct 4 ms 9816 KB Output is correct
9 Correct 4 ms 9816 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9816 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 4 ms 9720 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 5 ms 9820 KB Output is correct
17 Correct 6 ms 9820 KB Output is correct
18 Correct 4 ms 9820 KB Output is correct
19 Correct 4 ms 9820 KB Output is correct
20 Correct 5 ms 9820 KB Output is correct
21 Correct 5 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9816 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 4 ms 9816 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 4 ms 9816 KB Output is correct
7 Correct 4 ms 10072 KB Output is correct
8 Correct 4 ms 9816 KB Output is correct
9 Correct 4 ms 9816 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9816 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 4 ms 9720 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 5 ms 9820 KB Output is correct
17 Correct 6 ms 9820 KB Output is correct
18 Correct 4 ms 9820 KB Output is correct
19 Correct 4 ms 9820 KB Output is correct
20 Correct 5 ms 9820 KB Output is correct
21 Correct 5 ms 9820 KB Output is correct
22 Correct 12 ms 11100 KB Output is correct
23 Correct 13 ms 11088 KB Output is correct
24 Correct 11 ms 11096 KB Output is correct
25 Correct 12 ms 10924 KB Output is correct
26 Correct 12 ms 11036 KB Output is correct
27 Correct 11 ms 11100 KB Output is correct
28 Correct 11 ms 11096 KB Output is correct
29 Correct 10 ms 11100 KB Output is correct
30 Correct 12 ms 11100 KB Output is correct
31 Correct 12 ms 11100 KB Output is correct
32 Correct 13 ms 11196 KB Output is correct
33 Correct 12 ms 11100 KB Output is correct
34 Correct 11 ms 11100 KB Output is correct
35 Correct 10 ms 11100 KB Output is correct
36 Correct 12 ms 11128 KB Output is correct
37 Correct 10 ms 11100 KB Output is correct
38 Correct 11 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9816 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 4 ms 9816 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 4 ms 9816 KB Output is correct
7 Correct 4 ms 10072 KB Output is correct
8 Correct 4 ms 9816 KB Output is correct
9 Correct 4 ms 9816 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9816 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 4 ms 9720 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 5 ms 9820 KB Output is correct
17 Correct 6 ms 9820 KB Output is correct
18 Correct 4 ms 9820 KB Output is correct
19 Correct 4 ms 9820 KB Output is correct
20 Correct 5 ms 9820 KB Output is correct
21 Correct 5 ms 9820 KB Output is correct
22 Correct 12 ms 11100 KB Output is correct
23 Correct 13 ms 11088 KB Output is correct
24 Correct 11 ms 11096 KB Output is correct
25 Correct 12 ms 10924 KB Output is correct
26 Correct 12 ms 11036 KB Output is correct
27 Correct 11 ms 11100 KB Output is correct
28 Correct 11 ms 11096 KB Output is correct
29 Correct 10 ms 11100 KB Output is correct
30 Correct 12 ms 11100 KB Output is correct
31 Correct 12 ms 11100 KB Output is correct
32 Correct 13 ms 11196 KB Output is correct
33 Correct 12 ms 11100 KB Output is correct
34 Correct 11 ms 11100 KB Output is correct
35 Correct 10 ms 11100 KB Output is correct
36 Correct 12 ms 11128 KB Output is correct
37 Correct 10 ms 11100 KB Output is correct
38 Correct 11 ms 11100 KB Output is correct
39 Correct 550 ms 70800 KB Output is correct
40 Correct 551 ms 70084 KB Output is correct
41 Correct 601 ms 70904 KB Output is correct
42 Correct 590 ms 71620 KB Output is correct
43 Correct 642 ms 71624 KB Output is correct
44 Correct 557 ms 71616 KB Output is correct
45 Correct 714 ms 76028 KB Output is correct
46 Correct 679 ms 80156 KB Output is correct
47 Correct 522 ms 72508 KB Output is correct
48 Correct 500 ms 73184 KB Output is correct
49 Correct 614 ms 72564 KB Output is correct
50 Correct 501 ms 73016 KB Output is correct
51 Correct 599 ms 79648 KB Output is correct