이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
const int N = 4007;
int n;
vector<int> adj[N];
int sz[N], dist[N];
int x, ans;
void prep(int v = 1, int p = 0) {
sz[v] = 1;
for (int u : adj[v]) {
if (u != p) {
prep(u, v);
sz[v] += sz[u];
}
}
}
void dfs(int v = 1, int p = 0) {
if (sz[v] >= x)
dist[v] = 0;
for (int u : adj[v]) {
if (u != p) {
dfs(u, v);
dist[v] = max(dist[v], dist[u] + 1);
}
}
if (n - sz[v] >= x)
ans = max(ans, dist[v] + 2);
}
int main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
prep();
for (int i = 1; i <= n; i++) {
if (i % 2 == 1)
cout << 1 << '\n';
else {
x = i / 2, ans = 1;
for (int j = 1; j <= n; j++)
dist[j] = -1e9;
dfs();
for (int j = 1; j <= n; j++) {
if (sz[j] == x) {
vector<bool> vis(n + 1);
queue<array<int, 2>> q;
q.push({j, 0});
vis[j] = 1;
while (q.size()) {
auto [v, d] = q.front();
q.pop();
if (sz[v] == x)
ans = max(ans, d + 1);
for (int u : adj[v]) {
if (!vis[u]) {
vis[u] = 1;
q.push({u, d + 1});
}
}
}
}
}
cout << ans << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |