結果
問題 | No.2634 Tree Distance 3 |
ユーザー | Forested |
提出日時 | 2024-02-16 21:57:10 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,276 ms / 3,000 ms |
コード長 | 13,305 bytes |
コンパイル時間 | 2,261 ms |
コンパイル使用メモリ | 224,200 KB |
実行使用メモリ | 41,056 KB |
最終ジャッジ日時 | 2024-09-28 20:14:35 |
合計ジャッジ時間 | 34,877 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,262 ms
32,056 KB |
testcase_01 | AC | 1,250 ms
32,208 KB |
testcase_02 | AC | 1,260 ms
32,264 KB |
testcase_03 | AC | 1,271 ms
32,192 KB |
testcase_04 | AC | 1,276 ms
32,196 KB |
testcase_05 | AC | 741 ms
22,340 KB |
testcase_06 | AC | 754 ms
23,128 KB |
testcase_07 | AC | 738 ms
22,292 KB |
testcase_08 | AC | 474 ms
32,296 KB |
testcase_09 | AC | 459 ms
32,144 KB |
testcase_10 | AC | 475 ms
32,148 KB |
testcase_11 | AC | 476 ms
32,160 KB |
testcase_12 | AC | 412 ms
32,060 KB |
testcase_13 | AC | 246 ms
22,516 KB |
testcase_14 | AC | 230 ms
22,624 KB |
testcase_15 | AC | 229 ms
22,624 KB |
testcase_16 | AC | 259 ms
41,056 KB |
testcase_17 | AC | 151 ms
24,940 KB |
testcase_18 | AC | 198 ms
29,252 KB |
testcase_19 | AC | 219 ms
38,296 KB |
testcase_20 | AC | 65 ms
15,652 KB |
testcase_21 | AC | 208 ms
33,720 KB |
testcase_22 | AC | 204 ms
37,724 KB |
testcase_23 | AC | 208 ms
40,108 KB |
testcase_24 | AC | 374 ms
38,392 KB |
testcase_25 | AC | 370 ms
35,640 KB |
testcase_26 | AC | 373 ms
38,832 KB |
testcase_27 | AC | 203 ms
26,260 KB |
testcase_28 | AC | 223 ms
28,084 KB |
testcase_29 | AC | 209 ms
26,828 KB |
testcase_30 | AC | 395 ms
22,544 KB |
testcase_31 | AC | 398 ms
22,604 KB |
testcase_32 | AC | 388 ms
22,752 KB |
testcase_33 | AC | 108 ms
18,316 KB |
testcase_34 | AC | 21 ms
6,820 KB |
testcase_35 | AC | 61 ms
11,712 KB |
testcase_36 | AC | 35 ms
8,236 KB |
testcase_37 | AC | 73 ms
12,912 KB |
testcase_38 | AC | 2 ms
6,820 KB |
testcase_39 | AC | 3 ms
6,816 KB |
testcase_40 | AC | 2 ms
6,820 KB |
testcase_41 | AC | 2 ms
6,820 KB |
testcase_42 | AC | 3 ms
6,820 KB |
testcase_43 | AC | 175 ms
12,776 KB |
testcase_44 | AC | 105 ms
9,756 KB |
testcase_45 | AC | 581 ms
27,804 KB |
testcase_46 | AC | 342 ms
19,740 KB |
testcase_47 | AC | 692 ms
31,076 KB |
testcase_48 | AC | 718 ms
32,272 KB |
testcase_49 | AC | 715 ms
32,216 KB |
testcase_50 | AC | 744 ms
32,776 KB |
testcase_51 | AC | 741 ms
32,468 KB |
testcase_52 | AC | 730 ms
32,236 KB |
testcase_53 | AC | 3 ms
6,816 KB |
testcase_54 | AC | 4 ms
6,820 KB |
testcase_55 | AC | 3 ms
6,820 KB |
testcase_56 | AC | 2 ms
6,820 KB |
testcase_57 | AC | 3 ms
6,816 KB |
testcase_58 | AC | 2 ms
6,816 KB |
testcase_59 | AC | 2 ms
6,816 KB |
testcase_60 | AC | 1,030 ms
32,216 KB |
testcase_61 | AC | 949 ms
32,204 KB |
testcase_62 | AC | 970 ms
32,108 KB |
testcase_63 | AC | 320 ms
31,284 KB |
testcase_64 | AC | 206 ms
22,116 KB |
testcase_65 | AC | 303 ms
29,628 KB |
testcase_66 | AC | 77 ms
11,596 KB |
testcase_67 | AC | 63 ms
10,276 KB |
testcase_68 | AC | 183 ms
22,588 KB |
testcase_69 | AC | 178 ms
22,488 KB |
testcase_70 | AC | 183 ms
22,488 KB |
ソースコード
#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(); } }