Submission #1171684

#TimeUsernameProblemLanguageResultExecution timeMemory
1171684PedroBigManMeetings 2 (JOI21_meetings2)C++20
20 / 100
478 ms327680 KiB
/*
Author of all code: Pedro BIGMAN Dias
Last edit: 15/02/2021
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 500000000LL
#define EPS 0.00000001
#define pi 3.14159
ll mod=1000000007LL;

template<class A=ll> 
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}

template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} 

template<class T=ll>
class SparseTable //Range Minimum Queries
{
    public:
    ll N; 
    vector<T> a;
    vector<vector<T> > v;
	T neut = mp(INF,INF); //set for pairs due to LCA
    
    SparseTable() {N=0LL;}
    SparseTable(vector<T> b)
    {
        a=b; N=a.size();
        ll lo=(ll) floor((double) log2(N)) +1LL;
        vector<T> xx; 
        REP(i,0,lo) {xx.pb(neut);} REP(i,0,N) {v.pb(xx);}
        REP(step,0LL,lo)
        {
            REP(i,0,N-(1LL<<step)+1LL)
            {
                if(step==0) {v[i][0]=a[i];}
                else {v[i][step]=min(v[i][step-1],v[i+(1LL<<(step-1))][step-1]);}
            }
        }
    }
    
    T query(ll l, ll r)
    {
        ll step=(ll) floor((double) log2(r-l+1LL));
        return min(v[l][step],v[r-(1LL<<step)+1LL][step]);
    }
};

class Tree
{
    public:
    ll N; 
    vector<ll> p; 
    vector<vector<ll> > sons;
    vector<vector<ll> > adj;
    ll root;
    vector<bool> visited;
    vector<ll> level; //starting in 0
    vector<ll> sub; //number of nodes in subtree
    vector<ll> val; //node values
    vector<ll> DFSarr1; //DFS Array
    vector<ll> DFSarr2; //DFS Array for LCA with whole path
    vector<ll> pos; //inverted DFSArr, only for LCA
    vector<pl> levDFSarr; //array of levels on DFSarr, only needed for LCA
    vector<ll> sumto; //weighted graph, length of path root-->i
    SparseTable<pl> S; //for LCA
    
    Tree(vector<vector<ll> > ad, ll r=0LL)
    {
        N=ad.size(); root=r; adj=ad;
        REP(i,0,N) {visited.pb(false);}
        vector<ll> xx; REP(i,0,N) {sons.pb(xx); p.pb(-1); level.pb(0); sub.pb(1LL); pos.pb(0LL); sumto.pb(0LL);}
        DFS_Build(r,r);
        REP(i,0,DFSarr2.size()) {pos[DFSarr2[i]]=i;}
        REP(i,0,DFSarr2.size()) {levDFSarr.pb(mp(level[DFSarr2[i]],DFSarr2[i]));}
        SparseTable<pl> X(levDFSarr); S=X;
    }
    
    void Reset()
    {
        REP(i,0,N) {visited[i]=false;}
    }
    
    void DFS_Build(ll s, ll par)
    {
        DFSarr1.pb(s);
        DFSarr2.pb(s);
        if(s!=root) {level[s]=level[par]+1LL;}
        p[s]=par;
        visited[s]=true;
        REP(i,0,adj[s].size())
        {
            if(adj[s][i]==par) {continue;}
            sons[s].pb(adj[s][i]);
            DFS_Build(adj[s][i],s);
            sub[s]+=sub[adj[s][i]];
            DFSarr2.pb(s);
        }
        return;
    }
	
    ll LCA(ll a, ll b)
    {
        a=pos[a]; b=pos[b]; 
        ll l=min(a,b); ll r=max(a,b);
        pl ans=S.query(l,r);
        return ans.ss;
    }
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll N; cin>>N; vector<ll> xx; vector<vector<ll> > adj; REP(i,0,N) {adj.pb(xx);}
	pl cur;
	REP(i,0,N-1)
	{
		cin>>cur.ff>>cur.ss; cur.ff--; cur.ss--; adj[cur.ff].pb(cur.ss); adj[cur.ss].pb(cur.ff);
	}
	Tree T(adj);
	vector<ll> ans; REP(i,0,N+1) {ans.pb(1LL);}
	REP(i,0,N)
	{
		REP(j,0,N)
		{
			ll lca = T.LCA(i,j);
			if(lca!=i && lca!=j && i>j)
			{
				ll val = min(T.sub[i],T.sub[j]);
				ans[2*val]=max(ans[2*val],T.level[i]+T.level[j]-2*T.level[lca]+1LL);
			}
			else if(lca==i)
			{
				if(i==T.root) {continue;}
				ll val = min(T.sub[j],N-T.sub[i]);
				ans[2*val]=max(ans[2*val],T.level[j]-T.level[i]+2LL);
			}
		}
	}
	vector<ll> t_ans; ll mv=1LL; REP(i,0,N+1) {t_ans.pb(1LL);}
	for(ll i=N;i>=0;i--) {mv=max(mv,ans[i]); if(i%2==0) {t_ans[i]=mv;}}
	REP(i,1,N+1) {cout<<t_ans[i]<<endl;}
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...