結果
問題 | No.2634 Tree Distance 3 |
ユーザー | Misuki |
提出日時 | 2024-02-16 22:40:47 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 710 ms / 3,000 ms |
コード長 | 4,314 bytes |
コンパイル時間 | 3,080 ms |
コンパイル使用メモリ | 213,532 KB |
実行使用メモリ | 35,984 KB |
最終ジャッジ日時 | 2024-09-28 21:17:07 |
合計ジャッジ時間 | 27,027 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 267 ms
28,192 KB |
testcase_01 | AC | 255 ms
28,152 KB |
testcase_02 | AC | 253 ms
28,056 KB |
testcase_03 | AC | 257 ms
28,272 KB |
testcase_04 | AC | 261 ms
28,288 KB |
testcase_05 | AC | 256 ms
28,216 KB |
testcase_06 | AC | 262 ms
28,096 KB |
testcase_07 | AC | 257 ms
28,176 KB |
testcase_08 | AC | 193 ms
29,904 KB |
testcase_09 | AC | 193 ms
29,860 KB |
testcase_10 | AC | 202 ms
29,968 KB |
testcase_11 | AC | 200 ms
29,796 KB |
testcase_12 | AC | 199 ms
29,844 KB |
testcase_13 | AC | 203 ms
29,936 KB |
testcase_14 | AC | 199 ms
29,720 KB |
testcase_15 | AC | 191 ms
29,960 KB |
testcase_16 | AC | 449 ms
29,952 KB |
testcase_17 | AC | 249 ms
19,072 KB |
testcase_18 | AC | 341 ms
22,520 KB |
testcase_19 | AC | 368 ms
27,108 KB |
testcase_20 | AC | 95 ms
11,776 KB |
testcase_21 | AC | 710 ms
33,380 KB |
testcase_22 | AC | 661 ms
35,236 KB |
testcase_23 | AC | 650 ms
35,984 KB |
testcase_24 | AC | 578 ms
30,952 KB |
testcase_25 | AC | 599 ms
29,796 KB |
testcase_26 | AC | 602 ms
31,108 KB |
testcase_27 | AC | 681 ms
29,776 KB |
testcase_28 | AC | 650 ms
30,852 KB |
testcase_29 | AC | 524 ms
30,288 KB |
testcase_30 | AC | 432 ms
28,244 KB |
testcase_31 | AC | 414 ms
28,296 KB |
testcase_32 | AC | 407 ms
28,236 KB |
testcase_33 | AC | 275 ms
21,164 KB |
testcase_34 | AC | 42 ms
7,168 KB |
testcase_35 | AC | 130 ms
14,208 KB |
testcase_36 | AC | 77 ms
9,600 KB |
testcase_37 | AC | 169 ms
15,872 KB |
testcase_38 | AC | 4 ms
6,816 KB |
testcase_39 | AC | 4 ms
6,820 KB |
testcase_40 | AC | 3 ms
6,820 KB |
testcase_41 | AC | 3 ms
6,820 KB |
testcase_42 | AC | 3 ms
6,816 KB |
testcase_43 | AC | 95 ms
11,520 KB |
testcase_44 | AC | 60 ms
8,704 KB |
testcase_45 | AC | 325 ms
24,344 KB |
testcase_46 | AC | 192 ms
17,400 KB |
testcase_47 | AC | 403 ms
27,032 KB |
testcase_48 | AC | 393 ms
28,284 KB |
testcase_49 | AC | 371 ms
28,344 KB |
testcase_50 | AC | 415 ms
28,268 KB |
testcase_51 | AC | 415 ms
28,316 KB |
testcase_52 | AC | 380 ms
28,276 KB |
testcase_53 | AC | 3 ms
6,820 KB |
testcase_54 | AC | 3 ms
6,816 KB |
testcase_55 | AC | 3 ms
6,820 KB |
testcase_56 | AC | 3 ms
6,816 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 | 255 ms
28,192 KB |
testcase_61 | AC | 268 ms
28,100 KB |
testcase_62 | AC | 270 ms
28,232 KB |
testcase_63 | AC | 192 ms
28,856 KB |
testcase_64 | AC | 116 ms
20,356 KB |
testcase_65 | AC | 177 ms
27,496 KB |
testcase_66 | AC | 47 ms
10,752 KB |
testcase_67 | AC | 40 ms
9,728 KB |
testcase_68 | AC | 192 ms
29,640 KB |
testcase_69 | AC | 187 ms
29,788 KB |
testcase_70 | AC | 186 ms
29,824 KB |
ソースコード
#pragma GCC optimize("O2") #include <algorithm> #include <array> #include <bit> #include <bitset> #include <cassert> #include <cctype> #include <cfenv> #include <cfloat> #include <chrono> #include <cinttypes> #include <climits> #include <cmath> #include <compare> #include <complex> #include <concepts> #include <cstdarg> #include <cstddef> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <initializer_list> #include <iomanip> #include <ios> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <map> #include <memory> #include <new> #include <numbers> #include <numeric> #include <ostream> #include <queue> #include <random> #include <ranges> #include <set> #include <span> #include <sstream> #include <stack> #include <streambuf> #include <string> #include <tuple> #include <type_traits> #include <variant> //#define int ll #define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1) #define INT128_MIN (-INT128_MAX - 1) #define clock chrono::steady_clock::now().time_since_epoch().count() #ifdef DEBUG #define dbg(x) cout << (#x) << " = " << x << '\n' #else #define dbg(x) #endif namespace R = std::ranges; namespace V = std::views; using namespace std; using ll = long long; using ull = unsigned long long; using ldb = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; //#define double ldb template<class T> ostream& operator<<(ostream& os, const pair<T, T> pr) { return os << pr.first << ' ' << pr.second; } template<class T, size_t N> ostream& operator<<(ostream& os, const array<T, N> &arr) { for(const T &X : arr) os << X << ' '; return os; } template<class T> ostream& operator<<(ostream& os, const vector<T> &vec) { for(const T &X : vec) os << X << ' '; return os; } template<class T> ostream& operator<<(ostream& os, const set<T> &s) { for(const T &x : s) os << x << ' '; return os; } struct fastJump { vector<int> p, jp, dep; fastJump(vector<vector<int>> g, int root = 0) : p(ssize(g)), jp(ssize(g)), dep(ssize(g)) { auto dfs = [&](int v, auto self) -> void { if (dep[p[v]] + dep[jp[jp[p[v]]]] == 2 * dep[jp[p[v]]]) jp[v] = jp[jp[p[v]]]; else jp[v] = p[v]; for(int x : g[v]) { if (x == p[v]) continue; p[x] = v, dep[x] = dep[v] + 1; self(x, self); } }; p[root] = jp[root] = root; dfs(root, dfs); } int jump(int v, int k) { k = min(k, dep[v]); while(k) { if (int d = dep[v] - dep[jp[v]]; d <= k) v = jp[v], k -= d; else v = p[v], k -= 1; } return v; } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); u = jump(u, dep[u] - dep[v]); if (u == v) return u; while(p[u] != p[v]) { if (jp[u] != jp[v]) u = jp[u], v = jp[v]; else u = p[u], v = p[v]; } return p[u]; } int kth(int s, int t, int k) { int m = lca(s, t); if (dep[s] + dep[t] - 2 * dep[m] < k) return -1; else if (dep[s] - dep[m] >= k) return jump(s, k); else return jump(t, dep[s] + dep[t] - 2 * dep[m] - k); } int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } }; signed main() { ios::sync_with_stdio(false), cin.tie(NULL); int n; cin >> n; vector<int> a(n); for(int &x : a) cin >> x; vector<vector<int>> g(n); for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; u--, v--; g[u].emplace_back(v); g[v].emplace_back(u); } fastJump fj(g); vector<int> ord(n); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](int i, int j) { return a[i] > a[j]; }); int l = ord[0], r = ord[0], d = 0; vector<int> ans(n); for(int i = 1, p = 0; i <= n; i++) { if (i == n or a[ord[i]] != a[ord[i - 1]]) { for(int j = p; j < i; j++) { int x = fj.dis(ord[j], l); int y = fj.dis(ord[j], r); if (x >= y and x > d) r = ord[j], d = x; else if (x < y and y > d) l = ord[j], d = y; } for(int j = p; j < i; j++) ans[ord[j]] = max(fj.dis(l, ord[j]), fj.dis(r, ord[j])); p = i; } } cout << ans << '\n'; return 0; }