結果
問題 | No.2634 Tree Distance 3 |
ユーザー |
|
提出日時 | 2024-02-16 23:01:34 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 881 ms / 3,000 ms |
コード長 | 4,136 bytes |
コンパイル時間 | 2,366 ms |
コンパイル使用メモリ | 193,148 KB |
実行使用メモリ | 55,116 KB |
最終ジャッジ日時 | 2024-09-28 21:36:59 |
合計ジャッジ時間 | 34,683 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 69 |
ソースコード
#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 secondmt19937_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();}