結果

問題 No.2634 Tree Distance 3
ユーザー noya2noya2
提出日時 2024-02-11 18:40:11
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,963 ms / 3,000 ms
コード長 20,647 bytes
コンパイル時間 4,302 ms
コンパイル使用メモリ 289,608 KB
実行使用メモリ 63,572 KB
最終ジャッジ日時 2024-02-16 18:04:01
合計ジャッジ時間 81,646 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,884 ms
21,332 KB
testcase_01 AC 1,898 ms
21,332 KB
testcase_02 AC 1,905 ms
21,332 KB
testcase_03 AC 1,890 ms
21,332 KB
testcase_04 AC 1,887 ms
21,332 KB
testcase_05 AC 1,798 ms
21,332 KB
testcase_06 AC 1,815 ms
21,332 KB
testcase_07 AC 1,807 ms
21,332 KB
testcase_08 AC 699 ms
21,332 KB
testcase_09 AC 669 ms
21,332 KB
testcase_10 AC 708 ms
21,332 KB
testcase_11 AC 633 ms
21,332 KB
testcase_12 AC 620 ms
21,332 KB
testcase_13 AC 679 ms
21,332 KB
testcase_14 AC 614 ms
21,332 KB
testcase_15 AC 670 ms
21,332 KB
testcase_16 AC 1,517 ms
52,416 KB
testcase_17 AC 730 ms
33,024 KB
testcase_18 AC 962 ms
40,412 KB
testcase_19 AC 1,139 ms
46,688 KB
testcase_20 AC 262 ms
18,148 KB
testcase_21 AC 1,912 ms
63,572 KB
testcase_22 AC 1,962 ms
63,572 KB
testcase_23 AC 1,963 ms
63,572 KB
testcase_24 AC 1,736 ms
33,364 KB
testcase_25 AC 1,783 ms
31,828 KB
testcase_26 AC 1,932 ms
38,228 KB
testcase_27 AC 1,810 ms
33,364 KB
testcase_28 AC 1,761 ms
33,364 KB
testcase_29 AC 1,681 ms
29,780 KB
testcase_30 AC 1,618 ms
21,588 KB
testcase_31 AC 1,634 ms
21,716 KB
testcase_32 AC 1,617 ms
21,716 KB
testcase_33 AC 777 ms
17,292 KB
testcase_34 AC 112 ms
6,676 KB
testcase_35 AC 403 ms
11,636 KB
testcase_36 AC 202 ms
8,084 KB
testcase_37 AC 476 ms
12,616 KB
testcase_38 AC 6 ms
6,676 KB
testcase_39 AC 6 ms
6,676 KB
testcase_40 AC 4 ms
6,676 KB
testcase_41 AC 5 ms
6,676 KB
testcase_42 AC 4 ms
6,676 KB
testcase_43 AC 337 ms
9,980 KB
testcase_44 AC 205 ms
7,556 KB
testcase_45 AC 1,326 ms
19,328 KB
testcase_46 AC 752 ms
13,544 KB
testcase_47 AC 1,555 ms
20,684 KB
testcase_48 AC 1,583 ms
21,588 KB
testcase_49 AC 1,611 ms
21,588 KB
testcase_50 AC 1,704 ms
21,588 KB
testcase_51 AC 1,629 ms
21,588 KB
testcase_52 AC 1,651 ms
21,588 KB
testcase_53 AC 5 ms
6,676 KB
testcase_54 AC 5 ms
6,676 KB
testcase_55 AC 4 ms
6,676 KB
testcase_56 AC 5 ms
6,676 KB
testcase_57 AC 5 ms
6,676 KB
testcase_58 AC 2 ms
6,676 KB
testcase_59 AC 2 ms
6,676 KB
testcase_60 AC 1,410 ms
21,332 KB
testcase_61 AC 1,366 ms
21,332 KB
testcase_62 AC 1,415 ms
21,332 KB
testcase_63 AC 446 ms
20,804 KB
testcase_64 AC 261 ms
14,572 KB
testcase_65 AC 412 ms
20,024 KB
testcase_66 AC 89 ms
8,376 KB
testcase_67 AC 71 ms
7,756 KB
testcase_68 AC 453 ms
21,332 KB
testcase_69 AC 445 ms
21,332 KB
testcase_70 AC 449 ms
21,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"
using namespace std;

