結果
問題 | No.2634 Tree Distance 3 |
ユーザー | Misuki |
提出日時 | 2024-02-16 22:28:54 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,272 bytes |
コンパイル時間 | 2,754 ms |
コンパイル使用メモリ | 214,432 KB |
実行使用メモリ | 36,140 KB |
最終ジャッジ日時 | 2024-09-28 21:03:33 |
合計ジャッジ時間 | 26,135 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 258 ms
28,192 KB |
testcase_01 | AC | 251 ms
28,204 KB |
testcase_02 | AC | 247 ms
28,116 KB |
testcase_03 | AC | 252 ms
28,060 KB |
testcase_04 | AC | 253 ms
28,232 KB |
testcase_05 | AC | 253 ms
28,204 KB |
testcase_06 | AC | 257 ms
28,100 KB |
testcase_07 | AC | 257 ms
28,096 KB |
testcase_08 | AC | 197 ms
29,952 KB |
testcase_09 | WA | - |
testcase_10 | AC | 200 ms
29,872 KB |
testcase_11 | WA | - |
testcase_12 | AC | 198 ms
29,892 KB |
testcase_13 | AC | 200 ms
29,856 KB |
testcase_14 | AC | 198 ms
29,860 KB |
testcase_15 | AC | 195 ms
29,816 KB |
testcase_16 | AC | 445 ms
29,960 KB |
testcase_17 | WA | - |
testcase_18 | AC | 342 ms
22,396 KB |
testcase_19 | AC | 363 ms
27,084 KB |
testcase_20 | AC | 94 ms
11,648 KB |
testcase_21 | AC | 731 ms
33,436 KB |
testcase_22 | AC | 659 ms
35,328 KB |
testcase_23 | AC | 635 ms
36,140 KB |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 597 ms
31,104 KB |
testcase_27 | AC | 692 ms
29,720 KB |
testcase_28 | AC | 629 ms
30,672 KB |
testcase_29 | WA | - |
testcase_30 | AC | 418 ms
28,160 KB |
testcase_31 | AC | 418 ms
28,320 KB |
testcase_32 | AC | 406 ms
28,188 KB |
testcase_33 | AC | 283 ms
21,048 KB |
testcase_34 | AC | 42 ms
7,168 KB |
testcase_35 | AC | 135 ms
14,208 KB |
testcase_36 | AC | 76 ms
9,600 KB |
testcase_37 | AC | 169 ms
15,892 KB |
testcase_38 | AC | 4 ms
6,820 KB |
testcase_39 | AC | 4 ms
6,816 KB |
testcase_40 | AC | 3 ms
6,816 KB |
testcase_41 | AC | 3 ms
6,816 KB |
testcase_42 | AC | 3 ms
6,820 KB |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | AC | 329 ms
24,372 KB |
testcase_46 | AC | 193 ms
17,396 KB |
testcase_47 | AC | 382 ms
26,888 KB |
testcase_48 | WA | - |
testcase_49 | AC | 369 ms
28,216 KB |
testcase_50 | AC | 417 ms
28,412 KB |
testcase_51 | AC | 416 ms
28,296 KB |
testcase_52 | WA | - |
testcase_53 | AC | 3 ms
6,816 KB |
testcase_54 | AC | 3 ms
6,820 KB |
testcase_55 | WA | - |
testcase_56 | AC | 3 ms
6,820 KB |
testcase_57 | WA | - |
testcase_58 | AC | 2 ms
6,816 KB |
testcase_59 | AC | 2 ms
6,820 KB |
testcase_60 | AC | 258 ms
28,108 KB |
testcase_61 | AC | 262 ms
28,068 KB |
testcase_62 | AC | 270 ms
28,224 KB |
testcase_63 | AC | 188 ms
29,040 KB |
testcase_64 | AC | 126 ms
20,568 KB |
testcase_65 | AC | 177 ms
27,472 KB |
testcase_66 | AC | 50 ms
10,880 KB |
testcase_67 | AC | 40 ms
9,472 KB |
testcase_68 | AC | 185 ms
29,764 KB |
testcase_69 | AC | 185 ms
29,828 KB |
testcase_70 | AC | 194 ms
29,736 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++) { if (int x = fj.dis(ord[j], l); x > d) r = ord[j], d = x; if (int x = fj.dis(ord[j], r); x > d) l = ord[j], d = x; } 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; }