結果

問題 No.2634 Tree Distance 3
ユーザー i_am_noobi_am_noob
提出日時 2024-02-16 22:44:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 954 ms / 3,000 ms
コード長 6,549 bytes
コンパイル時間 5,373 ms
コンパイル使用メモリ 271,980 KB
実行使用メモリ 53,184 KB
最終ジャッジ日時 2024-02-16 22:46:01
合計ジャッジ時間 36,863 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 428 ms
41,028 KB
testcase_01 AC 413 ms
41,028 KB
testcase_02 AC 412 ms
41,028 KB
testcase_03 AC 430 ms
41,028 KB
testcase_04 AC 405 ms
41,028 KB
testcase_05 AC 408 ms
41,028 KB
testcase_06 AC 445 ms
41,028 KB
testcase_07 AC 384 ms
41,028 KB
testcase_08 AC 343 ms
41,912 KB
testcase_09 AC 354 ms
41,976 KB
testcase_10 AC 377 ms
41,948 KB
testcase_11 AC 349 ms
41,892 KB
testcase_12 AC 344 ms
41,860 KB
testcase_13 AC 369 ms
41,988 KB
testcase_14 AC 356 ms
41,860 KB
testcase_15 AC 324 ms
41,944 KB
testcase_16 AC 717 ms
44,044 KB
testcase_17 AC 361 ms
27,320 KB
testcase_18 AC 486 ms
32,692 KB
testcase_19 AC 595 ms
39,512 KB
testcase_20 AC 131 ms
16,384 KB
testcase_21 AC 954 ms
48,996 KB
testcase_22 AC 850 ms
51,904 KB
testcase_23 AC 911 ms
53,184 KB
testcase_24 AC 836 ms
45,168 KB
testcase_25 AC 767 ms
43,632 KB
testcase_26 AC 862 ms
45,684 KB
testcase_27 AC 752 ms
43,596 KB
testcase_28 AC 775 ms
44,964 KB
testcase_29 AC 766 ms
44,148 KB
testcase_30 AC 486 ms
41,136 KB
testcase_31 AC 517 ms
41,168 KB
testcase_32 AC 474 ms
41,152 KB
testcase_33 AC 290 ms
30,424 KB
testcase_34 AC 47 ms
9,344 KB
testcase_35 AC 153 ms
20,024 KB
testcase_36 AC 83 ms
13,184 KB
testcase_37 AC 186 ms
22,652 KB
testcase_38 AC 4 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 3 ms
6,676 KB
testcase_43 AC 131 ms
15,868 KB
testcase_44 AC 84 ms
11,904 KB
testcase_45 AC 432 ms
35,348 KB
testcase_46 AC 261 ms
24,968 KB
testcase_47 AC 446 ms
39,088 KB
testcase_48 AC 485 ms
41,128 KB
testcase_49 AC 489 ms
41,160 KB
testcase_50 AC 519 ms
41,152 KB
testcase_51 AC 499 ms
41,124 KB
testcase_52 AC 529 ms
41,168 KB
testcase_53 AC 3 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 2 ms
6,676 KB
testcase_60 AC 413 ms
41,028 KB
testcase_61 AC 419 ms
41,028 KB
testcase_62 AC 417 ms
41,028 KB
testcase_63 AC 352 ms
40,568 KB
testcase_64 AC 208 ms
28,500 KB
testcase_65 AC 310 ms
38,524 KB
testcase_66 AC 73 ms
14,592 KB
testcase_67 AC 58 ms
12,672 KB
testcase_68 AC 324 ms
41,816 KB
testcase_69 AC 340 ms
41,816 KB
testcase_70 AC 338 ms
41,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())

using ll=long long;
using u32=unsigned int;
using u64=unsigned long long;
using i128=__int128;
using u128=unsigned __int128;
using f128=__float128;

using pii=pair<int,int>;
using pll=pair<ll,ll>;

template<typename T> using vc=vector<T>;
template<typename T> using vvc=vc<vc<T>>;
template<typename T> using vvvc=vc<vvc<T>>;

using vi=vc<int>;
using vll=vc<ll>;
using vvi=vc<vi>;
using vvll=vc<vll>;

#define vv(type,name,n,...) \
    vector<vector<type>> name(n,vector<type>(__VA_ARGS__))
#define vvv(type,name,n,m,...) \
    vector<vector<vector<type>>> name(n,vector<vector<type>>(m,vector<type>(__VA_ARGS__)))

template<typename T> using min_heap=priority_queue<T,vector<T>,greater<T>>;
template<typename T> using max_heap=priority_queue<T>;

// https://trap.jp/post/1224/
#define rep1(n) for(ll i=0; i<(ll)(n); ++i)
#define rep2(i,n) for(ll i=0; i<(ll)(n); ++i)
#define rep3(i,a,b) for(ll i=(ll)(a); i<(ll)(b); ++i)
#define rep4(i,a,b,c) for(ll i=(ll)(a); i<(ll)(b); i+=(c))
#define cut4(a,b,c,d,e,...) e
#define rep(...) cut4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define per1(n) for(ll i=((ll)n)-1; i>=0; --i)
#define per2(i,n) for(ll i=((ll)n)-1; i>=0; --i)
#define per3(i,a,b) for(ll i=((ll)a)-1; i>=(ll)(b); --i)
#define per4(i,a,b,c) for(ll i=((ll)a)-1; i>=(ll)(b); i-=(c))
#define per(...) cut4(__VA_ARGS__,per4,per3,per2,per1)(__VA_ARGS__)
#define rep_subset(i,s) for(ll i=(s); i>=0; i=(i==0?-1:(i-1)&(s)))

