結果

問題 No.364 門松木
ユーザー latte0119latte0119
提出日時 2016-04-17 23:54:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 48 ms / 3,000 ms
コード長 2,198 bytes
コンパイル時間 2,556 ms
コンパイル使用メモリ 155,488 KB
実行使用メモリ 21,344 KB
最終ジャッジ日時 2023-07-27 14:57:01
合計ジャッジ時間 4,425 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,404 KB
testcase_01 AC 5 ms
9,316 KB
testcase_02 AC 4 ms
9,328 KB
testcase_03 AC 4 ms
9,356 KB
testcase_04 AC 4 ms
9,404 KB
testcase_05 AC 5 ms
9,356 KB
testcase_06 AC 4 ms
9,488 KB
testcase_07 AC 4 ms
9,352 KB
testcase_08 AC 39 ms
15,360 KB
testcase_09 AC 39 ms
15,268 KB
testcase_10 AC 34 ms
14,280 KB
testcase_11 AC 39 ms
15,624 KB
testcase_12 AC 32 ms
13,744 KB
testcase_13 AC 31 ms
15,544 KB
testcase_14 AC 30 ms
15,572 KB
testcase_15 AC 30 ms
15,432 KB
testcase_16 AC 29 ms
15,468 KB
testcase_17 AC 40 ms
15,372 KB
testcase_18 AC 42 ms
15,476 KB
testcase_19 AC 43 ms
16,040 KB
testcase_20 AC 42 ms
15,380 KB
testcase_21 AC 44 ms
15,316 KB
testcase_22 AC 30 ms
13,816 KB
testcase_23 AC 47 ms
16,740 KB
testcase_24 AC 44 ms
15,272 KB
testcase_25 AC 47 ms
17,040 KB
testcase_26 AC 48 ms
16,420 KB
testcase_27 AC 43 ms
15,728 KB
testcase_28 AC 44 ms
15,600 KB
testcase_29 AC 46 ms
16,392 KB
testcase_30 AC 45 ms
16,748 KB
testcase_31 AC 5 ms
9,332 KB
testcase_32 AC 4 ms
9,352 KB
testcase_33 AC 36 ms
21,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

const int SIZE=50000;
int N;
int A[SIZE];
vint G[SIZE];

int ans;
map<int,int>li_mi[SIZE],li_ma[SIZE];
int dp_mi[SIZE],dp_ma[SIZE];

void dfs(int v,int p,int t){
    if(t==0){
        for(auto to:G[v]){
            if(to==p)continue;
            dfs(to,v,1);
            if(A[v]>=A[to])continue;
            int tmp;
            if(li_ma[to].find(A[v])!=li_ma[to].end()){
                tmp=dp_ma[to]-li_ma[to][A[v]];
            }
            else{
                int hoge=li_ma[to].size();
                tmp=dp_ma[to]-hoge*(hoge-1)/2+(hoge+1)*hoge/2;
            }
            chmax(li_mi[v][A[to]],tmp);
        }
        each(it,li_mi[v])dp_mi[v]+=it->se;
        int hoge=li_mi[v].size();
        dp_mi[v]+=hoge*(hoge-1)/2;
        chmax(ans,dp_mi[v]);
    }
    else{
        for(auto to:G[v]){
            if(to==p)continue;
            dfs(to,v,0);
            if(A[v]<=A[to])continue;
            int tmp;
            if(li_mi[to].find(A[v])!=li_mi[to].end()){
                tmp=dp_mi[to]-li_mi[to][A[v]];
            }
            else{
                int hoge=li_mi[to].size();
                tmp=dp_mi[to]-hoge*(hoge-1)/2+(hoge+1)*hoge/2;
            }
            chmax(li_ma[v][A[to]],tmp);
        }
        each(it,li_ma[v])dp_ma[v]+=it->se;
        int hoge=li_ma[v].size();
        dp_ma[v]+=hoge*(hoge-1)/2;
        chmax(ans,dp_ma[v]);
    }
}

signed main(){
    scanf("%lld",&N);
    rep(i,N)scanf("%lld",&A[i]);
    rep(i,N-1){
        int a,b;
        scanf("%lld%lld",&a,&b);
        a--;b--;
        G[a].pb(b);
        G[b].pb(a);
    }

    dfs(0,-1,0);dfs(0,-1,1);
    cout<<ans<<endl;
    return 0;
}
0