結果

問題 No.364 門松木
ユーザー parukiparuki
提出日時 2016-06-20 15:30:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,157 bytes
コンパイル時間 1,728 ms
コンパイル使用メモリ 170,916 KB
実行使用メモリ 14,008 KB
最終ジャッジ日時 2024-04-19 13:05:45
合計ジャッジ時間 9,689 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

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