結果

問題 No.2634 Tree Distance 3
ユーザー maksimmaksim
提出日時 2024-02-16 22:05:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,276 bytes
コンパイル時間 3,086 ms
コンパイル使用メモリ 179,180 KB
実行使用メモリ 87,116 KB
最終ジャッジ日時 2024-02-16 22:06:21
合計ジャッジ時間 38,522 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 AC 13 ms
34,948 KB
testcase_59 AC 13 ms
34,952 KB
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 479 ms
78,628 KB
testcase_64 AC 319 ms
64,896 KB
testcase_65 AC 449 ms
75,972 KB
testcase_66 AC 133 ms
48,560 KB
testcase_67 AC 108 ms
46,000 KB
testcase_68 AC 506 ms
78,952 KB
testcase_69 AC 496 ms
78,856 KB
testcase_70 AC 503 ms
78,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define app push_back
#define all(x) (x).begin(),(x).end()
#ifdef LOCAL
#define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
#else
#define debug(...)
#endif
#ifdef LOCAL
#define __int128 long long
#endif // LOCAL
const int maxn=1e6+5;
vector<int> a[maxn];
int c[maxn];
bool used[maxn];
int corn[maxn];
int u[maxn][20];
int res[maxn];
pair<int,int> cur={-1,-1};
void dfs(int x)
{
    used[x]=true;
    for(int v:a[x])
    {
        if(!used[v])
        {
            u[v][0]=x;
            corn[v]=corn[x]+1;
            dfs(v);
        }
    }
    used[x]=false;
}
int lca(int x,int y)
{
    for(int i=19;i>=0;--i) {if(corn[x]>=corn[y]+(1<<i)) x=u[x][i];}
    for(int i=19;i>=0;--i) {if(corn[y]>=corn[x]+(1<<i)) y=u[y][i];}
    for(int i=19;i>=0;--i) {if(u[x][i]!=u[y][i]) {x=u[x][i];y=u[y][i];}}
    if(x!=y) {x=u[x][0];y=u[y][0];}
    return x;
}
int dist(int x,int y)
{
    return corn[x]+corn[y]-2*corn[lca(x,y)];
}
void add(int pos)
{
    if(cur.first==(-1)) {cur.first=pos;return;}
    if(cur.second==(-1)) {cur.second=pos;return;}
    int was=dist(cur.first,cur.second);int x1=dist(cur.first,pos);int x2=dist(cur.second,pos);
    int ma=max({was,x1,x2});
    if(was==ma) return;
    if(x1==ma) {cur={cur.first,pos};return;}
    cur={pos,cur.second};
}
int get(int pos)
{
    return max(dist(pos,cur.first),dist(pos,cur.second));
}
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;cin>>n;
    for(int i=0;i<n;++i) cin>>c[i];
    for(int i=0;i<n-1;++i) {int x,y;cin>>x>>y;--x;--y;a[x].app(y);a[y].app(x);}
    vector<int> v(n);iota(all(v),0LL);sort(all(v),[&](int i,int j) {return make_pair(c[i],i)<make_pair(c[j],j);});
    dfs(0);
    for(int i=n-1;i>=0;--i)
    {
        int pos=v[i];
        if(i==n-1) {add(pos);continue;}
        res[pos]=max(res[pos],get(pos));
        add(pos);
    }
    cur={-1,-1};
    sort(all(v),[&](int i,int j) {return make_pair(c[i],-i)<make_pair(c[j],-j);});
    for(int i=n-1;i>=0;--i)
    {
        int pos=v[i];
        if(i==n-1) {add(pos);continue;}
        res[pos]=max(res[pos],get(pos));
        add(pos);
    }
    for(int i=0;i<n;++i) cout<<res[i]<<' ';
    return 0;
}
0