#include<bits/stdc++.h>
#line 1 "/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp"
namespace noya2 {

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p){
    os << p.first << " " << p.second;
    return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p){
    is >> p.first >> p.second;
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v){
    for (auto &x : v) is >> x;
    return is;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u){
    cin >> t;
    in(u...);
}

void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u){
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}

template<typename T>
void out(const vector<vector<T>> &vv){
    int s = (int)vv.size();
    for (int i = 0; i < s; i++) out(vv[i]);
}

struct IoSetup {
    IoSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(7);
    }
} iosetup_noya2;

} // namespace noya2
#line 1 "/Users/noya2/Desktop/Noya2_library/template/const.hpp"
namespace noya2{

const int iinf = 1'000'000'007;
const long long linf = 2'000'000'000'000'000'000LL;
const long long mod998 =  998244353;
const long long mod107 = 1000000007;
const long double pi = 3.14159265358979323;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

void yes(){ cout << "Yes\n"; }
void no(){ cout << "No\n"; }
void YES(){ cout << "YES\n"; }
void NO(){ cout << "NO\n"; }
void yn(bool t){ t ? yes() : no(); }
void YN(bool t){ t ? YES() : NO(); }

} // namespace noya2
#line 1 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp"
namespace noya2{

unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){
    if (a == 0 || b == 0) return a + b;
    int n = __builtin_ctzll(a); a >>= n;
    int m = __builtin_ctzll(b); b >>= m;
    while (a != b) {
        int mm = __builtin_ctzll(a - b);
        bool f = a > b;
        unsigned long long c = f ? a : b;
        b = f ? b : a;
        a = (c - b) >> mm;
    }
    return a << min(n, m);
}

template<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }

long long sqrt_fast(long long n) {
    if (n <= 0) return 0;
    long long x = sqrt(n);
    while ((x + 1) * (x + 1) <= n) x++;
    while (x * x > n) x--;
    return x;
}

template<typename T> T floor_div(const T n, const T d) {
    assert(d != 0);
    return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);
}

template<typename T> T ceil_div(const T n, const T d) {
    assert(d != 0);
    return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);
}

template<typename T> void uniq(vector<T> &v){
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}

template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }

} // namespace noya2
#line 8 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"

#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)
#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;

namespace noya2{

/* ~ (. _________ . /) */

}

using namespace noya2;


#line 2 "c.cpp"

struct csr_for_cd {
    csr_for_cd (int n_ = 0) : n(n_), m((n_-1)*2), removed(n_,false) {
        es.reserve(m);
        start.reserve(m);
        if (m == 0) build();
    }
    int add(int u, int v){
        int eid = start.size();
        es.emplace_back(u);
        start.emplace_back(v);
        if (eid+1 == m) build();
        return eid;
    }
    void build(){
        if (m == -2) return ;
        m = start.size();
        std::vector<int> nes(m);
        std::vector<int> nstart(n+2,0);
        for (int i = 0; i < m; i++) nstart[start[i]+2]++;
        for (int i = 1; i < n; i++) nstart[i+2] += nstart[i+1];
        for (int i = 0; i < m; i++) nes[nstart[start[i]+1]++] = es[i];
        swap(es,nes);
        swap(start,nstart);
        stop = vector<int>(start.begin()+1,start.end());
        m = -2;
    }
    const auto operator[](int idx){
        assert(m == -2);
        for (int i = start[idx]; i < stop[idx]; i++){
            if (removed[es[i]]) swap(es[i],es[--stop[idx]]);
        }
        return std::ranges::subrange(es.begin()+start[idx],es.begin()+stop[idx]);
    }
    void remove(int v){
        removed[v] = true;
        stop[v] = start[v];
    }
  private:
    int n, m;
    std::vector<bool> removed;
    std::vector<int> es;
    std::vector<int> start, stop;
};

