結果

問題 No.2634 Tree Distance 3
ユーザー maksimmaksim
提出日時 2024-02-16 22:06:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,331 bytes
コンパイル時間 3,655 ms
コンパイル使用メモリ 179,436 KB
実行使用メモリ 86,072 KB
最終ジャッジ日時 2024-02-16 22:07:39
合計ジャッジ時間 51,009 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 707 ms
78,700 KB
testcase_01 AC 684 ms
78,664 KB
testcase_02 AC 674 ms
78,680 KB
testcase_03 AC 651 ms
78,728 KB
testcase_04 WA -
testcase_05 AC 698 ms
78,640 KB
testcase_06 AC 701 ms
78,660 KB
testcase_07 AC 733 ms
78,680 KB
testcase_08 AC 615 ms
78,024 KB
testcase_09 AC 612 ms
78,096 KB
testcase_10 AC 596 ms
78,100 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 598 ms
78,120 KB
testcase_14 AC 562 ms
77,940 KB
testcase_15 AC 582 ms
78,096 KB
testcase_16 AC 899 ms
76,672 KB
testcase_17 WA -
testcase_18 AC 705 ms
65,200 KB
testcase_19 WA -
testcase_20 AC 212 ms
47,636 KB
testcase_21 AC 1,316 ms
82,808 KB
testcase_22 AC 1,195 ms
85,104 KB
testcase_23 AC 1,194 ms
86,072 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1,219 ms
80,064 KB
testcase_27 AC 1,206 ms
78,440 KB
testcase_28 AC 1,218 ms
79,556 KB
testcase_29 WA -
testcase_30 AC 776 ms
77,448 KB
testcase_31 AC 801 ms
77,504 KB
testcase_32 AC 758 ms
77,464 KB
testcase_33 AC 457 ms
66,268 KB
testcase_34 AC 95 ms
39,816 KB
testcase_35 AC 253 ms
55,128 KB
testcase_36 AC 155 ms
45,068 KB
testcase_37 AC 300 ms
58,004 KB
testcase_38 AC 17 ms
33,240 KB
testcase_39 AC 17 ms
33,248 KB
testcase_40 AC 15 ms
33,088 KB
testcase_41 AC 15 ms
33,080 KB
testcase_42 AC 15 ms
33,084 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 646 ms
71,700 KB
testcase_46 AC 426 ms
60,676 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 769 ms
77,412 KB
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 AC 15 ms
33,080 KB
testcase_57 AC 15 ms
33,084 KB
testcase_58 AC 12 ms
32,900 KB
testcase_59 AC 12 ms
32,904 KB
testcase_60 AC 692 ms
77,452 KB
testcase_61 WA -
testcase_62 AC 686 ms
77,404 KB
testcase_63 AC 515 ms
77,612 KB
testcase_64 AC 359 ms
63,880 KB
testcase_65 AC 485 ms
74,972 KB
testcase_66 AC 149 ms
47,568 KB
testcase_67 AC 122 ms
45,004 KB
testcase_68 AC 535 ms
77,940 KB
testcase_69 AC 553 ms
77,972 KB
testcase_70 AC 536 ms
77,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 i=1;i<20;++i) {u[x][i]=u[u[x][i-1]][i-1];}
    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