답안 #404836

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404836 2021-05-15T06:09:04 Z dvdg6566 Meetings 2 (JOI21_meetings2) C++14
20 / 100
4000 ms 37712 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define pb emplace_back
#define f first 
#define s second
#define mp make_pair
#define SZ(x) (ll)x.size()
#define ALL(x) x.begin(),x.end()
#define lb lower_bound
#define ub upper_bound
 
const ll MAXN=200100;
const int MAXK=19;
 
ll N,a,b,c,Q;
vi V[MAXN];
ll sub[MAXN];
ll small[MAXN];
ll A[MAXN];
ll out[MAXN];
int p[MAXN][MAXK];
int d[MAXN];
 
ll dfs(ll x,ll pa){
    sub[x]=1;
    p[x][0]=pa;
    if(pa!=-1)d[x]=d[pa]+1;
    for(int i=1;i<MAXK;++i){
        if(p[x][i-1]!=-1)p[x][i]=p[p[x][i-1]][i-1];
    }
    for(auto v:V[x])if(v!=pa)sub[x]+=dfs(v,x);
    return sub[x];
}
 
set<pi> S;

int lca(int a,int b){
    if(a==b)return a;
    if(d[a]>d[b])swap(a,b); // d[a] < d[b]
    int hd=d[b]-d[a];
    for(int i=0;i<MAXK;++i)if((1<<i)&hd){
        b=p[b][i];
    }
    if(a==b)return a;
    for(int i=MAXK-1;i>=0;--i){
        if(p[a][i]!=p[b][i]){a=p[a][i];b=p[b][i];}
    }
    assert(p[a][0]==p[b][0]);
    return p[a][0];
}

int fd(int x,int y){
    return d[x]+d[y]-2*d[lca(x,y)]+1;
}

pi T;
int ans;

void addNode(int x){
    // cerr<<"Add "<<x<<'\n';
    if(T.f==-1)T=mp(x,x);
    else{
        int a=fd(x,T.f);
        int b=fd(x,T.s);
        if(a>=ans&&a>=b){
            T=mp(x,T.f);
        }else if(b>=a&&b>=ans){
            T=mp(x,T.s);
        }
        ans=max({ans,a,b});
    }
    assert(!A[x]);
    A[x]=1;
    for(auto v:V[x])if(!A[v]){
        S.insert(mp(small[v],v));
    }
    for(int i=1;i<=N;++i)if(A[i])ans=max(ans,fd(x,i));
}
 