struct centroid_decomposition {
    centroid_decomposition () {}
    centroid_decomposition (int n_) : n(n_), g(n_), sub(n_,0) {
        que.push(0);
        sub[0] = n;
    }
    void add_edge(int u, int v){
        g.add(u,v);
        g.add(v,u);
    }
    int find_centroid(){
        assert(!que.empty());
        int from = que.front(); que.pop();
        int sz = sub[from];
        int centroid = -1;
        auto dfs = [&](auto sfs, int v, int f) -> void {
            sub[v] = 1;
            bool is_centroid = (centroid == -1);
            for (int u : g[v]){
                if (u == f) continue;
                sfs(sfs,u,v);
                if (centroid != -1) return ;
                if (sub[u] > sz/2) is_centroid = false;
                sub[v] += sub[u];
            }
            if (sz - sub[v] > sz/2) is_centroid = false;
            if (is_centroid){
                centroid = v;
                if (f != -1){
                    sub[f] = sz - sub[v];
                }
            }
        };
        dfs(dfs,from,-1);
        assert(centroid != -1);
        return centroid;
    }
    void remove_centroid(int v){
        for (int u : g[v]){
            que.push(u);
        }
        g.remove(v);
    }
    const auto operator[](int idx) { return g[idx]; }
  private:
    int n;
    csr_for_cd g;
    queue<int> que;
    vector<int> sub;
};

#line 2 "/Users/noya2/Desktop/Noya2_library/data_structure/segment_tree.hpp"

namespace noya2{

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = 0;
        size = 1;
        while (size < _n) size <<= 1, log++;

        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

} // namespace noya2
#line 99 "c.cpp"

int op(int a, int b){
    return max(a,b);
}
int e(){
    return -iinf;
}

#line 2 "/Users/noya2/Desktop/Noya2_library/tree/heavy_light_decomposition.hpp"

#line 4 "/Users/noya2/Desktop/Noya2_library/tree/heavy_light_decomposition.hpp"

