답안 #872991

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
872991 2023-11-14T08:52:35 Z serifefedartar Meetings 2 (JOI21_meetings2) C++17
0 / 100
6 ms 12892 KB
#include <bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9+9;
const ll LOGN = 20; 
const ll MAXN = 2e5 + 100;

vector<vector<int>> graph;
vector<int> v[MAXN];
int sz[MAXN], depth[MAXN];
int up[LOGN][MAXN], ans[MAXN];
int get_sz(int node, int parent) {
	sz[node] = 1;
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		sz[node] += get_sz(u, node);
	}
}

int find_centro(int node, int parent, int n) {
	for (auto u : graph[node]) {
		if (u != parent && sz[u] * 2 >= n)
			return find_centro(u, node, n);
	}
	return node;
}

int dfs(int node, int parent) {
	sz[node] = 1;
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		depth[u] = depth[node] + 1;
		up[0][u] = node;
		for (int i = 1; i < LOGN; i++)
			up[i][u] = up[i-1][up[i-1][u]];
		sz[node] += dfs(u, node);
	}
	v[sz[node]].push_back(node);
	return sz[node];
}

int find(int node, int k) {
	for (int i = 0; i < LOGN; i++) {
		if ((1<<i) & k)
			node = up[i][node];
	}
	return node;
}

int lca(int a, int b) {
	if (depth[b] > depth[a])
		swap(a, b);
	a = find(a, depth[a] - depth[b]);

	if (a == b)
		return a;

	for (int i = LOGN-1; i >= 0; i--) {
		if (up[i][a] != up[i][b]) {
			a = up[i][a];
			b = up[i][b];
		}
	}
	return up[0][a];
}

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

int main() {
	fast
	int N, A, B;
	cin >> N;

	graph = vector<vector<int>>(N+1, vector<int>());
	for (int i = 1; i < N; i++) {
		cin >> A >> B;
		graph[A].push_back(B);
		graph[B].push_back(A);
	}
	get_sz(1, 1);
	int centro = find_centro(1, 1, sz[1]);
	dfs(centro, centro);

	for (int i = 0; i < MAXN; i++)
		ans[i] = 1;

	int n1 = centro, n2 = centro, diameter = 0;
	for (int i = N; i > N / 2; i--) {
		for (auto u : v[i]) {
			if (dist(u, n1) > diameter)
				n2 = u, diameter = dist(u, n1);
			if (dist(u, n2) > diameter)
				n1 = u, diameter = dist(u, n2);
		}
	}

	for (int i = N / 2; i >= 1; i--) {
		for (auto u : v[i]) {
			if (dist(u, n1) > diameter)
				n2 = u, diameter = dist(u, n1);
			if (dist(u, n2) > diameter)
				n1 = u, diameter = dist(u, n2);
		}
		ans[i*2] = diameter + 1;
	}

	for (int i = 1; i <= N; i++)
		cout << ans[i] << "\n";
}

Compilation message

meetings2.cpp: In function 'int get_sz(int, int)':
meetings2.cpp:23:1: warning: no return statement in function returning non-void [-Wreturn-type]
   23 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 12892 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 12892 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 12892 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -