This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
if (fopen(filein.c_str(), "r")){
freopen(filein.c_str(), "r", stdin);
freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
freopen(fileerr.c_str(), "w", stderr);
#endif
}
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main(){
#ifdef hollwo_pelw_local
FAST_IO("input.inp", "output.out", "error.err");
auto start = chrono::steady_clock::now();
#else
FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
return 0;
}
const int N = 2e5 + 5, L = 20;
vector<int> adj[N];
struct calc_dist {
template<class T, const T& merge(const T&, const T&)> struct rmq {
vector<T> st[L];
rmq () {}
rmq (const vector<T> &a) {
st[0] = a; int n = a.size(); __build__(n);
}
rmq (T * __first, T * __last) {
st[0] = vector<T> (__first, __last);
int n = __last - __first; __build__(n);
}
inline void __build__(int n) {
for (int j = 1; 1 << j <= n; j++) {
st[j].resize(n - (1 << j) + 1);
for (int i = 0; i + (1 << j) <= n; i++)
st[j][i] = merge(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
}
}
inline T query(int l, int r) {
int j = __lg(r - l + 1);
// return merge(st[j][l], st[j][r - (1 << j) + 1]); // 0 base
return merge(st[j][l - 1], st[j][r - (1 << j)]); // 1 base
}
};
rmq<int, min<int>> rmq_dist;
int d[N], val[N << 1], tin[N], timer;
void pre_dfs(int u, int p) {
val[tin[u] = ++ timer] = d[u];
for (int v : adj[u]) if (v != p) {
d[v] = d[u] + 1;
pre_dfs(v, u);
val[++ timer] = d[u];
}
}
inline void __init__() {
pre_dfs(1, 0);
rmq_dist = rmq<int, min<int>> (val + 1, val + timer + 1);
}
inline int operator () (int u, int v) {
int l = tin[u], r = tin[v]; if (l > r) swap(l, r);
return d[u] + d[v] - 2 * rmq_dist.query(l, r);
}
} dist;
int n, res[N], sz[N], vis[N], deg[N];
vector<int> leaf[N];
inline int solve(int sz) {
vector<int> lf;
for (int i = sz; i <= n; i++)
lf.insert(lf.end(), leaf[i].begin(), leaf[i].end());
if (lf.empty()) return 1;
int d1 = lf[0], d2 = lf[0];
for (auto v : lf) {
if (dist(d1, v) > dist(d1, d2))
d2 = v;
}
for (auto v : lf) {
if (dist(d2, v) > dist(d1, d2))
d1 = v;
}
return dist(d1, d2) + 1;
}
void Hollwo_Pelw() {
cin >> n;
for (int i = 1, u, v; i < n; i++)
cin >> u >> v, adj[u].push_back(v), adj[v].push_back(u);
dist.__init__();
fill(sz + 1, sz + n + 1, 1);
for (int i = 1; i <= n; i++) {
if (adj[i].size() == 1 + deg[i])
vis[i] = 1, leaf[1].push_back(i);
}
for (int i = 1; i <= n; i++) for (auto u : leaf[i]) {
for (auto v : adj[u]) if (!vis[v]) {
sz[v] += sz[u];
++ deg[v];
if (adj[v].size() == 1 + deg[v])
vis[v] = 1, leaf[sz[v]].push_back(v);
}
}
fill(res + 1, res + n + 1, 1);
int d1 = -1, d2 = -1;
for (int i = n; i >= 1; i--) for (auto v : leaf[i]) {
if (d1 == -1) d1 = v;
if (d2 == -1) d2 = v;
if (dist(v, d2) > dist(d1, d2)) d1 = v;
if (dist(d1, v) > dist(d1, d2)) d2 = v;
int j = min(sz[d1], sz[d2]) * 2;
res[j] = max(res[j], dist(d1, d2) + 1);
}
for (int j = n - n % 2; j > 2; j -= 2)
res[j - 2] = max(res[j - 2], res[j]);
for (int i = 1; i <= n; i++)
cout << res[i] << '\n';
}
Compilation message (stderr)
meetings2.cpp: In function 'void Hollwo_Pelw()':
meetings2.cpp:138:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
138 | if (adj[i].size() == 1 + deg[i])
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~
meetings2.cpp:147:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
147 | if (adj[v].size() == 1 + deg[v])
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~
meetings2.cpp: In instantiation of 'void calc_dist::rmq<T, merge>::__build__(int) [with T = int; const T& (* merge)(const T&, const T&) = std::min<int>]':
meetings2.cpp:62:30: required from 'calc_dist::rmq<T, merge>::rmq(T*, T*) [with T = int; const T& (* merge)(const T&, const T&) = std::min<int>]'
meetings2.cpp:95:58: required from here
meetings2.cpp:69:59: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
69 | st[j][i] = merge(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
| ~~^~~
meetings2.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
meetings2.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(filein.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
meetings2.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(fileout.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |