結果

問題 No.2634 Tree Distance 3
ユーザー ForestedForested
提出日時 2024-02-16 21:57:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,914 ms / 3,000 ms
コード長 13,305 bytes
コンパイル時間 3,673 ms
コンパイル使用メモリ 225,116 KB
実行使用メモリ 41,824 KB
最終ジャッジ日時 2024-02-16 21:58:03
合計ジャッジ時間 50,682 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,914 ms
32,392 KB
testcase_01 AC 1,867 ms
32,392 KB
testcase_02 AC 1,849 ms
32,392 KB
testcase_03 AC 1,843 ms
32,392 KB
testcase_04 AC 1,799 ms
32,392 KB
testcase_05 AC 985 ms
22,752 KB
testcase_06 AC 1,003 ms
22,752 KB
testcase_07 AC 1,034 ms
22,752 KB
testcase_08 AC 702 ms
32,392 KB
testcase_09 AC 730 ms
32,392 KB
testcase_10 AC 731 ms
32,392 KB
testcase_11 AC 738 ms
32,392 KB
testcase_12 AC 679 ms
32,392 KB
testcase_13 AC 319 ms
22,752 KB
testcase_14 AC 304 ms
22,752 KB
testcase_15 AC 296 ms
22,752 KB
testcase_16 AC 393 ms
41,824 KB
testcase_17 AC 204 ms
25,096 KB
testcase_18 AC 291 ms
29,452 KB
testcase_19 AC 323 ms
37,916 KB
testcase_20 AC 78 ms
15,652 KB
testcase_21 AC 282 ms
34,016 KB
testcase_22 AC 276 ms
37,984 KB
testcase_23 AC 271 ms
39,776 KB
testcase_24 AC 560 ms
38,192 KB
testcase_25 AC 561 ms
36,040 KB
testcase_26 AC 569 ms
38,912 KB
testcase_27 AC 282 ms
26,336 KB
testcase_28 AC 295 ms
28,256 KB
testcase_29 AC 304 ms
27,104 KB
testcase_30 AC 514 ms
22,880 KB
testcase_31 AC 538 ms
22,880 KB
testcase_32 AC 524 ms
22,880 KB
testcase_33 AC 140 ms
18,140 KB
testcase_34 AC 25 ms
6,676 KB
testcase_35 AC 75 ms
11,836 KB
testcase_36 AC 40 ms
8,236 KB
testcase_37 AC 91 ms
13,044 KB
testcase_38 AC 3 ms
6,676 KB
testcase_39 AC 3 ms
6,676 KB
testcase_40 AC 3 ms
6,676 KB
testcase_41 AC 3 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
testcase_43 AC 225 ms
12,904 KB
testcase_44 AC 127 ms
9,876 KB
testcase_45 AC 856 ms
28,408 KB
testcase_46 AC 487 ms
20,008 KB
testcase_47 AC 1,004 ms
31,016 KB
testcase_48 AC 1,097 ms
32,520 KB
testcase_49 AC 1,039 ms
32,512 KB
testcase_50 AC 1,101 ms
32,500 KB
testcase_51 AC 1,072 ms
32,488 KB
testcase_52 AC 1,088 ms
32,516 KB
testcase_53 AC 3 ms
6,676 KB
testcase_54 AC 4 ms
6,676 KB
testcase_55 AC 3 ms
6,676 KB
testcase_56 AC 4 ms
6,676 KB
testcase_57 AC 4 ms
6,676 KB
testcase_58 AC 2 ms
6,676 KB
testcase_59 AC 2 ms
6,676 KB
testcase_60 AC 1,459 ms
32,392 KB
testcase_61 AC 1,444 ms
32,392 KB
testcase_62 AC 1,470 ms
32,392 KB
testcase_63 AC 476 ms
31,560 KB
testcase_64 AC 278 ms
22,236 KB
testcase_65 AC 463 ms
30,080 KB
testcase_66 AC 94 ms
11,720 KB
testcase_67 AC 77 ms
10,272 KB
testcase_68 AC 234 ms
22,752 KB
testcase_69 AC 229 ms
22,752 KB
testcase_70 AC 269 ms
22,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef LOCAL
#define FAST_IO
#endif

// ============
#include <bits/stdc++.h>
#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, m, n) for (i32 i = (i32)(n)-1; i >= (i32)(m); --i)
#define PER(...) OVERRIDE(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32)(x.size())
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
using i32 = signed int;
using i64 = signed long long;
using f64 = double;
using f80 = long double;
using pi = pair<i32, i32>;
using pl = pair<i64, i64>;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<V<V<T>>>;
template <typename T>
using VVVV = V<V<V<V<T>>>>;
template <typename T>
using PQR = priority_queue<T, V<T>, greater<T>>;
template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
i32 lob(const V<T> &arr, const T &v) {
    return (i32)(lower_bound(ALL(arr), v) - arr.begin());
}
template <typename T>
i32 upb(const V<T> &arr, const T &v) {
    return (i32)(upper_bound(ALL(arr), v) - arr.begin());
}
template <typename T>
V<i32> argsort(const V<T> &arr) {
    V<i32> ret(arr.size());
    iota(ALL(ret), 0);
    sort(ALL(ret), [&](i32 i, i32 j) -> bool {
        if (arr[i] == arr[j]) {
            return i < j;
        } else {
            return arr[i] < arr[j];
        }
    });
    return ret;
}
#ifdef INT128
using u128 = __uint128_t;
using i128 = __int128_t;
#endif
[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
    SetUpIO() {
#ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
#endif
        cout << fixed << setprecision(15);
    }
} set_up_io;
void scan(char &x) { cin >> x; }
void scan(u32 &x) { cin >> x; }
void scan(u64 &x) { cin >> x; }
void scan(i32 &x) { cin >> x; }
void scan(i64 &x) { cin >> x; }
void scan(string &x) { cin >> x; }
template <typename T>
void scan(V<T> &x) {
    for (T &ele : x) {
        scan(ele);
    }
}
void read() {}
template <typename Head, typename... Tail>
void read(Head &head, Tail &...tail) {
    scan(head);
    read(tail...);
}
#define CHAR(...)     \
    char __VA_ARGS__; \
    read(__VA_ARGS__);
#define U32(...)     \
    u32 __VA_ARGS__; \
    read(__VA_ARGS__);
#define U64(...)     \
    u64 __VA_ARGS__; \
    read(__VA_ARGS__);
#define I32(...)     \
    i32 __VA_ARGS__; \
    read(__VA_ARGS__);
#define I64(...)     \
    i64 __VA_ARGS__; \
    read(__VA_ARGS__);
#define STR(...)        \
    string __VA_ARGS__; \
    read(__VA_ARGS__);
#define VEC(type, name, size) \
    V<type> name(size);       \
    read(name);
#define VVEC(type, name, size1, size2)    \
    VV<type> name(size1, V<type>(size2)); \
    read(name);
// ============

#ifdef DEBUGF
#else
#define DBG(...) (void)0
#endif

// ============
#include <algorithm>
#include <utility>
// ============
#include <iostream>
#include <cassert>
#include <vector>
template <typename T>
struct Edge {
    using W = T;
    int from, to, id;
    W weight;
    Edge<T> rev() const {
        return Edge<T>{to, from, id, weight};
    }
};
template <typename T>
void debug(const Edge<T> &e) {
    std::cerr << e.from << " -> " << e.to << " id = " << e.id << std::cerr << " weight = ";
    debug(e.weight);
}
template <typename T = int, bool DIR = false>
class Graph {
public:
    using E = Edge<T>;
    using W = T;
    static constexpr bool DIRECTED = DIR;
    struct Adjacency {
        using Iter = typename std::vector<E>::iterator;
        Iter be, en;
        Iter begin() const { return be; }
        Iter end() const { return en; }
        int size() const { return (int)std::distance(be, en); }
        E &operator[](int idx) const { return be[idx]; }
    };
    struct ConstAdjacency {
        using Iter = typename std::vector<E>::const_iterator;
        Iter be, en;
        Iter begin() const { return be; }
        Iter end() const { return en; }
        int size() const { return (int)std::distance(be, en); }
        const E &operator[](int idx) const { return be[idx]; }
    };

private:
    int n, m;
    std::vector<E> edges, csr;
    std::vector<int> sep;
    bool built;

public:
    Graph(int n) : n(n), m(0), built(false) {}
    int v() const { return n; }
    int e() const { return m; }
    int add_vertex() {
        return n++;
    }
    void add_edge(int from, int to, W weight = 1) {
        assert(0 <= from && from < n && 0 <= to && to < n);
        edges.emplace_back(E{from, to, m++, weight});
    }
    void build() {
        sep.assign(n + 1, 0);
        csr.resize(DIRECTED ? m : 2 * m);
        for (const E &e : edges) {
            ++sep[e.from + 1];
            if (!DIRECTED) {
                ++sep[e.to + 1];
            }
        }
        for (int i = 0; i < n; ++i) {
            sep[i + 1] += sep[i];
        }
        std::vector<int> c = sep;
        for (const E &e : edges) {
            csr[c[e.from]++] = e;
            if (!DIRECTED) {
                csr[c[e.to]++] = e.rev();
            }
        }
        built = true;
    }
    Adjacency operator[](int v) {
        assert(built && 0 <= v && v < n);
        return Adjacency{csr.begin() + sep[v], csr.begin() + sep[v + 1]};
    }
    ConstAdjacency operator[](int v) const {
        assert(built && 0 <= v && v < n);
        return ConstAdjacency{csr.begin() + sep[v], csr.begin() + sep[v + 1]};
    }
};
// ============
class HeavyLightDecomposition {
public:
    std::vector<int> siz, par, hea, in, out, dep, rev;

private:
    template <typename T, bool DIR>
    void dfs1(Graph<T, DIR> &g, int v) {
        if (g[v].size() >= 1 && g[v][0].to == par[v]) {
            std::swap(g[v][0], g[v][g[v].size() - 1]);
        }
        for (Edge<T> &e : g[v]) {
            if (e.to != par[v]) {
                par[e.to] = v;
                dfs1(g, e.to);
                siz[v] += siz[e.to];
                if (siz[e.to] > siz[g[v][0].to]) {
                    std::swap(g[v][0], e);
                }
            }
        }
    }
    template <typename T, bool DIR>
    void dfs2(const Graph<T, DIR> &g, int v, int &t) {
        in[v] = t;
        rev[t++] = v;
        for (const Edge<T> &e : g[v]) {
            if (e.to == par[v]) {
                continue;
            }
            if (e.to == g[v][0].to) {
                hea[e.to] = hea[v];
            } else {
                hea[e.to] = e.to;
            }
            dep[e.to] = dep[v] + 1;
            dfs2(g, e.to, t);
        }
        out[v] = t;
    }

public:
    template <typename T, bool DIR>
    HeavyLightDecomposition(Graph<T, DIR> &g, int root = 0)
        : siz(g.v(), 1),
          par(g.v(), root),
          hea(g.v(), root),
          in(g.v(), 0),
          out(g.v(), 0),
          dep(g.v(), 0),
          rev(g.v(), 0) {
        assert(0 <= root && root < g.v());
        dfs1(g, root);
        int t = 0;
        dfs2(g, root, t);
    }
    // par^k
    int la(int v, int k) const {
        assert(0 <= v && v < (int)dep.size());
        assert(k >= 0);
        if (k > dep[v]) {
            return -1;
        }
        while (true) {
            int u = hea[v];
            if (in[u] + k <= in[v]) {
                return rev[in[v] - k];
            }
            k -= in[v] - in[u] + 1;
            v = par[u];
        }
        return 0;
    }
    int lca(int u, int v) const {
        assert(0 <= u && u < (int)dep.size());
        assert(0 <= v && v < (int)dep.size());
        while (u != v) {
            if (in[u] > in[v]) {
                std::swap(u, v);
            }
            if (hea[u] == hea[v]) {
                v = u;
            } else {
                v = par[hea[v]];
            }
        }
        return u;
    }
    int dist(int u, int v) const {
        assert(0 <= u && u < (int)dep.size());
        assert(0 <= v && v < (int)dep.size());
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }
    int jump(int u, int v, int k) const {
        assert(0 <= u && u < (int)dep.size());
        assert(0 <= v && v < (int)dep.size());
        assert(k >= 0);
        int l = lca(u, v);
        int dis = dep[u] + dep[v] - 2 * dep[l];
        if (k > dis) {
            return -1;
        }
        if (k <= dep[u] - dep[l]) {
            return la(u, k);
        } else {
            return la(v, dis - k);
        }
    }
    int meet(int u, int v, int w) const {
        return lca(u, v) ^ lca(v, w) ^ lca(w, u);
    }
    std::vector<std::pair<int, int>> path(int u, int v, bool edge) const {
        assert(0 <= u && u < (int)dep.size());
        assert(0 <= v && v < (int)dep.size());
        std::vector<std::pair<int, int>> fromu, fromv;
        bool rev = false;
        while (true) {
            if (u == v && edge) {
                break;
            }
            if (in[u] > in[v]) {
                std::swap(u, v);
                std::swap(fromu, fromv);
                rev ^= true;
            }
            if (hea[u] == hea[v]) {
                fromv.emplace_back(in[v], in[u] + (int)edge);
                v = u;
                break;
            } else {
                fromv.emplace_back(in[v], in[hea[v]]);
                v = par[hea[v]];
            }
        }
        if (rev) {
            std::swap(fromu, fromv);
        }
        std::reverse(fromv.begin(), fromv.end());
        fromu.reserve(fromv.size());
        for (auto [x, y] : fromv) {
            fromu.emplace_back(y, x);
        }
        return fromu;
    }
};
// ============
// ============

#include <algorithm>
#include <vector>

template <typename T>
class CoordinateCompression {
    std::vector<T> data;

    int size_sum() { return 0; }

    template <typename... Tail>
    int size_sum(const std::vector<T> &head, const Tail &...tail) {
        return (int)head.size() + size_sum(tail...);
    }

    void push() {}

    template <typename... Tail>
    void push(const std::vector<T> &head, const Tail &...tail) {
        for (const T &ele : head) {
            data.emplace_back(ele);
        }
        push(tail...);
    }

    void compress() {}

    template <typename... Tail>
    void compress(std::vector<T> &head, Tail &...tail) {
        for (T &ele : head) {
            ele = (T)(std::lower_bound(data.begin(), data.end(), ele) -
                      data.begin());
        }
        compress(tail...);
    }

public:
    template <typename... V>
    CoordinateCompression(V &...v) {
        data.reserve(size_sum(v...));
        push(v...);
        std::sort(data.begin(), data.end());
        data.erase(std::unique(data.begin(), data.end()), data.end());
        compress(v...);
    }

    const T &operator[](const T &ele) const { return data[ele]; }

    int size() const { return data.size(); }

    bool contains(const T &ele) const {
        auto it = std::lower_bound(data.begin(), data.end(), ele);
        return it != data.end() && *it == ele;
    }

    T cc(const T &ele) const {
        return (T)(std::lower_bound(data.begin(), data.end(), ele) -
                   data.begin());
    }
};
// ============

void solve() {
    I32(n);
    VEC(i32, a, n);
    Graph<> g(n);
    REP(i, n - 1) {
        I32(u, v);
        --u;
        --v;
        g.add_edge(u, v);
    }
    g.build();
    HeavyLightDecomposition hld(g);
    CoordinateCompression<i32> _cc(a);
    VV<i32> idx(_cc.size());
    REP(i, n) { idx[a[i]].push_back(i); }
    V<i32> ans(n, 0);
    auto rec = [&](auto rec, i32 l, i32 r) -> void {
        if (r - l == 1) {
            i32 u = idx[l][0], v = u, d = 0;
            for (i32 ver : idx[l]) {
                if (chmax(d, hld.dist(u, ver))) {
                    v = ver;
                }
            }
            d = 0;
            u = v;
            for (i32 ver : idx[l]) {
                if (chmax(d, hld.dist(v, ver))) {
                    u = ver;
                }
            }
            for (i32 ver : idx[l]) {
                chmax(ans[ver], hld.dist(ver, u));
                chmax(ans[ver], hld.dist(ver, v));
            }
            return;
        }
        i32 mid = (l + r) / 2;
        i32 u = idx[mid][0], v = u, d = 0;
        REP(i, mid, r) for (i32 ver : idx[i]) {
            if (chmax(d, hld.dist(u, ver))) {
                v = ver;
            }
        }
        d = 0;
        u = v;
        REP(i, mid, r) for (i32 ver : idx[i]) {
            if (chmax(d, hld.dist(v, ver))) {
                u = ver;
            }
        }
        REP(i, l, mid) for (i32 ver : idx[i]) {
            chmax(ans[ver], hld.dist(ver, u));
            chmax(ans[ver], hld.dist(ver, v));
        }
        rec(rec, l, mid);
        rec(rec, mid, r);
    };
    rec(rec, 0, LEN(idx));
    REP(i, n) {
        cout << ans[i] << " \n"[i + 1 == n];
    }
}

int main() {
    i32 t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}
0