namespace noya2 {

struct hldTree {
    hldTree (int n_ = 0, int root_ = 0) : n(n_), root(root_), inner_edge_id(0){
        down.resize(n);
        tour.resize(n);
        if (n == 1) build();
    }
    void add_edge(int u, int v){
        down[inner_edge_id] = u;
        tour[inner_edge_id] = v;
        if (++inner_edge_id == n-1) build();
    }
    void input(int indexed = 1){
        for (int i = 0; i < n-1; i++){
            int u, v; cin >> u >> v;
            u -= indexed, v -= indexed;
            add_edge(u,v);
        }
    }
    void input_parents(int indexed = 1){
        for (int i = 0; i < n-1; i++){
            int p; cin >> p;
            p -= indexed;
            add_edge(p,i+1);
        }
    }
    int degree(int v){
        assert(0 <= v && v < n);
        return start[v+1] - start[v];
    }
    int parent(int v){
        assert(0 <= v && v < n);
        if (v == root) return -1;
        return es[start[v]];
    }
    int subtree_size(int v){
        assert(0 <= v && v < n);
        return sub[v];
    }
    int depth(int v){
        assert(0 <= v && v < n);
        return dep[v];
    }
    int la(int v, int d){
        assert(0 <= v && v < n);
        while (v != -1){
            int u = nxt[v];
            if (down[v] - d >= down[u]){
                v = tour[down[v] - d];
                break;
            }
            d -= down[v] - down[u] + 1;
            v = parent(u);
        }
        return v;
    }
    int lca(int u, int v){
        assert(0 <= v && v < n && 0 <= u && u < n);
        while (nxt[u] != nxt[v]){
            if (down[u] < down[v]) std::swap(u,v);
            u = es[start[nxt[u]]];
        }
        return dep[u] < dep[v] ? u : v;
    }
    int jump(int from, int to, int d){
        int l = lca(from,to);
        if (d <= dep[from] - dep[l]){
            return la(from,d);
        }
        d -= dep[from] - dep[l];
        if (d <= dep[to] - dep[l]){
            return la(to,dep[to]-dep[l]-d);
        }
        return -1;
    }
    int dist(int u, int v){ return dep[lca(u,v)]*(-2) + dep[u] + dep[v]; }
    bool is_in_subtree(int r, int v){ return down[r] < down[v] && up[v] <= up[r]; }
    bool is_in_path(int lv, int mv, int rv){ return dist(lv,mv) + dist(mv,rv) == dist(lv,rv); }
    vector<int> path(int from, int to){
        int l = lca(from,to);
        const int sizf = dep[from]-dep[l], sizt = dep[to]-dep[l];
        vector<int> pf = {from}, pt;
        pf.reserve(sizf+1); pt.reserve(sizt);
        for (int i = 0; i < sizf; i++){
            from = parent(from);
            pf.push_back(from);
        }
        for (int i = 0; i < sizt; i++){
            pt.push_back(to);
            to = parent(to);
        }
        pf.insert(pf.end(),pt.rbegin(),pt.rend());
        return pf;
    }
    // dist, v1, v2
    tuple<int,int,int> diameter(){
        int v1 = max_element(dep.begin(),dep.end()) - dep.begin();
        vector<int> dist_from_v1(n,numeric_limits<int>::max());
        queue<int> que;
        que.push(v1);
        dist_from_v1[v1] = 0;
        while (!que.empty()){
            int p = que.front(); que.pop();
            for (int i = start[p]; i < start[p+1]; i++){
                if (dist_from_v1[es[i]] > dist_from_v1[p]+1){
                    dist_from_v1[es[i]] = dist_from_v1[p]+1;
                    que.push(es[i]);
                }
            }
        }
        int v2 = max_element(dist_from_v1.begin(),dist_from_v1.end()) - dist_from_v1.begin();
        return make_tuple(dist_from_v1[v2],v1,v2);
    }
    template<typename F>
    void path_query(int u, int v, bool vertex, const F &f){ // f is function takes (left, right) as argument, range = [left,right).
        int l = lca(u,v);
        for (auto &p : ascend(u,l)){
            int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1)
            s > t ? f(t,s) : f(s,t);
        }
        if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point 
        for (auto &p : descend(l,v)){
            int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1)
            s > t ? f(t,s) : f(s,t);
        }
    }
    template<typename F>
    void path_noncommutative_query(int u, int v, bool vertex, const F &f){ // op(l,r) != op(r,l), so prod[u->...->v] != prod[v->...->u]
        int l = lca(u,v);
        for (auto &p : ascend(u,l)){
            int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1)
            f(s,t); // le > ri ok
        }
        if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point 
        for (auto &p : descend(l,v)){
            int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1)
            f(s,t); // le > ri ok
        }
    }
    template<typename F>
    void subtree_query(int v, bool vertex, const F &f){
        f(down[v] + (vertex ? 0 : 1), up[v]);
    }
    const auto operator[](int idx){ return std::ranges::subrange(es.begin()+start[idx],es.begin()+start[idx+1]); }
    const auto operator()(int idx){ return std::ranges::subrange(es.begin()+start_skip_parent(idx),es.begin()+start[idx+1]); }
    int set_id_v(int v) const {
        return down[v];
    }
    int set_id_e(int u, int v) const {
        return (dep[u] < dep[v] ? down[v] : down[u]);
    }
    int vertex_id(int i){
        return tour[i];
    }
    int subtree_l(int v) const {
        return down[v];
    }
    int subtree_r(int v) const {
        return up[v];
    }
  private:
    void build(){
        es.resize((n-1)*2);
        start.resize(n+2,0);
        for (int i = 0; i < n-1; i++){
            start[down[i]+2]++;
            start[tour[i]+2]++;
        }
        for (int i = 1; i <= n; i++){
            start[i+1] += start[i];
        }
        for (int i = 0; i < n-1; i++){
            es[start[down[i]+1]++] = tour[i];
            es[start[tour[i]+1]++] = down[i];
        }
        init_bfs();
        init_dfs();
    }
    void init_bfs(){
        dep.resize(n,numeric_limits<int>::max());
        up.resize(n);
        int l = 0, r = 0;
        auto push = [&](int x){
            up[r++] = x;
        };
        auto pop_front = [&](){
            return up[l++];
        };
        dep[root] = 0;
        push(root);
        while (l < r){
            int p = pop_front();
            for (int i = start[p]; i < start[p+1]; i++){
                auto q = es[i];
                if (dep[q] > dep[p]+1){
                    dep[q] = dep[p]+1;
                    push(q);
                }
                else {
                    swap(es[start[p]],es[i]);
                }
            }
        }
        sub.resize(n,1);
        for (int v : up | std::views::reverse){
            const int stv = start_skip_parent(v);
            for (int i = stv; i < start[v+1]; i++){
                sub[v] += sub[es[i]];
                if (sub[es[stv]] < sub[es[i]]) swap(es[stv],es[i]);
            }
        }
    }
    void init_dfs(){
        nxt.resize(n);
        nxt[root] = root;
        int inner_clock = 0;
        auto dfs = [&](auto sfs, int v) -> void {
            down[v] = inner_clock++;
            tour[down[v]] = v;
            int stv = start_skip_parent(v);
            if (stv < start[v+1]){
                nxt[es[stv]] = nxt[v];
                sfs(sfs,es[stv]);
                for (int i = stv+1; i < start[v+1]; i++){
                    nxt[es[i]] = es[i];
                    sfs(sfs,es[i]);
                }
            }
            up[v] = inner_clock;
        };
        dfs(dfs,root);
    }
    vector<pair<int,int>> ascend(int u, int v){ // [u,v), depth[u] > depth[v]
        vector<pair<int,int>> res;
        while (nxt[u] != nxt[v]){
            res.emplace_back(down[u],down[nxt[u]]); // [s1,t1], [s2,t2], ...
            u = es[start[nxt[u]]]; // parent of nxt[u]
        }
        if (u != v) res.emplace_back(down[u],down[v]+1); // [s,t). v is not in the range (down[] is ordered opposite direction of depth)
        return res;
    }
    vector<pair<int,int>> descend(int u, int v){ // (u,v], depth[u] < depth[v]
        if (u == v) return {};
        if (nxt[u] == nxt[v]){
            return {pair<int,int>(down[u]+1,down[v])}; // (s,t]. u is not in the range
        }
        vector<pair<int,int>> res = descend(u,es[start[nxt[v]]]); // descend(u, parent of nxt[v])
        res.emplace_back(down[nxt[v]],down[v]); // [s1,t1], [s2,t2], ...
        return res;
    }
    int start_skip_parent(int v) const { return start[v]+int(v != root); }
    int n, root, inner_edge_id;
    vector<int> es, start, dep, sub, down, up, tour, nxt;
};

} // namespace noya2
#line 108 "c.cpp"