int main(){
    T=mp(-1,-1);
    memset(p,-1,sizeof(p));
    cin>>N;
    for(ll i=1;i<N;++i){
        cin>>a>>b;
        V[a].pb(b);
        V[b].pb(a);
    }    
    dfs(1,-1);
    for(ll i=1;i<=N;++i){
        b=N-sub[i];
        for(auto j:V[i])if(j!=p[i][0]){
            b=max(b,sub[j]);
        }
        small[i]=N-b; // everything except biggest subtree
    }
    // for(ll i=1;i<=N;++i)cerr<<small[i]<<' ';cerr<<'\n';
    vpi nodes;
    for(ll i=1;i<=N;++i)nodes.pb(small[i],i);
    sort(ALL(nodes));
    S.insert(nodes.back());
 
    for(ll i=(N/2)*2; i>=2;i-=2){
        // cerr<<"At "<<i<<'\n';
        while(SZ(S)){
            pi t=*(--S.end());
            if(t.f<i/2)break;
            S.erase(--S.end());
            addNode(t.s);
        }
        for(int k=1;k<=N;++k)if(small[k]>=i/2)assert(A[k]);
        // cerr<<ans<<'\n';
        out[i]=ans;
    }
    for(int i=1;i<=N;++i){
        if(i%2)cout<<1<<'\n';
        else cout<<out[i]<<'\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19936 KB Output is correct
2 Correct 10 ms 19900 KB Output is correct
3 Correct 10 ms 19788 KB Output is correct
4 Correct 10 ms 19896 KB Output is correct
5 Correct 11 ms 19848 KB Output is correct
6 Correct 11 ms 19788 KB Output is correct
7 Correct 10 ms 19788 KB Output is correct
8 Correct 10 ms 19788 KB Output is correct
9 Correct 10 ms 19892 KB Output is correct
10 Correct 10 ms 19788 KB Output is correct
11 Correct 10 ms 19788 KB Output is correct
12 Correct 10 ms 19908 KB Output is correct
13 Correct 10 ms 19912 KB Output is correct
14 Correct 10 ms 19788 KB Output is correct
15 Correct 10 ms 19788 KB Output is correct
16 Correct 12 ms 19840 KB Output is correct
17 Correct 10 ms 19788 KB Output is correct
18 Correct 10 ms 19788 KB Output is correct
19 Correct 10 ms 19788 KB Output is correct
20 Correct 10 ms 19788 KB Output is correct
21 Correct 10 ms 19804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19936 KB Output is correct
2 Correct 10 ms 19900 KB Output is correct
3 Correct 10 ms 19788 KB Output is correct
4 Correct 10 ms 19896 KB Output is correct
5 Correct 11 ms 19848 KB Output is correct
6 Correct 11 ms 19788 KB Output is correct
7 Correct 10 ms 19788 KB Output is correct
8 Correct 10 ms 19788 KB Output is correct
9 Correct 10 ms 19892 KB Output is correct
10 Correct 10 ms 19788 KB Output is correct
11 Correct 10 ms 19788 KB Output is correct
12 Correct 10 ms 19908 KB Output is correct
13 Correct 10 ms 19912 KB Output is correct
14 Correct 10 ms 19788 KB Output is correct
15 Correct 10 ms 19788 KB Output is correct
16 Correct 12 ms 19840 KB Output is correct
17 Correct 10 ms 19788 KB Output is correct
18 Correct 10 ms 19788 KB Output is correct
19 Correct 10 ms 19788 KB Output is correct
20 Correct 10 ms 19788 KB Output is correct
21 Correct 10 ms 19804 KB Output is correct
22 Correct 801 ms 20360 KB Output is correct
23 Correct 765 ms 20420 KB Output is correct
24 Correct 759 ms 20340 KB Output is correct
25 Correct 730 ms 20356 KB Output is correct
26 Correct 729 ms 20548 KB Output is correct
27 Correct 763 ms 20368 KB Output is correct
28 Correct 767 ms 20360 KB Output is correct
29 Correct 756 ms 20420 KB Output is correct
30 Correct 758 ms 20364 KB Output is correct
31 Correct 749 ms 20464 KB Output is correct
32 Correct 1062 ms 20464 KB Output is correct
33 Correct 1058 ms 20388 KB Output is correct
34 Correct 562 ms 20412 KB Output is correct
35 Correct 513 ms 20428 KB Output is correct
36 Correct 1052 ms 20412 KB Output is correct
37 Correct 561 ms 20556 KB Output is correct
38 Correct 892 ms 20404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19936 KB Output is correct
2 Correct 10 ms 19900 KB Output is correct
3 Correct 10 ms 19788 KB Output is correct
4 Correct 10 ms 19896 KB Output is correct
5 Correct 11 ms 19848 KB Output is correct
6 Correct 11 ms 19788 KB Output is correct
7 Correct 10 ms 19788 KB Output is correct
8 Correct 10 ms 19788 KB Output is correct
9 Correct 10 ms 19892 KB Output is correct
10 Correct 10 ms 19788 KB Output is correct
11 Correct 10 ms 19788 KB Output is correct
12 Correct 10 ms 19908 KB Output is correct
13 Correct 10 ms 19912 KB Output is correct
14 Correct 10 ms 19788 KB Output is correct
15 Correct 10 ms 19788 KB Output is correct
16 Correct 12 ms 19840 KB Output is correct
17 Correct 10 ms 19788 KB Output is correct
18 Correct 10 ms 19788 KB Output is correct
19 Correct 10 ms 19788 KB Output is correct
20 Correct 10 ms 19788 KB Output is correct
21 Correct 10 ms 19804 KB Output is correct
22 Correct 801 ms 20360 KB Output is correct
23 Correct 765 ms 20420 KB Output is correct
24 Correct 759 ms 20340 KB Output is correct
25 Correct 730 ms 20356 KB Output is correct
26 Correct 729 ms 20548 KB Output is correct
27 Correct 763 ms 20368 KB Output is correct
28 Correct 767 ms 20360 KB Output is correct
29 Correct 756 ms 20420 KB Output is correct
30 Correct 758 ms 20364 KB Output is correct
31 Correct 749 ms 20464 KB Output is correct
32 Correct 1062 ms 20464 KB Output is correct
33 Correct 1058 ms 20388 KB Output is correct
34 Correct 562 ms 20412 KB Output is correct
35 Correct 513 ms 20428 KB Output is correct
36 Correct 1052 ms 20412 KB Output is correct
37 Correct 561 ms 20556 KB Output is correct
38 Correct 892 ms 20404 KB Output is correct
39 Execution timed out 4059 ms 37712 KB Time limit exceeded
40 Halted 0 ms 0 KB -