結果
問題 | No.2634 Tree Distance 3 |
ユーザー | ゆにぽけ |
提出日時 | 2024-02-24 09:26:27 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2,700 ms / 3,000 ms |
コード長 | 6,887 bytes |
コンパイル時間 | 2,088 ms |
コンパイル使用メモリ | 149,052 KB |
実行使用メモリ | 53,520 KB |
最終ジャッジ日時 | 2024-09-29 09:56:00 |
合計ジャッジ時間 | 95,638 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2,543 ms
41,264 KB |
testcase_01 | AC | 2,634 ms
41,260 KB |
testcase_02 | AC | 2,589 ms
41,264 KB |
testcase_03 | AC | 2,700 ms
41,264 KB |
testcase_04 | AC | 2,583 ms
41,264 KB |
testcase_05 | AC | 1,602 ms
38,852 KB |
testcase_06 | AC | 1,555 ms
38,724 KB |
testcase_07 | AC | 1,617 ms
38,848 KB |
testcase_08 | AC | 976 ms
41,232 KB |
testcase_09 | AC | 979 ms
41,156 KB |
testcase_10 | AC | 926 ms
41,048 KB |
testcase_11 | AC | 823 ms
40,760 KB |
testcase_12 | AC | 797 ms
40,828 KB |
testcase_13 | AC | 558 ms
38,536 KB |
testcase_14 | AC | 525 ms
38,276 KB |
testcase_15 | AC | 526 ms
38,380 KB |
testcase_16 | AC | 2,055 ms
46,464 KB |
testcase_17 | AC | 1,046 ms
29,560 KB |
testcase_18 | AC | 1,407 ms
35,472 KB |
testcase_19 | AC | 1,732 ms
40,284 KB |
testcase_20 | AC | 404 ms
16,128 KB |
testcase_21 | AC | 1,622 ms
53,392 KB |
testcase_22 | AC | 1,623 ms
53,516 KB |
testcase_23 | AC | 1,574 ms
53,520 KB |
testcase_24 | AC | 2,476 ms
45,860 KB |
testcase_25 | AC | 2,491 ms
45,944 KB |
testcase_26 | AC | 2,576 ms
47,604 KB |
testcase_27 | AC | 1,458 ms
43,328 KB |
testcase_28 | AC | 1,493 ms
43,148 KB |
testcase_29 | AC | 1,491 ms
41,976 KB |
testcase_30 | AC | 1,469 ms
38,780 KB |
testcase_31 | AC | 1,504 ms
38,772 KB |
testcase_32 | AC | 1,512 ms
38,760 KB |
testcase_33 | AC | 634 ms
28,920 KB |
testcase_34 | AC | 90 ms
8,960 KB |
testcase_35 | AC | 339 ms
18,816 KB |
testcase_36 | AC | 157 ms
12,416 KB |
testcase_37 | AC | 399 ms
21,376 KB |
testcase_38 | AC | 5 ms
5,248 KB |
testcase_39 | AC | 5 ms
5,248 KB |
testcase_40 | AC | 4 ms
5,248 KB |
testcase_41 | AC | 3 ms
5,248 KB |
testcase_42 | AC | 4 ms
5,248 KB |
testcase_43 | AC | 517 ms
16,072 KB |
testcase_44 | AC | 303 ms
11,904 KB |
testcase_45 | AC | 1,902 ms
36,096 KB |
testcase_46 | AC | 1,078 ms
24,924 KB |
testcase_47 | AC | 2,208 ms
39,684 KB |
testcase_48 | AC | 2,271 ms
41,548 KB |
testcase_49 | AC | 2,293 ms
41,408 KB |
testcase_50 | AC | 2,506 ms
41,536 KB |
testcase_51 | AC | 2,338 ms
41,276 KB |
testcase_52 | AC | 2,316 ms
41,504 KB |
testcase_53 | AC | 5 ms
5,248 KB |
testcase_54 | AC | 5 ms
5,248 KB |
testcase_55 | AC | 5 ms
5,248 KB |
testcase_56 | AC | 5 ms
5,248 KB |
testcase_57 | AC | 6 ms
5,248 KB |
testcase_58 | AC | 2 ms
5,248 KB |
testcase_59 | AC | 2 ms
5,248 KB |
testcase_60 | AC | 2,095 ms
40,992 KB |
testcase_61 | AC | 2,007 ms
40,608 KB |
testcase_62 | AC | 2,113 ms
40,984 KB |
testcase_63 | AC | 457 ms
37,952 KB |
testcase_64 | AC | 285 ms
26,352 KB |
testcase_65 | AC | 422 ms
35,976 KB |
testcase_66 | AC | 93 ms
13,440 KB |
testcase_67 | AC | 77 ms
11,904 KB |
testcase_68 | AC | 343 ms
36,984 KB |
testcase_69 | AC | 325 ms
37,132 KB |
testcase_70 | AC | 334 ms
36,876 KB |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <array> #include <iterator> #include <string> #include <cctype> #include <cstring> #include <cstdlib> #include <cassert> #include <cmath> #include <ctime> #include <iomanip> #include <numeric> #include <stack> #include <queue> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <bitset> #include <random> #include <utility> #include <functional> using namespace std; struct CentroidDecomposition { private: int N; vector<vector<int>> G; vector<int> seen,subtree; void calc_subtree(int u,int p) { subtree[u] = 1; for(int v : G[u]) { if(v == p || seen[v]) { continue; } calc_subtree(v,u); subtree[u] += subtree[v]; } } int find_centroid(int u,int p,const int total_size) { for(int v : G[u]) { if(v == p || seen[v]) { continue; } if(subtree[v] * 2 > total_size) { return find_centroid(v,u,total_size); } } return u; } public: CentroidDecomposition(int N) : N(N),G(N),seen(N),subtree(N) {} void add_edge(int u,int v) { assert(0 <= u && u < N && 0 <= v && v < N); G[u].push_back(v); G[v].push_back(u); } int build(vector<vector<int>> &H,int u) { static bool first_call = true; if(first_call) { first_call = false; H.resize(N); } calc_subtree(u,-1); int total_size = subtree[u]; int centroid = find_centroid(u,-1,total_size); seen[centroid] = 1; for(int v : G[centroid]) { if(seen[v]) { continue; } int next_centroid = build(H,v); H[centroid].push_back(next_centroid); } return centroid; } }; template<class T,T (*op)(T,T),T (*e)()> struct segtree { private: int size,N; vector<T> node; public: segtree(int N = 0) : segtree(vector<T>(N,e())) {} segtree(const vector<T> &V) { N = (int)V.size(); size = 1; while(size < N) { size <<= 1; } node.assign(2 * size,e()); for(int i = 0;i < N;i++) { node[i + size] = V[i]; } for(int i = size - 1;i >= 1;i--) { node[i] = op(node[2 * i],node[2 * i + 1]); } } void replace(int pos,T x) { func_update(pos,x,[](T u,T v){return u = v;}); } void add(int pos,T x) { func_update(pos,x,[](T u,T v){return u + v;}); } void func_update(int pos,T x) { func_update(pos,x,op); } void func_update(int pos,T x,T (*func)(T,T)) { assert(0 <= pos && pos < N); pos += size; node[pos] = func(node[pos],x); while(pos >> 1) { pos >>= 1; node[pos] = op(node[2 * pos],node[2 * pos + 1]); } } T operator[](int pos) { assert(0 <= pos && pos < N); return node[pos + size]; } int get_log() { int res = 0; while(1 << res < size) { res++; } assert((1 << res) == size); return res; } vector<vector<T>> tree_list() { vector<vector<T>> res; int log = get_log(); res.reserve(log + 1); for(int i = 0;i <= log;i++) { int L = 1 << log,R = L << 1; vector<T> cur(node.begin() + L,node.begin() + R); res.push_back(cur); } return res; } //suppose that class T has ostream &operator<< void print(int k) { assert(0 <= k && k <= get_log()); int L = 1 << k,R = L << 1; for(int i = L;i < R;i++) { cerr << node[i] << (i + 1 == R ? "\n":" "); } } void print() { int log = get_log(); for(int k = 0;k <= log;k++) { int L = 1 << k,R = L << 1; const string unit((1 << (log - k)) - 1,' '); for(int i = L;i < R;i++) { if(i == L) { cerr << unit; } cerr << node[i]; if(i + 1 < R) { cerr << unit << unit << ' '; } else { cerr << unit; } } cerr << "\n"; } } T prod(int l,int r) { assert(0 <= l && l <= r && r <= N); T L = e(),R = e(); l += size,r += size; while(l < r) { if(l & 1) { L = op(L,node[l++]); } if(r & 1) { R = op(node[--r],R); } l >>= 1,r >>= 1; } return op(L,R); } T prod() { return node[1]; } template<class F> int max_right(int l,F f) { assert(0 <= l && l <= N); assert(f(e())); if(l == N) { return N; } l += size; T val = e(); do { while(!(l & 1)) { l >>= 1; } if(!f(op(val,node[l]))) { while(l < size) { l <<= 1; if(f(op(val,node[l]))) { val = op(val,node[l++]); } } return l - size; } val = op(val,node[l++]); } while((l & -l) != l); return N; } template<class F> int min_left(int r,F f) { assert(0 <= r && r <= N); assert(f(e())); if(r == 0) { return 0; } r += size; T val = e(); do { r--; while(r > 1 && (r & 1)) { r >>= 1; } if(!f(op(node[r],val))) { while(r < size) { r <<= 1; r++; if(f(op(node[r],val))) { val = op(node[r--],val); } } return r + 1 - size; } val = op(node[r],val); } while((r & -r) != r); return 0; } }; int op(int a,int b) { return max(a,b); } int e() { return -1; } void Main() { int N; cin >> N; vector<int> A(N); for(int i = 0;i < N;i++) { cin >> A[i]; } vector<int> comp = A; sort(comp.begin(),comp.end()); comp.erase(unique(comp.begin(),comp.end()),comp.end()); for(int i = 0;i < N;i++) { A[i] = lower_bound(comp.begin(),comp.end(),A[i]) - comp.begin(); } CentroidDecomposition cd(N); vector<vector<int>> G(N); for(int i = 1;i < N;i++) { int u,v; cin >> u >> v; u--; v--; cd.add_edge(u,v); G[u].push_back(v); G[v].push_back(u); } vector<vector<int>> H; int r = cd.build(H,0); vector<int> seen(N); segtree<int,op,e> seg(comp.size()); vector<int> ans(N); vector<pair<int,int>> tmp; auto dfs1 = [&](auto self,int u,int p,int d) -> void { int prod = seg.prod(A[u],comp.size()); if(prod != -1) { ans[u] = max(ans[u],prod + d); } tmp.push_back(make_pair(A[u],d)); for(int v : G[u]) { if(v == p || seen[v]) { continue; } self(self,v,u,d + 1); } }; auto dfs2 = [&](auto self,int u,int p) -> void { if(seg[A[u]] != e()) { seg.replace(A[u],e()); } for(int v : G[u]) { if(v == p || seen[v]) { continue; } self(self,v,u); } }; auto divide = [&](auto self,int u) -> void { seg.func_update(A[u],0); for(int t = 0;t < 2;t++) { for(int v : G[u]) { if(seen[v]) { continue; } dfs1(dfs1,v,u,1); for(const auto &[a,d] : tmp) { seg.func_update(a,d); } tmp.clear(); } if(t) { ans[u] = max(ans[u],seg.prod(A[u],comp.size())); } for(int v : G[u]) { if(seen[v]) { continue; } dfs2(dfs2,v,u); } reverse(G[u].begin(),G[u].end()); } seen[u] = 1; seg.replace(A[u],e()); for(int v : H[u]) { self(self,v); } }; divide(divide,r); for(int i = 0;i < N;i++) { cout << ans[i] << (i + 1 == N ? "\n":" "); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt = 1; /* cin >> tt; */ while(tt--) Main(); }