Submission #914904

# Submission time Handle Problem Language Result Execution time Memory
914904 2024-01-22T20:40:35 Z alexander707070 Meetings 2 (JOI21_meetings2) C++14
20 / 100
4000 ms 63868 KB
#include<bits/stdc++.h>
#define MAXN 300007
using namespace std;

struct dude{
    int h,l;
};

int n,a,b,sz[MAXN],root,parent[MAXN],pt[MAXN],ans[MAXN];
int dist[MAXN],maxs,dep[MAXN],from,to,diameter;
vector<int> v[MAXN];
vector<int> rem[MAXN],s;
bool used[MAXN],in[MAXN];

unordered_set<int> active;

void dfs(int x,int p,int d){
    sz[x]=1; parent[x]=p; dep[x]=d;

    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p)continue;
        dfs(v[x][i],x,d+1);
        sz[x]+=sz[v[x][i]];
    }
}

int dp[MAXN][20];
bool li[MAXN][20];

int ff(int x,int p){
    if(p==0)return parent[x];
    if(x==0)return 0;

    if(li[x][p])return dp[x][p];
    li[x][p]=true;

    dp[x][p]=ff(ff(x,p-1),p-1);
    return dp[x][p];
}

int getdist(int x,int y){
    int res=0;

    if(dep[x]<dep[y])swap(x,y);

    for(int i=19;i>=0;i--){
        if((1<<i)<=dep[x]-dep[y]){
            x=ff(x,i);
            res+=(1<<i);
        }
    }

    for(int i=19;i>=0;i--){
        if(ff(x,i)!=0 and ff(y,i)!=0 and ff(x,i)!=ff(y,i)){
            res+=2*(1<<i);
            x=ff(x,i); y=ff(y,i);
        }
    }

    if(x!=y)res+=2;

    return res;
}

int centroid(int x,int p){
    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p)continue;
        if(sz[v[x][i]]>n/2){
            return centroid(v[x][i],x);
        }
    }

    return x;
}

void dfs1(int x,int p,int d){
    diameter=max(diameter,d);

    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p or !in[v[x][i]])continue;
        dfs1(v[x][i],x,d+1);
    }
}

void add(int x){
    in[x]=true;

    if(from==0){
        from=to=x;
        diameter=0;
        return;
    }

    for(int i=0;i<v[x].size();i++){
        if(in[v[x][i]]){
            if(getdist(x,from)>diameter){
                diameter=getdist(x,from);
                to=x;
            }

            if(getdist(x,to)>diameter){
                diameter=getdist(x,to);
                from=x;
            }
        }
    }
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    for(int i=1;i<=n-1;i++){
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    dfs(1,0,0);
    root=centroid(1,0);
    dfs(root,0,0);

    for(int i=1;i<=n;i++){
        pt[i]=i; active.insert(i);
    }

    for(int k=1;k<=n/2;k++){

        for(int i:active){
            while(sz[pt[i]]<k){

                if(used[pt[i]]){
                    s.push_back(i);
                    break;
                }
                
                rem[k].push_back(pt[i]);
                used[pt[i]]=true;
                pt[i]=parent[pt[i]];
            }
        }

        reverse(rem[k].begin(),rem[k].end());

        for(int i:s){
            active.erase(i);
        }
        s.clear();
    }

    add(root);
    for(int i=1;i<=n;i++){
        if(!used[i] and i!=root){
            add(i);
        }
    }

    for(int k=n/2;k>=1;k--){

        ans[2*k]=diameter+1;

        for(int f:rem[k]){
            add(f);
        }
    }

    for(int i=1;i<=n;i++){
        if(i%2==1)cout<<"1\n";
        else cout<<ans[i]<<"\n";
    }

    return 0;
}

Compilation message

meetings2.cpp: In function 'void dfs(int, int, int)':
meetings2.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
meetings2.cpp: In function 'int centroid(int, int)':
meetings2.cpp:66:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
meetings2.cpp: In function 'void dfs1(int, int, int)':
meetings2.cpp:79:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
meetings2.cpp: In function 'void add(int)':
meetings2.cpp:94:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 18780 KB Output is correct
2 Correct 5 ms 22876 KB Output is correct
3 Correct 6 ms 22872 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 5 ms 22976 KB Output is correct
6 Correct 5 ms 23028 KB Output is correct
7 Correct 6 ms 22876 KB Output is correct
8 Correct 5 ms 22872 KB Output is correct
9 Correct 6 ms 23036 KB Output is correct
10 Correct 5 ms 22876 KB Output is correct
11 Correct 7 ms 22876 KB Output is correct
12 Correct 6 ms 22876 KB Output is correct
13 Correct 5 ms 23040 KB Output is correct
14 Correct 5 ms 22876 KB Output is correct
15 Correct 6 ms 23036 KB Output is correct
16 Correct 7 ms 22872 KB Output is correct
17 Correct 5 ms 22876 KB Output is correct
18 Correct 6 ms 22876 KB Output is correct
19 Correct 7 ms 22876 KB Output is correct
20 Correct 6 ms 22872 KB Output is correct
21 Correct 6 ms 22876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 18780 KB Output is correct
2 Correct 5 ms 22876 KB Output is correct
3 Correct 6 ms 22872 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 5 ms 22976 KB Output is correct
6 Correct 5 ms 23028 KB Output is correct
7 Correct 6 ms 22876 KB Output is correct
8 Correct 5 ms 22872 KB Output is correct
9 Correct 6 ms 23036 KB Output is correct
10 Correct 5 ms 22876 KB Output is correct
11 Correct 7 ms 22876 KB Output is correct
12 Correct 6 ms 22876 KB Output is correct
13 Correct 5 ms 23040 KB Output is correct
14 Correct 5 ms 22876 KB Output is correct
15 Correct 6 ms 23036 KB Output is correct
16 Correct 7 ms 22872 KB Output is correct
17 Correct 5 ms 22876 KB Output is correct
18 Correct 6 ms 22876 KB Output is correct
19 Correct 7 ms 22876 KB Output is correct
20 Correct 6 ms 22872 KB Output is correct
21 Correct 6 ms 22876 KB Output is correct
22 Correct 8 ms 23388 KB Output is correct
23 Correct 9 ms 23388 KB Output is correct
24 Correct 9 ms 23388 KB Output is correct
25 Correct 9 ms 23384 KB Output is correct
26 Correct 9 ms 23304 KB Output is correct
27 Correct 9 ms 23388 KB Output is correct
28 Correct 10 ms 23456 KB Output is correct
29 Correct 8 ms 23388 KB Output is correct
30 Correct 8 ms 23464 KB Output is correct
31 Correct 9 ms 23388 KB Output is correct
32 Correct 21 ms 23644 KB Output is correct
33 Correct 18 ms 23640 KB Output is correct
34 Correct 16 ms 23384 KB Output is correct
35 Correct 22 ms 23384 KB Output is correct
36 Correct 10 ms 23388 KB Output is correct
37 Correct 17 ms 23332 KB Output is correct
38 Correct 18 ms 23504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 18780 KB Output is correct
2 Correct 5 ms 22876 KB Output is correct
3 Correct 6 ms 22872 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 5 ms 22976 KB Output is correct
6 Correct 5 ms 23028 KB Output is correct
7 Correct 6 ms 22876 KB Output is correct
8 Correct 5 ms 22872 KB Output is correct
9 Correct 6 ms 23036 KB Output is correct
10 Correct 5 ms 22876 KB Output is correct
11 Correct 7 ms 22876 KB Output is correct
12 Correct 6 ms 22876 KB Output is correct
13 Correct 5 ms 23040 KB Output is correct
14 Correct 5 ms 22876 KB Output is correct
15 Correct 6 ms 23036 KB Output is correct
16 Correct 7 ms 22872 KB Output is correct
17 Correct 5 ms 22876 KB Output is correct
18 Correct 6 ms 22876 KB Output is correct
19 Correct 7 ms 22876 KB Output is correct
20 Correct 6 ms 22872 KB Output is correct
21 Correct 6 ms 22876 KB Output is correct
22 Correct 8 ms 23388 KB Output is correct
23 Correct 9 ms 23388 KB Output is correct
24 Correct 9 ms 23388 KB Output is correct
25 Correct 9 ms 23384 KB Output is correct
26 Correct 9 ms 23304 KB Output is correct
27 Correct 9 ms 23388 KB Output is correct
28 Correct 10 ms 23456 KB Output is correct
29 Correct 8 ms 23388 KB Output is correct
30 Correct 8 ms 23464 KB Output is correct
31 Correct 9 ms 23388 KB Output is correct
32 Correct 21 ms 23644 KB Output is correct
33 Correct 18 ms 23640 KB Output is correct
34 Correct 16 ms 23384 KB Output is correct
35 Correct 22 ms 23384 KB Output is correct
36 Correct 10 ms 23388 KB Output is correct
37 Correct 17 ms 23332 KB Output is correct
38 Correct 18 ms 23504 KB Output is correct
39 Correct 316 ms 61068 KB Output is correct
40 Correct 311 ms 62624 KB Output is correct
41 Correct 309 ms 63280 KB Output is correct
42 Correct 317 ms 63816 KB Output is correct
43 Correct 318 ms 63868 KB Output is correct
44 Correct 343 ms 63868 KB Output is correct
45 Execution timed out 4005 ms 42880 KB Time limit exceeded
46 Halted 0 ms 0 KB -