// author: erray
#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> string to_string(const pair<A, B>& p);
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t);
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char& c) {
return string("'") + c + "'";
}
string to_string(const char *c) {
return to_string(string(c));
}
string to_string(const bool& b) {
return (b ? "true" : "false");
}
string to_string(const vector<bool>& v) {
string res = "{";
for (int i = 0; i < (int) v.size(); ++i) {
if (i > 0) {
res += ", ";
}
res += to_string(v[i]);
}
res += "}";
return res;
}
template<size_t T> string to_string(const bitset<T>& bs) {
return bs.to_string();
}
template<typename T> string to_string(queue<T> q) {
string res = "{";
size_t sz = q.size();
while (sz--) {
T cur = q.front();
q.pop();
if ((int) res.size() > 1) {
res += ", ";
}
res += to_string(cur);
}
res += "}";
return res;
}
template<typename T, class C> string to_string(priority_queue<T, vector<T>, C> pq) {
string res = "{";
while (!pq.empty()) {
T cur = pq.top();
pq.pop();
if ((int) res.size() > 1) {
res += ", ";
}
res += to_string(cur);
}
res += "}";
return res;
}
template<typename T> string to_string(const T& v) {
string res = "{";
for (auto &el : v) {
if ((int) res.size() > 1) {
res += ", ";
}
res += to_string(el);
}
res += "}";
return res;
}
template<typename A, typename B> string to_string(const pair<A, B>& p) {
return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t) {
return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t) {
return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ')';
}
void debug_out(int size, bool first, string name) {
vector<string> tmp = {name};
vector<int> tmp2 = {size, first};
cerr << endl;
}
constexpr int buffer_size = 255;
template<typename Head, typename... Tail> void debug_out(int size, bool first, string name, Head H, Tail... T) {
string tmp;
int off = 0;
while ((!name.empty() && name[0] != ',') || off != 0) {
tmp += name[0];
name.erase(name.begin());
char c = tmp.back();
if (c == '{' || c == '(') {
++off;
} else if (c == '}' || c == ')'){
--off;
}
}
if (!name.empty()) {
name.erase(name.begin());
}
if (tmp[0] == ' ') {
tmp.erase(tmp.begin());
}
string buff = to_string(H);
if ((int) buff.size() + size + (int) tmp.size() > buffer_size - 5 && !first) {
cerr << '\n';
size = 0;
}
cerr << '[' << tmp << ": " << buff << "] ";
debug_out(((int) buff.size() + size + (int) tmp.size() + 5) % buffer_size, false, name, T...);
}
#ifdef DEBUG
#define debug(...) cerr << "-> ", debug_out(3, true, string(#__VA_ARGS__), __VA_ARGS__)
#define here cerr << "-> " << __LINE__ << endl
#else
#define debug(...) void(37)
#define here void(37)
#endif
struct Fenwick {
const int def = 0;
vector<int> a;
int n;
Fenwick(int _n) : n(_n) {
a.resize(n + 1, def);
}
int get(int x) {
x = n - x;
int res = def;
while (x > 0) {
res = max(res, a[x]);
x -= x & -x;
}
return res;
}
void modify(int x, int c) {
x = n - x;
while (x <= n) {
a[x] = max(a[x], c);
x += x & -x;
}
}
};
string to_string(Fenwick x) {
string res;
for (int i = 0; i < x.n; ++i) {
if (i > 0) {
res += ", ";
}
res += to_string(x.get(i));
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
vector<int> size(n, 1);
function<void(int, int)> Pre = [&](int v, int pr) {
for (auto u : g[v]) {
if (u != pr) {
Pre(u, v);
size[v] += size[u];
}
}
};
Pre(0, -1);
debug(size);
int root = -1;
int next = 0;
while (next != -1) {
root = next;
next = -1;
for (auto u : g[root]) {
if (size[u] * 2 > n) {
next = u;
size[root] -= size[next];
size[next] += size[root];
}
}
}
debug(root, size);
debug(g);
vector<array<int, 2>> ct(n, {-1, -1});
vector<int> ans(n + 1, -1);
vector<int> d(n);
vector<Fenwick*> dp(n);
function<void(int)> Dfs = [&](int v) {
for (int i = 0; i < int(g[v].size()); ++i) {
int u = g[v][i];
g[u].erase(find(g[u].begin(), g[u].end(), v));
d[u] = d[v] + 1;
Dfs(u);
if (size[u] > size[g[v][0]]) {
swap(g[v][0], g[v][i]);
}
}
debug(v);
debug(g[v]);
if (g[v].empty()) {
dp[v] = new Fenwick(n + 2);
} else {
dp[v] = dp[g[v][0]];
}
for (int i = 1; i < int(g[v].size()); ++i) {
int u = g[v][i];
vector<int> que;
que.push_back(u);
for (int j = 0; j < int(que.size()); ++j) {
int x = que[j];
for (auto nu : g[x]) {
que.push_back(nu);
}
}
debug(que);
debug(*dp[v]);
for (auto e : que) {
ans[size[e]] = max(ans[size[e]], dp[v]->get(size[e]) + d[e] - 2 * d[v] + 1);
}
for (auto e : que) {
dp[v]->modify(size[e], d[e]);
}
}
dp[v]->modify(size[v], d[v]);
ans[size[v]] = max(ans[size[v]], d[v] + 1);
debug(v, *dp[v], size[v], d[v]);
debug(ans);
};
Dfs(root);
debug(d);
for (int i = n; i > 0; --i) {
ans[i - 1] = max(ans[i - 1], ans[i]);
}
for (int i = 1; i <= n; ++i) {
cout << (i % 2 ? 1 : ans[i / 2]) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |