結果

問題 No.2634 Tree Distance 3
ユーザー ponjuiceponjuice
提出日時 2024-02-16 23:08:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 669 ms / 3,000 ms
コード長 7,419 bytes
コンパイル時間 4,562 ms
コンパイル使用メモリ 276,784 KB
実行使用メモリ 47,580 KB
最終ジャッジ日時 2024-09-28 21:44:51
合計ジャッジ時間 31,660 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 342 ms
32,992 KB
testcase_01 AC 375 ms
32,988 KB
testcase_02 AC 352 ms
32,984 KB
testcase_03 AC 346 ms
32,992 KB
testcase_04 AC 338 ms
32,988 KB
testcase_05 AC 354 ms
32,988 KB
testcase_06 AC 374 ms
33,116 KB
testcase_07 AC 388 ms
33,116 KB
testcase_08 AC 301 ms
34,012 KB
testcase_09 AC 304 ms
33,892 KB
testcase_10 AC 304 ms
33,920 KB
testcase_11 AC 284 ms
33,980 KB
testcase_12 AC 278 ms
33,952 KB
testcase_13 AC 313 ms
34,080 KB
testcase_14 AC 292 ms
33,960 KB
testcase_15 AC 292 ms
33,908 KB
testcase_16 AC 368 ms
39,724 KB
testcase_17 AC 195 ms
23,912 KB
testcase_18 AC 263 ms
28,208 KB
testcase_19 AC 309 ms
35,628 KB
testcase_20 AC 71 ms
14,680 KB
testcase_21 AC 589 ms
42,584 KB
testcase_22 AC 550 ms
46,172 KB
testcase_23 AC 480 ms
47,580 KB
testcase_24 AC 540 ms
37,984 KB
testcase_25 AC 544 ms
36,060 KB
testcase_26 AC 521 ms
38,624 KB
testcase_27 AC 669 ms
36,060 KB
testcase_28 AC 594 ms
37,720 KB
testcase_29 AC 558 ms
36,828 KB
testcase_30 AC 563 ms
33,172 KB
testcase_31 AC 560 ms
33,176 KB
testcase_32 AC 497 ms
33,300 KB
testcase_33 AC 295 ms
24,644 KB
testcase_34 AC 44 ms
7,680 KB
testcase_35 AC 133 ms
16,048 KB
testcase_36 AC 72 ms
10,476 KB
testcase_37 AC 174 ms
18,152 KB
testcase_38 AC 3 ms
6,816 KB
testcase_39 AC 4 ms
6,820 KB
testcase_40 AC 3 ms
6,816 KB
testcase_41 AC 3 ms
6,816 KB
testcase_42 AC 3 ms
6,816 KB
testcase_43 AC 108 ms
13,016 KB
testcase_44 AC 67 ms
9,568 KB
testcase_45 AC 393 ms
28,688 KB
testcase_46 AC 217 ms
19,880 KB
testcase_47 AC 454 ms
31,564 KB
testcase_48 AC 475 ms
33,172 KB
testcase_49 AC 502 ms
33,300 KB
testcase_50 AC 486 ms
33,172 KB
testcase_51 AC 478 ms
33,176 KB
testcase_52 AC 469 ms
33,180 KB
testcase_53 AC 3 ms
6,816 KB
testcase_54 AC 3 ms
6,820 KB
testcase_55 AC 3 ms
6,816 KB
testcase_56 AC 3 ms
6,820 KB
testcase_57 AC 3 ms
6,816 KB
testcase_58 AC 1 ms
6,820 KB
testcase_59 AC 2 ms
6,816 KB
testcase_60 AC 387 ms
32,984 KB
testcase_61 AC 380 ms
33,012 KB
testcase_62 AC 367 ms
32,984 KB
testcase_63 AC 247 ms
32,804 KB
testcase_64 AC 148 ms
22,576 KB
testcase_65 AC 224 ms
31,012 KB
testcase_66 AC 57 ms
11,648 KB
testcase_67 AC 44 ms
10,240 KB
testcase_68 AC 264 ms
33,780 KB
testcase_69 AC 270 ms
33,904 KB
testcase_70 AC 258 ms
33,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;

//高速化 
struct ponjuice{ponjuice(){cin.tie(0);ios::sync_with_stdio(0);cout<<fixed<<setprecision(20);}}PonJuice;
//#define endl '\n' //インタラクティブ問題の時は消す

//型
using ll = long long;
using ld = long double;
using mint = modint998244353;//1000000007;
template<class T>using vc = vector<T>; template<class T>using vvc = vc<vc<T>>; template<class T>using vvvc = vvc<vc<T>>;
using vi = vc<int>;  using vvi = vvc<int>;  using vvvi = vvvc<int>;
using vl = vc<ll>;   using vvl = vvc<ll>;   using vvvl = vvvc<ll>;
using pi = pair<int, int>;  using pl = pair<ll, ll>;
using ull = unsigned ll;
template<class T>using priq = priority_queue<T>;
template<class T>using priqg = priority_queue<T, vc<T>, greater<T>>;

// for文
#define overload4(a, b, c, d, e, ...) e
#define rep1(n)             for(ll i = 0; i < n; i++)
#define rep2(i, n)          for(ll i = 0; i < n; i++)
#define rep3(i, a, b)       for(ll i = a; i < b; i++)
#define rep4(i, a, b, step) for(ll i = a; i < b; i+= step)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define per1(n)             for(ll i = n-1; i >= 0; i--)
#define per2(i, n)          for(ll i = n-1; i >= 0; i--)
#define per3(i, a, b)       for(ll i = b-1; i >= a; i--)
#define per4(i, a, b, step) for(ll i = b-1; i >= a; i-= step)
#define per(...) overload4(__VA_ARGS__, per4, per3, per2, per1)(__VA_ARGS__)
#define fore1(a)       for(auto&& i : a)	
#define fore2(i,a)     for(auto&& i : a)
#define fore3(x,y,a)   for(auto&& [x, y] : a)
#define fore4(x,y,z,a) for(auto&& [x, y, z] : a)
#define fore(...) overload4(__VA_ARGS__, fore4, fore3, fore2, fore1)(__VA_ARGS__)

//関数
#define mp make_pair
#define mt make_tuple
#define a first
#define b second
#define pb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define si(x) (ll)(x).size()
template<class S, class T>inline bool chmax(S& a, T b){return a < b && ( a = b , true);}
template<class S, class T>inline bool chmin(S& a, T b){return a > b && ( a = b , true);}
template<class T>void uniq(vc<T>&a){sort(all(a));a.erase(unique(all(a)),a.end());}
template<class T>vc<T> operator++(vc<T>&v,signed){auto res = v;fore(e,v)e++;return res;}
template<class T>vc<T> operator--(vc<T>&v,signed){auto res = v;fore(e,v)e--;return res;}
template<class T>vc<T> operator++(vc<T>&v){fore(e,v)e++;return v;}
template<class T>vc<T> operator--(vc<T>&v){fore(e,v)e--;return v;}

//入出力(operator)
template<int T>istream&operator>>(istream&is,static_modint<T>&a){ll v;is>>v;a=v;return is;}
istream&operator>>(istream&is,modint&a){ll v;cin>>v;a=v;return is;}
template<class S,class T>istream&operator>>(istream&is,pair<S,T>&a){is>>a.a>>a.b;return is;}
template<class T>istream&operator>>(istream&is,vc<T>&a){fore(e,a)is>>e;return is;}

template<int T>ostream&operator<<(ostream&os,static_modint<T>a){return os<<a.val();}
ostream&operator<<(ostream&os,modint a){return os<<a.val();}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>&a){return os<<a.a<<" "<<a.b;}
template<class T>ostream&operator<<(ostream&os,set<T>&a){fore(it,a){os<<it<<" ";}return os;}
template<class T>ostream&operator<<(ostream&os,multiset<T>&a){fore(it,a){os<<it<<" ";}return os;}
template<class S,class T>ostream&operator<<(ostream&os,map<S,T>&a){fore(x,y,a){os<<x<<" "<<y<<"\n";}return os;}
template<class T>ostream&operator<<(ostream&os,unordered_set<T>&a){fore(it,a){os<<it<<" ";}return os;}
template<class S,class T>ostream&operator<<(ostream&os,unordered_map<S,T>&a){fore(x,y,a){os<<x<<" "<<y<<"\n";}return os;}
template<class T>ostream&operator<<(ostream&os,vc<T>&a){fore(e,a)os<<e<<" ";return os;}
template<class T>ostream&operator<<(ostream&os,vvc<T>&a){fore(e,a)os<<e<<"\n";return os;}

//入出力(関数)
vi readvi(ll n){vi a(n);cin>>a;return a;}
vl readvl(ll n){vl a(n);cin>>a;return a;}
vvi readg(ll n,ll m,bool bidirected=true){vvi g(n);rep(i,m){ll a,b;cin>>a>>b;a--;b--;g[a].pb(b);if(bidirected)g[b].pb(a);}return g;}
vvc<pi>readgc(ll n,ll m,bool bidirected=true){vvc<pi> g(n);rep(i,m){ll a,b,c;cin>>a>>b>>c;a--;b--;g[a].pb(b,c);if(bidirected)g[b].pb(a,c);}return g;}
vvi readt(ll n,bool bidirected=true){return readg(n,n-1,bidirected);}
vvc<pi> readtc(ll n,bool bidirected=true){return readgc(n,n-1,bidirected);}

inline void yes(){cout << "Yes\n";}
inline void no(){cout << "No\n";}
inline void yesno(bool y = true){if(y)yes();else no();}
inline void print(){cout<<endl;}
template<class T>inline void print(T a){cout<<a<<endl;}
template<class T,class... Ts>inline void print(T a,Ts ...b){cout<<a;(cout<<...<<(cout<<' ',b));cout<<endl;}

//定数
constexpr ll mod = 998244353;
constexpr ll minf=-(1<<29);
constexpr ll inf=(1<<29);
constexpr ll MINF=-(1LL<<60);
constexpr ll INF=(1LL<<60);
constexpr ld EPS = 1e-8;
const ld PI = acosl(-1);
#define equals(a, b) (abs((a) - (b)) < EPS)
const int dx[4] ={-1, 0, 1, 0};
const int dy[4] ={ 0, 1, 0,-1};
const int dx8[8] ={-1,-1,-1, 0, 1, 1, 1, 0};
const int dy8[8] ={-1, 0, 1, 1, 1, 0,-1,-1};

void solve();
int main() {
	int t = 1;
    // cin >>t;
    while(t--)solve();
}

class LCA{
  private:
  int root;
  int k; // n<=2^kとなる最小のk
  vector<vector<int>> dp; // dp[i][j]:=要素jの2^i上の要素
  vector<int> depth;  // depth[i]:=rootに対する頂点iの深さ
  
  public:
  LCA(const vector<vector<int>>& _G, const int _root=0){
    int n=_G.size();
    root=_root;
    k=1;
    int nibeki=2;
    while(nibeki<n){
      nibeki<<=1;
      k++;
    }
    // 頂点iの親ノードを初期化
    dp = vector<vector<int>>(k+1, vector<int>(n, -1));
    depth.resize(n);
    function<void(int, int)> _dfs=[&](int v, int p){
      dp[0][v]=p;
      for(auto nv: _G[v]){
        if(nv==p) continue;
        depth[nv]=depth[v]+1;
        _dfs(nv, v); 
      }
    };
    _dfs(root, -1);
    // ダブリング
    for(int i=0; i<k; i++){
      for(int j=0; j<n; j++){
        if(dp[i][j]==-1) continue;
        dp[i+1][j]=dp[i][dp[i][j]];
      }
    }
  }
  
  /// get LCA
  int get(int u, int v){
    if(depth[u]<depth[v]) swap(u,v); // u側を深くする
    if(depth[u]!=depth[v]){
      long long d=depth[u]-depth[v];
      for(int i=0; i<k; i++) if((d>>i)&1) u=dp[i][u];
    }
    if(u==v) return u;
    
    for(int i=k; i>=0; i--){
      if(dp[i][u]!=dp[i][v]){
        u=dp[i][u], v=dp[i][v];
      }
    }
    return dp[0][u];
  }
  
  int get_distance(const int u, const int v){
    int lca=get(u,v);
    return depth[u]+depth[v]-2*depth[lca];
  }
};

void solve(){
    int n;
    cin >> n;
    vi a = readvi(n);
    vvi t = readt(n);
    LCA dist(t);
    vi io(n);
    iota(all(io), 0);
    sort(all(io), [&](int l,int r){
        return a[l] > a[r];
    });
    vi st;
    vi ans(n);
    int l = io[0],r = io[0];
    rep(k,n){
        int i = io[k];
        if(k == 0 || a[i] != a[io[k-1]]){
            rep(L, k, n){
                int j = io[L];
                if(a[i] != a[j]) break; 
                if(dist.get_distance(j,r) > dist.get_distance(l,r)){
                    if(dist.get_distance(j,l) < dist.get_distance(j,r)) l = j;
                    else r = j;
                }
                if(dist.get_distance(j,l) > dist.get_distance(l,r)) r = j;
            }
        }
        ans[i] = max(dist.get_distance(i,l),dist.get_distance(i,r));
    }

    print(ans);
}
0