結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 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 1 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 53 ms
6,400 KB
testcase_10 AC 52 ms
6,400 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 56 ms
6,400 KB
testcase_14 AC 54 ms
6,400 KB
testcase_15 AC 56 ms
6,272 KB
testcase_16 AC 48 ms
6,272 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 51 ms
6,784 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 56 ms
8,576 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 59 ms
8,320 KB
testcase_30 WA -
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 54 ms
15,616 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)
            continue;
        int value = solve(next, curr, a[curr], ans);
        if(a[curr] == a[next] || ((a[curr] < prevValue) ^ (a[curr] < a[next])))
            continue;
        maxValue[a[next]] = max(maxValue[a[next]], value);
    }

    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