/*
// 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 = 18;
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 sub) {
vector<int> lf;
for (int i = sub; 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);
for (int i = 2; i <= n; i += 2)
res[i] = solve(i / 2);
for (int i = 1; i <= n; i++)
cout << res[i] << '\n';
}
Compilation message
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 |
1 |
Correct |
4 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
4 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9752 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
4 ms |
9676 KB |
Output is correct |
14 |
Correct |
6 ms |
9672 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9676 KB |
Output is correct |
20 |
Correct |
4 ms |
9676 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
4 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9752 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
4 ms |
9676 KB |
Output is correct |
14 |
Correct |
6 ms |
9672 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9676 KB |
Output is correct |
20 |
Correct |
4 ms |
9676 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
22 |
Correct |
14 ms |
10444 KB |
Output is correct |
23 |
Correct |
12 ms |
10316 KB |
Output is correct |
24 |
Correct |
12 ms |
10440 KB |
Output is correct |
25 |
Correct |
13 ms |
10384 KB |
Output is correct |
26 |
Correct |
12 ms |
10448 KB |
Output is correct |
27 |
Correct |
13 ms |
10384 KB |
Output is correct |
28 |
Correct |
12 ms |
10444 KB |
Output is correct |
29 |
Correct |
12 ms |
10484 KB |
Output is correct |
30 |
Correct |
12 ms |
10420 KB |
Output is correct |
31 |
Correct |
13 ms |
10476 KB |
Output is correct |
32 |
Correct |
70 ms |
10508 KB |
Output is correct |
33 |
Correct |
93 ms |
10584 KB |
Output is correct |
34 |
Correct |
11 ms |
10448 KB |
Output is correct |
35 |
Correct |
12 ms |
10488 KB |
Output is correct |
36 |
Correct |
16 ms |
10512 KB |
Output is correct |
37 |
Correct |
12 ms |
10448 KB |
Output is correct |
38 |
Correct |
34 ms |
10444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
4 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9752 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
4 ms |
9676 KB |
Output is correct |
14 |
Correct |
6 ms |
9672 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9676 KB |
Output is correct |
20 |
Correct |
4 ms |
9676 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
22 |
Correct |
14 ms |
10444 KB |
Output is correct |
23 |
Correct |
12 ms |
10316 KB |
Output is correct |
24 |
Correct |
12 ms |
10440 KB |
Output is correct |
25 |
Correct |
13 ms |
10384 KB |
Output is correct |
26 |
Correct |
12 ms |
10448 KB |
Output is correct |
27 |
Correct |
13 ms |
10384 KB |
Output is correct |
28 |
Correct |
12 ms |
10444 KB |
Output is correct |
29 |
Correct |
12 ms |
10484 KB |
Output is correct |
30 |
Correct |
12 ms |
10420 KB |
Output is correct |
31 |
Correct |
13 ms |
10476 KB |
Output is correct |
32 |
Correct |
70 ms |
10508 KB |
Output is correct |
33 |
Correct |
93 ms |
10584 KB |
Output is correct |
34 |
Correct |
11 ms |
10448 KB |
Output is correct |
35 |
Correct |
12 ms |
10488 KB |
Output is correct |
36 |
Correct |
16 ms |
10512 KB |
Output is correct |
37 |
Correct |
12 ms |
10448 KB |
Output is correct |
38 |
Correct |
34 ms |
10444 KB |
Output is correct |
39 |
Runtime error |
151 ms |
95176 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |