結果

問題 No.364 門松木
ユーザー mamekinmamekin
提出日時 2016-10-09 16:55:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,795 bytes
コンパイル時間 1,591 ms
コンパイル使用メモリ 125,040 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-05-01 18:31:01
合計ジャッジ時間 15,787 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 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,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 235 ms
18,816 KB
testcase_12 WA -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
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 #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const int INF = INT_MAX / 4;

vector<int> a;
vector<vector<int> > edges;

map<tuple<int, int, int>, int> memo;

int solve(int curr, int prev, int prevValue)
{
    if(memo.find(make_tuple(curr, prev, prevValue)) != memo.end())
        return memo[make_tuple(curr, prev, prevValue)];

    map<int, int> maxValue;
    for(int next : edges[curr]){
        if(next == prev || a[next] == prevValue || ((a[curr] < prevValue) ^ (a[curr] < a[next])))
            continue;
        maxValue[a[next]] = max(maxValue[a[next]], solve(next, curr, a[curr]));
    }

    int ans;
    if(prevValue == INF || prevValue == -INF)
        ans = maxValue.size() * (maxValue.size() - 1) / 2;
    else
        ans = maxValue.size() * (maxValue.size() + 1) / 2;
    for(const auto& p : maxValue)
        ans += p.second;

    return memo[make_tuple(curr, prev, prevValue)] = ans;
}

int main()
{
    int n;
    cin >> n;
    a.resize(n);
    for(int i=0; i<n; ++i)
        cin >> a[i];

    edges.assign(n, vector<int>());
    for(int i=0; i<n-1; ++i){
        int x, y;
        cin >> x >> y;
        -- x;
        -- y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }

    int ans = 0;
    for(int i=0; i<n; ++i){
        ans = max(ans, solve(i, -1, -INF));
        ans = max(ans, solve(i, -1, INF));
    }
    cout << ans << endl;

    return 0;
}
0