This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
#define orz
#ifdef orz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("[" + string(#a) + "] = ", a)
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#else
#define print(a..)
void kout() {}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) {}
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int main(){
StarBurstStream
int n;
cin >> n;
vector<vector<int>> g(n + 1);
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
vector<int> sz(n + 1);
int cen = -1;
function<void(int, int)> dfs;
dfs = [&](int now, int p){
sz[now] = 1;
int mx = 0;
for(int i : g[now]){
if(i == p) continue;
dfs(i, now);
sz[now] += sz[i];
mx = max(mx, sz[i]);
}
mx = max(mx, n - sz[now]);
if(mx <= n / 2) cen = now;
};
dfs(1, 1);
vector<pii> all;
vector<int> in(n + 1), out(n + 1), dpt(n + 1);
const int SZ = 20;
vector<vector<int>> anc(SZ, vector<int>(n + 1));
int ts = 0;
dfs = [&](int now, int p){
dpt[now] = dpt[p] + 1;
anc[0][now] = p;
in[now] = ts++;
sz[now] = 1;
for(int i : g[now]){
if(i == p) continue;
dfs(i, now);
sz[now] += sz[i];
}
all.eb(mp(sz[now], now));
out[now] = ts++;
};
dfs(cen, cen);
gsort(all);
for(int i = 1; i < SZ; i++){
for(int j = 1; j <= n; j++) anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
auto isAnc = [&](int a, int b){
return in[a] <= in[b] && out[a] >= out[b];
};
auto getlca = [&](int a, int b){
if(isAnc(a, b)) return a;
for(int i = SZ - 1; i >= 0; i--){
if(!isAnc(anc[i][a], b)) a = anc[i][a];
}
return anc[0][a];
};
auto dis = [&](int a, int b){
int lca = getlca(a, b);
return dpt[a] + dpt[b] - 2 * dpt[lca];
};
//cerr << cen << "\n";
//printv(all, cerr);
int v1 = cen, v2 = cen;
auto upd = [&](int v){
//cerr << "upd " << v << "\n";
if(dis(v1, v) > dis(v1, v2)) v2 = v;
else if(dis(v, v2) > dis(v1, v2)) v1 = v;
};
int p = 0;
vector<int> ans(n + 1);
for(int i = n; i >= 1; i--){
if(i % 2 == 1){
ans[i] = 1;
continue;
}
while(p < n && all[p].F >= i / 2){
upd(all[p].S);
p++;
}
//cerr << "ans " << i << "\n";
ans[i] = dis(v1, v2) + 1;
}
for(int i = 1; i <= n; i++) cout << ans[i] << "\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... |