結果

問題 No.364 門松木
ユーザー parukiparuki
提出日時 2016-06-20 15:23:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,880 bytes
コンパイル時間 2,044 ms
コンパイル使用メモリ 169,604 KB
実行使用メモリ 405,120 KB
最終ジャッジ日時 2024-04-19 13:04:55
合計ジャッジ時間 10,354 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 24 ms
9,856 KB
testcase_14 AC 25 ms
9,856 KB
testcase_15 AC 21 ms
7,808 KB
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 AC 513 ms
359,680 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 526 ms
405,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int NX = 50001, AX = 1001;
int N, A[NX];
vector<vi> G;

ll memo[NX][2];
unsigned maxi_[NX * 3][AX], ptr = 0;
ll rec(int cur, int pre, int preUse){
    ll &res = memo[cur][preUse];
    if(res != -1)return res;
    // 葉
    if(sz(G[cur]) == 1 && pre != -1)return res = 0;
    res = 0;
    
    if(!preUse){
        // curを使わない
        each(nex, G[cur])
            smax(res, rec(nex, cur, 0));
        // A[cur]が最小
        {
            auto maxi = maxi_[ptr++];
            ll tmp = 0, cnt = 0;
            each(nex, G[cur])if(nex!=pre && A[cur] < A[nex]){
                cnt++;
                smax(maxi[A[nex]], (unsigned)rec(nex, cur, 1));
            }
            rep(i, AX)tmp += maxi[i];
            smax(res, tmp + cnt * (cnt - 1) / 2);
        }
        // A[cur]が最大
        {
            auto maxi = maxi_[ptr++];
            ll tmp = 0, cnt = 0;
            each(nex, G[cur])if(nex != pre && A[cur] > A[nex]){
                cnt++;
                smax(maxi[A[nex]], (unsigned)rec(nex, cur, 1));
            }
            rep(i, AX)tmp += maxi[i];
            smax(res, tmp + cnt * (cnt - 1) / 2);
        }
    } else{
        // A[cur]が最小
        if(A[cur] < A[pre]){
            auto maxi = maxi_[ptr++];
            ll tmp = 0, cnt = 1;
            each(nex, G[cur])if(A[cur] < A[nex] && A[nex] != A[pre]){
                cnt++;
                smax(maxi[A[nex]], (unsigned)rec(nex, cur, 1));
            }
            rep(i, AX)tmp += maxi[i];
            smax(res, tmp + cnt * (cnt - 1) / 2);
        }
        // A[cur]が最大
        if(A[cur] > A[pre]){
            auto maxi = maxi_[ptr++];
            ll tmp = 0, cnt = 1;
            each(nex, G[cur])if(A[cur] > A[nex] && A[nex] != A[pre]){
                cnt++;
                smax(maxi[A[nex]], (unsigned)rec(nex, cur, 1));
            }
            rep(i, AX)tmp += maxi[i];
            smax(res, tmp + cnt * (cnt - 1) / 2);
        }
    }
    return res;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N;
    if(N < 3){
        cout << 0 << endl;
        return 0;
    }
    rep(i, N)cin >> A[i + 1];
    G.resize(N + 1);
    rep(i, N - 1){
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    
    MEM(memo, -1);
    ll ans = rec(1, -1, 0);
    cout << ans << endl;
    
}
0