void solve(){
    int n; in(n);
    vector<int> a(n); in(a);
    centroid_decomposition g(n);
    rep(i,n-1){
        int u, v; in(u,v); u--, v--;
        g.add_edge(u,v);
    }
    vector<int> ans(n,-1);
    vector<int> ids(n);
    rep(t_,n){
        int ctr = g.find_centroid();
        vector<int> vs;
        auto dfs = [&](auto sfs, int v, int f) -> void {
            vs.emplace_back(v);
            for (int u : g[v]){
                if (u == f) continue;
                sfs(sfs,u,v);
            }
        };
        dfs(dfs,ctr,-1);
        int sz = vs.size();
        rep(i,sz) ids[vs[i]] = i;
        hldTree hld(sz);
        auto init = [&](auto sfs, int v, int f) -> void {
            for (int u : g[v]){
                if (u == f) continue;
                hld.add_edge(ids[u],ids[v]);
                sfs(sfs,u,v);
            }
        };
        init(init,ctr,-1);
        vector<int> ord = vs;
        sort(all(ord),[&](int u, int v){
            return a[u] > a[v];
        });
        segtree<int,op,e> seg(sz);
        for (int l = 0, r = 0; l < sz; l = r){
            while (r < sz && a[ord[r]] == a[ord[l]]){
                seg.set(hld.set_id_v(ids[ord[r]]),hld.depth(ids[ord[r]]));
                r++;
            }
            repp(i,l,r){
                int iv = ids[ord[i]];
                if (hld.depth(iv) == 0){
                    chmax(ans[ord[i]],seg.all_prod());
                    continue;
                }
                int nxt = hld.jump(ids[ctr],iv,1);
                chmax(ans[ord[i]],op(seg.prod(0,hld.subtree_l(nxt)),seg.prod(hld.subtree_r(nxt),sz))+hld.depth(iv));
            }
        }
        g.remove_centroid(ctr);
    }
    out(ans);
}

int main(){
    int t = 1; //in(t);
    while (t--) { solve(); }
}
0