結果

問題 No.364 門松木
ユーザー parukiparuki
提出日時 2016-06-20 15:35:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,144 bytes
コンパイル時間 1,593 ms
コンパイル使用メモリ 171,496 KB
実行使用メモリ 813,832 KB
最終ジャッジ日時 2024-04-19 13:06:23
合計ジャッジ時間 12,736 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 435 ms
410,624 KB
testcase_09 AC 422 ms
410,880 KB
testcase_10 AC 337 ms
273,280 KB
testcase_11 AC 405 ms
383,872 KB
testcase_12 AC 261 ms
184,704 KB
testcase_13 AC 24 ms
12,544 KB
testcase_14 AC 23 ms
12,288 KB
testcase_15 AC 20 ms
8,448 KB
testcase_16 AC 37 ms
22,272 KB
testcase_17 MLE -
testcase_18 MLE -
testcase_19 AC 405 ms
397,312 KB
testcase_20 MLE -
testcase_21 MLE -
testcase_22 AC 209 ms
194,688 KB
testcase_23 MLE -
testcase_24 AC 341 ms
296,832 KB
testcase_25 MLE -
testcase_26 MLE -
testcase_27 AC 445 ms
429,312 KB
testcase_28 AC 33 ms
18,304 KB
testcase_29 MLE -
testcase_30 MLE -
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 MLE -
権限があれば一括ダウンロードができます

ソースコード

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], use_[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];
            auto use = use_[ptr++];
            ll tmp = 0, cnt = 0;
            each(nex, G[cur])if(nex!=pre && A[cur] < A[nex]){
                use[A[nex]] = true;
                smax(maxi[A[nex]], (unsigned)rec(nex, cur, 1));
            }
            rep(i, AX)if(use[i])tmp += maxi[i], cnt++;
            smax(res, tmp + cnt * (cnt - 1) / 2);
        }
        // A[cur]が最大
        {
            auto maxi = maxi_[ptr];
            auto use = use_[ptr++];
            ll tmp = 0, cnt = 0;
            each(nex, G[cur])if(nex != pre && A[cur] > A[nex]){
                use[A[nex]] = true;
                smax(maxi[A[nex]], (unsigned)rec(nex, cur, 1));
            }
            rep(i, AX)if(use[i])tmp += maxi[i], cnt++;
            smax(res, tmp + cnt * (cnt - 1) / 2);
        }
    } else{
        // A[cur]が最小
        if(A[cur] < A[pre]){
            auto maxi = maxi_[ptr];
            auto use = use_[ptr++];
            ll tmp = 0, cnt = 1;
            each(nex, G[cur])if(A[cur] < A[nex] && A[nex] != A[pre]){
                use[A[nex]] = true;
                smax(maxi[A[nex]], (unsigned)rec(nex, cur, 1));
            }
            rep(i, AX)if(use[i])tmp += maxi[i], cnt++;
            smax(res, tmp + cnt * (cnt - 1) / 2);
        }
        // A[cur]が最大
        if(A[cur] > A[pre]){
            auto maxi = maxi_[ptr];
            auto use = use_[ptr++];
            ll tmp = 0, cnt = 1;
            each(nex, G[cur])if(A[cur] > A[nex] && A[nex] != A[pre]){
                use[A[nex]] = true;
                smax(maxi[A[nex]], (unsigned)rec(nex, cur, 1));
            }
            rep(i, AX)if(use[i])tmp += maxi[i], cnt++;
            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