Submission #973392

# Submission time Handle Problem Language Result Execution time Memory
973392 2024-05-01T22:03:24 Z MarwenElarbi Meetings 2 (JOI21_meetings2) C++17
100 / 100
568 ms 44180 KB
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
const int nax=2e5+5;
int bj[nax][20];
int dep[nax];
int sz[nax];
vector<int> adj[nax];
int n;
void get_size(int x,int p){
    sz[x]=1;
    for(auto u:adj[x]){
        if(u==p) continue;
        bj[u][0]=x;
        dep[u]=dep[x]+1;
        get_size(u,x);
        sz[x]+=sz[u];
    }
    return;
}
int centroid(int x,int p){
    for(auto u:adj[x]){
        if(u==p) continue;
        if(sz[u]*2>n) return centroid(u,x);
    }
    return x;
}
void build(int x,int p){
    for (int i = 1; i < 20; ++i)
    {
        bj[x][i]=bj[bj[x][i-1]][i-1];
    }
    for(auto u:adj[x]){
        if(u==p) continue;
        build(u,x);
    }
}
int jump(int x,int d){
    for (int i = 0; i < 20; ++i)
    {
        if(d&(1<<i)) x=bj[x][i];
    }
    return x;
}
int lca(int x,int y){
    if(dep[x]<dep[y]) swap(x,y);
    x=jump(x,dep[x]-dep[y]);
    if(x==y) return x;
    for (int i = 19; i >= 0; --i)
    {
        int curx=bj[x][i];
        int cury=bj[y][i];
        if(cury!=curx){
            x=curx;
            y=cury;
        }
    }
    return bj[x][0];
}
int dis(int x,int y){
    return dep[x]+dep[y]-2*dep[lca(x,y)];
}
int main(){
    cin>>n;
    for (int i = 0; i < n-1; ++i)
    {
        int x,y;
        cin>>x>>y;
        x--;y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    get_size(0,-1);
    int cen=centroid(0,-1);
    dep[cen]=0;
    bj[cen][0]=cen;
    get_size(cen,-1);
    build(cen,-1);
    vector<int> tab[n+1];
    for (int i = 0; i < n; ++i)
    {
        tab[sz[i]].pb(i);
    }
    int a=cen;
    int b=cen;
    int ans=0;
    int res[n];
    //cout <<dep[4]<<" "<<dep[0]<<" "<<lca(0,4)<<endl;
    for (int i = n/2; i >=1 ; --i)
    {
        //cout <<"nabba"<<" "<<i<<endl;
        for (int j = 0; j < tab[i].size(); ++j)
        {
            //cout <<tab[i][j]<<endl;
            int nw=tab[i][j];
            int x=dis(nw,a);
            int y=dis(nw,b);
            //cout <<x<<" "<<y<<" "<<a<<" "<<b<<endl;
            if(ans<x){
                b=nw;
                ans=x;
            }
            if(ans<y){
                a=nw;
                ans=y;
            }
        }//cout <<endl;
        res[i]=ans;
    }
    for (int i = 1; i <= n; ++i)
    {
        if(i%2) cout <<1<<endl;
        else cout <<res[i/2]+1<<endl;
    }
}

Compilation message

meetings2.cpp: In function 'int main()':
meetings2.cpp:95:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for (int j = 0; j < tab[i].size(); ++j)
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 7000 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 3 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6760 KB Output is correct
11 Correct 1 ms 6748 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 1 ms 6748 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6744 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 1 ms 6748 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 1 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 7000 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 3 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6760 KB Output is correct
11 Correct 1 ms 6748 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 1 ms 6748 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6744 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 1 ms 6748 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 1 ms 6748 KB Output is correct
22 Correct 9 ms 9052 KB Output is correct
23 Correct 9 ms 9080 KB Output is correct
24 Correct 9 ms 9052 KB Output is correct
25 Correct 9 ms 9048 KB Output is correct
26 Correct 9 ms 9052 KB Output is correct
27 Correct 9 ms 9140 KB Output is correct
28 Correct 13 ms 9308 KB Output is correct
29 Correct 9 ms 9048 KB Output is correct
30 Correct 9 ms 9052 KB Output is correct
31 Correct 9 ms 9052 KB Output is correct
32 Correct 9 ms 9052 KB Output is correct
33 Correct 9 ms 9092 KB Output is correct
34 Correct 9 ms 9052 KB Output is correct
35 Correct 9 ms 9052 KB Output is correct
36 Correct 9 ms 9052 KB Output is correct
37 Correct 8 ms 9052 KB Output is correct
38 Correct 11 ms 9048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 7000 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 3 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6760 KB Output is correct
11 Correct 1 ms 6748 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 1 ms 6748 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6744 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 1 ms 6748 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 1 ms 6748 KB Output is correct
22 Correct 9 ms 9052 KB Output is correct
23 Correct 9 ms 9080 KB Output is correct
24 Correct 9 ms 9052 KB Output is correct
25 Correct 9 ms 9048 KB Output is correct
26 Correct 9 ms 9052 KB Output is correct
27 Correct 9 ms 9140 KB Output is correct
28 Correct 13 ms 9308 KB Output is correct
29 Correct 9 ms 9048 KB Output is correct
30 Correct 9 ms 9052 KB Output is correct
31 Correct 9 ms 9052 KB Output is correct
32 Correct 9 ms 9052 KB Output is correct
33 Correct 9 ms 9092 KB Output is correct
34 Correct 9 ms 9052 KB Output is correct
35 Correct 9 ms 9052 KB Output is correct
36 Correct 9 ms 9052 KB Output is correct
37 Correct 8 ms 9052 KB Output is correct
38 Correct 11 ms 9048 KB Output is correct
39 Correct 466 ms 37732 KB Output is correct
40 Correct 485 ms 36868 KB Output is correct
41 Correct 470 ms 37712 KB Output is correct
42 Correct 483 ms 38156 KB Output is correct
43 Correct 526 ms 38116 KB Output is correct
44 Correct 456 ms 38092 KB Output is correct
45 Correct 568 ms 40020 KB Output is correct
46 Correct 516 ms 44180 KB Output is correct
47 Correct 429 ms 38596 KB Output is correct
48 Correct 400 ms 38780 KB Output is correct
49 Correct 460 ms 38860 KB Output is correct
50 Correct 401 ms 38472 KB Output is correct
51 Correct 473 ms 41700 KB Output is correct