제출 #1333505

#제출 시각아이디문제언어결과실행 시간메모리
1333505PlayVoltzMeetings 2 (JOI21_meetings2)C++20
100 / 100
602 ms68200 KiB
#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
#include <random>
#include <chrono>
#include <fstream> 
using namespace std;

#define int long long
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back

vector<int> sz, depth;
vector<vector<int> > graph, twok;

void dfs(int node, int p, int d){
	depth[node]=d;
	twok[node][0]=p;
	sz[node]=1;
	for (int i=1; i<20; ++i)twok[node][i]=twok[twok[node][i-1]][i-1];
	for (auto num:graph[node])if (num!=p)dfs(num, node, d+1), sz[node]+=sz[num];
}

int find_centroid(int node, int p, int size){
	for (auto num:graph[node])if (num!=p&&sz[num]>size/2)return find_centroid(num, node, size);
	return node;
}

int lca(int a, int b){
	if (depth[a]<depth[b])swap(a, b);
	for (int i=0, k=depth[a]-depth[b]; i<20; ++i)if (k&(1<<i))a=twok[a][i];
	if (a==b)return a;
	for (int i=19; i>=0; --i)if (twok[a][i]!=twok[b][i])a=twok[a][i], b=twok[b][i];
	return twok[a][0];
}

int dist(int a, int b){
	return depth[a]+depth[b]-2*depth[lca(a, b)];
}

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, a, b, p=0;
	cin>>n;
	graph.resize(n+1);
	sz.resize(n+1);
	depth.resize(n+1);
	twok.resize(n+1, vector<int>(20));
	vector<int> ans(n+1, 1);
	vector<pii> ord;
	for (int i=1; i<n; ++i){
		cin>>a>>b;
		graph[a].pb(b);
		graph[b].pb(a);
	}
	dfs(1, 1, 0);
	a=b=find_centroid(1, 1, n);
	dfs(a, a, 0);
	for (int i=1; i<=n; ++i)if (i!=a)ord.pb(mp(sz[i], i));
	sort(ord.begin(), ord.end(), greater<pii>());
	for (int i=n-n%2; i>0; i-=2){
		while (p<ord.size()&&ord[p].fi>=i/2){
			if (dist(ord[p].se, b)>dist(a, b))a=ord[p].se;
			else if (dist(a, ord[p].se)>dist(a, b))b=ord[p].se;
			++p;
		}
		ans[i]=dist(a, b)+1;
	}
	for (int i=1; i<=n; ++i)cout<<ans[i]<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...