結果
問題 | No.2634 Tree Distance 3 |
ユーザー | umimel |
提出日時 | 2024-02-16 23:01:34 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 898 ms / 3,000 ms |
コード長 | 4,136 bytes |
コンパイル時間 | 3,078 ms |
コンパイル使用メモリ | 192,188 KB |
実行使用メモリ | 55,252 KB |
最終ジャッジ日時 | 2024-02-16 23:02:20 |
合計ジャッジ時間 | 35,628 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 486 ms
47,572 KB |
testcase_01 | AC | 526 ms
47,572 KB |
testcase_02 | AC | 480 ms
47,572 KB |
testcase_03 | AC | 495 ms
47,572 KB |
testcase_04 | AC | 465 ms
47,572 KB |
testcase_05 | AC | 439 ms
42,964 KB |
testcase_06 | AC | 491 ms
42,964 KB |
testcase_07 | AC | 447 ms
42,964 KB |
testcase_08 | AC | 426 ms
45,728 KB |
testcase_09 | AC | 387 ms
45,688 KB |
testcase_10 | AC | 382 ms
45,672 KB |
testcase_11 | AC | 407 ms
45,620 KB |
testcase_12 | AC | 381 ms
45,668 KB |
testcase_13 | AC | 332 ms
40,740 KB |
testcase_14 | AC | 309 ms
40,548 KB |
testcase_15 | AC | 327 ms
40,636 KB |
testcase_16 | AC | 482 ms
49,788 KB |
testcase_17 | AC | 269 ms
30,176 KB |
testcase_18 | AC | 424 ms
36,104 KB |
testcase_19 | AC | 423 ms
44,464 KB |
testcase_20 | AC | 104 ms
17,792 KB |
testcase_21 | AC | 777 ms
50,136 KB |
testcase_22 | AC | 718 ms
53,632 KB |
testcase_23 | AC | 735 ms
55,252 KB |
testcase_24 | AC | 721 ms
50,588 KB |
testcase_25 | AC | 739 ms
48,740 KB |
testcase_26 | AC | 796 ms
51,204 KB |
testcase_27 | AC | 898 ms
43,708 KB |
testcase_28 | AC | 804 ms
45,348 KB |
testcase_29 | AC | 773 ms
44,244 KB |
testcase_30 | AC | 645 ms
42,016 KB |
testcase_31 | AC | 700 ms
41,916 KB |
testcase_32 | AC | 686 ms
41,940 KB |
testcase_33 | AC | 383 ms
30,948 KB |
testcase_34 | AC | 52 ms
9,088 KB |
testcase_35 | AC | 168 ms
19,892 KB |
testcase_36 | AC | 89 ms
12,800 KB |
testcase_37 | AC | 214 ms
22,508 KB |
testcase_38 | AC | 3 ms
6,676 KB |
testcase_39 | AC | 4 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 | 166 ms
17,376 KB |
testcase_44 | AC | 90 ms
12,672 KB |
testcase_45 | AC | 515 ms
39,772 KB |
testcase_46 | AC | 294 ms
27,532 KB |
testcase_47 | AC | 628 ms
44,080 KB |
testcase_48 | AC | 670 ms
46,408 KB |
testcase_49 | AC | 627 ms
46,400 KB |
testcase_50 | AC | 681 ms
46,388 KB |
testcase_51 | AC | 646 ms
46,376 KB |
testcase_52 | AC | 655 ms
46,416 KB |
testcase_53 | AC | 4 ms
6,676 KB |
testcase_54 | AC | 3 ms
6,676 KB |
testcase_55 | AC | 3 ms
6,676 KB |
testcase_56 | AC | 3 ms
6,676 KB |
testcase_57 | AC | 3 ms
6,676 KB |
testcase_58 | AC | 2 ms
6,676 KB |
testcase_59 | AC | 1 ms
6,676 KB |
testcase_60 | AC | 492 ms
46,140 KB |
testcase_61 | AC | 498 ms
46,144 KB |
testcase_62 | AC | 498 ms
46,140 KB |
testcase_63 | AC | 389 ms
44,236 KB |
testcase_64 | AC | 238 ms
30,296 KB |
testcase_65 | AC | 330 ms
41,840 KB |
testcase_66 | AC | 81 ms
15,116 KB |
testcase_67 | AC | 66 ms
13,060 KB |
testcase_68 | AC | 288 ms
40,512 KB |
testcase_69 | AC | 316 ms
40,512 KB |
testcase_70 | AC | 295 ms
40,512 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template<typename T> struct edge{ int from, to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int to, T cost, int id) : from(-1), to(to), cost(cost), id(id){} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} }; template<typename T> struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template<typename T> using Edges = vector<edge<T>>; template<typename T> using weighted_graph = vector<Edges<T>>; template<typename T> using tree = vector<Edges<T>>; using unweighted_graph = vector<vector<int>>; template<typename T> using residual_graph = vector<vector<redge<T>>>; template<typename W> struct lowest_common_ancestor{ int root = 0; int n; int log_n = 1; vector<vector<int>> par; vector<int> depth; lowest_common_ancestor(tree<W> &T, int root=0) : root(root){ n = (int)T.size(); while((1 << log_n) < n) log_n++; par.resize(log_n, vector<int>(n, -1)); depth.resize(n, 0); init(T); } void init(tree<W> &T){ function<void(int, int)> dfs = [&](int v, int p){ par[0][v] = p; for(edge<W> e : T[v]) if(e.to!=p){ depth[e.to] = depth[v]+1; dfs(e.to, v); } }; dfs(root, -1); for(int k=0; k+1<log_n; k++){ for(int v=0; v<n; v++){ if(par[k][v]<0) par[k+1][v] = -1; else par[k+1][v] = par[k][par[k][v]]; } } } int query(int u, int v){ if(depth[u] < depth[v]) swap(u, v); for(int k=0; k<log_n; k++) if((depth[u]-depth[v]) >> k & 1) u = par[k][u]; if(u == v) return u; for(int k=log_n-1; k>=0; k--){ if(par[k][u] != par[k][v]){ u = par[k][u]; v = par[k][v]; } } return par[0][u]; } }; void solve(){ int n; cin >> n; vector<int> a(n); for(int i=0; i<n; i++) cin >> a[i]; { vector<int> c(n); for(int i=0; i<n; i++) c[i] = a[i]; sort(all(c)); c.erase(unique(all(c)), c.end()); for(int i=0; i<n; i++) a[i] = lower_bound(all(c), a[i])-c.begin(); } tree<int> T(n); for(int i=0; i<n-1; i++){ int u, v; cin >> u >> v; u--; v--; T[u].pb(edge<int>(v)); T[v].pb(edge<int>(u)); } lowest_common_ancestor<int> lca(T); vector<int> depth(n, 0); function<void(int, int)> dfs = [&](int v, int p){ for(edge<int> e : T[v]) if(e.to!=p){ depth[e.to] = depth[v]+1; dfs(e.to, v); } }; dfs(0, -1); function<int(int, int)> dist = [&](int u, int v){ return depth[u] + depth[v] -2*depth[lca.query(u, v)]; }; vector<vector<int>> b(n); for(int i=0; i<n; i++) b[a[i]].push_back(i); int s = -1, t = -1; int diam = 0; vector<int> ans(n, 0); for(int i=n-1; i>=0; i--){ //追加 for(int v : b[i]){ if(s==-1){ s = v; t = v; diam = 0; }else{ int s_dist = dist(s, v); int t_dist = dist(t, v); if(diam < max(s_dist, t_dist)){ if(s_dist>=t_dist){ t = v; diam = s_dist; }else{ s = v; diam = t_dist; } } } } for(int v : b[i]){ ans[v] = max(dist(s, v), dist(t, v)); } } for(int i=0; i<n; i++) cout << ans[i] << " "; cout << '\n'; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }