結果

問題 No.364 門松木
ユーザー parukiparuki
提出日時 2016-06-20 15:36:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,149 bytes
コンパイル時間 1,745 ms
コンパイル使用メモリ 170,036 KB
実行使用メモリ 552,316 KB
最終ジャッジ日時 2024-04-19 21:19:47
合計ジャッジ時間 11,193 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 371 ms
294,668 KB
testcase_09 AC 351 ms
293,412 KB
testcase_10 AC 291 ms
212,520 KB
testcase_11 AC 343 ms
275,016 KB
testcase_12 AC 255 ms
157,008 KB
testcase_13 AC 23 ms
11,840 KB
testcase_14 AC 23 ms
11,972 KB
testcase_15 AC 20 ms
7,808 KB
testcase_16 AC 33 ms
17,664 KB
testcase_17 AC 425 ms
345,644 KB
testcase_18 AC 421 ms
340,600 KB
testcase_19 AC 352 ms
279,880 KB
testcase_20 AC 459 ms
355,604 KB
testcase_21 AC 427 ms
330,492 KB
testcase_22 AC 175 ms
135,648 KB
testcase_23 AC 505 ms
392,736 KB
testcase_24 AC 270 ms
205,416 KB
testcase_25 AC 620 ms
495,656 KB
testcase_26 AC 401 ms
318,804 KB
testcase_27 AC 375 ms
304,412 KB
testcase_28 AC 33 ms
14,208 KB
testcase_29 AC 448 ms
354,348 KB
testcase_30 AC 507 ms
403,320 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 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], ptr = 0;
bool use_[NX * 3][AX];
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