結果

問題 No.364 門松木
ユーザー mamekinmamekin
提出日時 2016-10-09 19:40:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,576 bytes
コンパイル時間 1,355 ms
コンパイル使用メモリ 116,352 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2024-05-01 18:32:16
合計ジャッジ時間 3,290 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 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 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 43 ms
6,528 KB
testcase_14 AC 43 ms
6,528 KB
testcase_15 AC 45 ms
6,272 KB
testcase_16 AC 47 ms
6,272 KB
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 WA -
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 1 ms
5,376 KB
testcase_33 AC 49 ms
15,488 KB
権限があれば一括ダウンロードができます

ソースコード

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;

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

    int score = maxValue.size() * (maxValue.size() - 1) / 2;
    for(const auto& p : maxValue)
        score += p.second;
    ans = max(score, ans);

    maxValue[prevValue] = 0;
    score = maxValue.size() * (maxValue.size() - 1) / 2;
    for(const auto& p : maxValue)
        score += p.second;
    return score;
}

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;
    solve(0, -1, -INF, ans);
    ans, solve(0, -1, INF, ans);
    cout << ans << endl;

    return 0;
}
0