Submission #1300620

#TimeUsernameProblemLanguageResultExecution timeMemory
1300620Zbyszek99Meetings 2 (JOI21_meetings2)C++20
0 / 100
2 ms576 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

vi graph[200001];
int dist[200001];
int siz1[200001];
int siz2[200001];
int ans[200001];
int v1,v2;
vi ls;
vi st;

void dfs_dist(int v, int pop, int d = 0)
{
    dist[v] = d++;
    forall(it,graph[v]) if(it != pop) dfs_dist(it,v,d);
}

void dfs(int v, int pop)
{
    st.pb(v);
    if(v == v2) ls = st;
    forall(it,graph[v]) if(it != pop) dfs(it,v);
    st.pop_back();
}

int dfs_siz(int v, int pop)
{
    int s = 1;
    forall(it,graph[v]) if(it != pop) s += dfs_siz(it,v);
    return s;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    int n;
    cin >> n;
    rep(i,n-1)
    {
        int a,b;
        cin >> a >> b;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    dfs_dist(1,1);
    pii best = {0,-1};
    rep2(i,1,n) best = max(best,{dist[i],i});
    v1 = best.ss;
    dfs_dist(v1,v1);
    best = {0,-1};
    rep2(i,1,n) best = max(best,{dist[i],i});
    v2 = best.ss;
    dfs(v1,v1);
    int pop_siz = 0;
    rep(i,siz(ls))
    {
        siz1[i] = pop_siz+1;
        forall(it,graph[ls[i]])
        {
            if((i != siz(ls)-1 && it == ls[i+1]) || (i != 0 && it == ls[i-1])) continue;
            siz1[i] += dfs_siz(it,ls[i]);
        }
        pop_siz = siz1[i];
    }
    pop_siz = 0;
    for(int i = siz(ls)-1; i >= 0; i--)
    {
        siz2[i] = pop_siz+1;
        forall(it,graph[ls[i]])
        {
            if((i != siz(ls)-1 && it == ls[i+1]) || (i != 0 && it == ls[i-1])) continue;
            siz2[i] += dfs_siz(it,ls[i]);
        }
        pop_siz = siz2[i];
    }
    int p1 = 0;
    int p2 = siz(ls)-1;
    rep2(i,1,n/2)
    {
        while(siz1[p1] < i) p1++;
        while(siz2[p2] < i) p2--;
        ans[i*2] = max(0,p2-p1)+1;
    }
    rep2(i,1,n) if(i % 2 == 1) cout << "1\n"; else cout << ans[i] << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...