template<typename T, typename S> constexpr T ifloor(const T a, const S b){return a/b-(a%b&&(a^b)<0);}
template<typename T, typename S> constexpr T iceil(const T a, const S b){return ifloor(a+b-1,b);}

template<typename T>
void sort_unique(vector<T> &vec){
    sort(vec.begin(),vec.end());
    vec.resize(unique(vec.begin(),vec.end())-vec.begin());
}

template<typename T, typename S> constexpr bool chmin(T &a, const S b){if(a>b) return a=b,true; return false;}
template<typename T, typename S> constexpr bool chmax(T &a, const S b){if(a<b) return a=b,true; return false;}

template<typename T, typename S> istream& operator >> (istream& i, pair<T,S> &p){return i >> p.first >> p.second;}
template<typename T, typename S> ostream& operator << (ostream& o, const pair<T,S> &p){return o << p.first << ' ' << p.second;}

#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif

template<typename T> void print(vector<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(set<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(unordered_set<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(T && x) {cout << x << "\n";}
template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);}

template<typename T> istream& operator >> (istream& i, vector<T> &vec){for(auto &x: vec) i >> x; return i;}

vvi read_graph(int n, int m, int base=1){
    vvi adj(n);
    for(int i=0,u,v; i<m; ++i){
        cin >> u >> v,u-=base,v-=base;
        adj[u].pb(v),adj[v].pb(u);
    }
    return adj;
}

vvi read_tree(int n, int base=1){return read_graph(n,n-1,base);}

template<typename T, typename S> pair<T,S> operator + (const pair<T,S> &a, const pair<T,S> &b){return {a.first+b.first,a.second+b.second};}

template<typename T> constexpr T inf=0;
template<> constexpr int inf<int> = 0x3f3f3f3f;
template<> constexpr ll inf<ll> = 0x3f3f3f3f3f3f3f3f;

template<typename T> vector<T> operator += (vector<T> &a, int val){for(auto &i: a) i+=val; return a;}

template<typename T> T isqrt(const T &x){T y=sqrt(x+2); while(y*y>x) y--; return y;}

#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())

#include<atcoder/all>
using namespace atcoder;

using mint=modint998244353;
//using mint=modint1000000007;

struct Tree{
    int n;
    vector<vector<int>> adj,anc;
    vector<int> par,tl,tr,dep,ord;
    Tree(int _n=0){
        n=_n;
        adj.resize(n);
    }
    void add_edge(int u, int v){
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    void init(){
        par.resize(n),tl.resize(n),tr.resize(n),dep.resize(n),anc.resize(n);
        int cur=-1,m=0;
        while((1<<m)<=n) m++;
        for(int i=0; i<n; ++i) anc[i].resize(m);
        auto dfs=[&](auto &self, int u, int fa) -> void{
            ord.push_back(u);
            par[u]=fa;
            tl[u]=++cur;
            anc[u][0]=fa;
            if(fa==-1) dep[u]=0;
            else dep[u]=dep[fa]+1;
            for(int i=1; i<m; ++i){
                if(anc[u][i-1]==-1) anc[u][i]=-1;
                else anc[u][i]=anc[anc[u][i-1]][i-1];
            }
            for(int v: adj[u]) if(v!=fa) self(self,v,u);
            tr[u]=cur;
        };
        dfs(dfs,0,-1);
    }
    bool is_anc(int u, int v){return tl[u]<=tl[v]&&tr[u]>=tr[v];}
    int lca(int u, int v){
        if(is_anc(u,v)) return u;
        for(int i=anc[0].size()-1; i>=0; --i) if(anc[u][i]!=-1&&!is_anc(anc[u][i],v)) u=anc[u][i];
        return par[u];
    }
};

void ahcorz(){
    int n; cin >> n;
    vi a(n); cin >> a;
    Tree tree(n);
    rep(n-1){
        int u,v; cin >> u >> v; u--,v--;
        tree.add_edge(u,v);
    }
    tree.init();
    vi ord(n); iota(all(ord),0);
    sort(all(ord),[&](int i, int j){return a[i]>a[j];});
    int u=ord[0],v=ord[0],cur=0;
    auto dis=[&](int u, int v){return tree.dep[u]+tree.dep[v]-2*tree.dep[tree.lca(u,v)];};
    auto go=[&](int x, int y){
        if(dis(x,y)>dis(u,v)) u=x,v=y;
    };
    vi res(n);
    for(auto i: ord){
        while(cur<n&&a[ord[cur]]>=a[i]){
            int old_u=u,old_v=v;
            go(old_u,ord[cur]),go(old_v,ord[cur]);
            cur++;
        }
        res[i]=max(dis(i,u),dis(i,v));
    }
    rep(n) cout << res[i] << ' ';
    cout << '\n';
}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    cout << fixed << setprecision(20);
    int t=1;
    //cin >> t;
    while(t--) ahcorz();
}
0