結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 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 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 52 ms
6,528 KB
testcase_09 AC 52 ms
6,528 KB
testcase_10 AC 45 ms
6,400 KB
testcase_11 AC 52 ms
6,528 KB
testcase_12 AC 46 ms
6,400 KB
testcase_13 AC 45 ms
6,400 KB
testcase_14 AC 46 ms
6,400 KB
testcase_15 AC 49 ms
6,272 KB
testcase_16 AC 45 ms
6,272 KB
testcase_17 AC 51 ms
6,272 KB
testcase_18 AC 53 ms
6,528 KB
testcase_19 AC 55 ms
7,040 KB
testcase_20 AC 52 ms
6,784 KB
testcase_21 AC 52 ms
6,528 KB
testcase_22 AC 37 ms
6,400 KB
testcase_23 AC 51 ms
8,576 KB
testcase_24 AC 51 ms
6,400 KB
testcase_25 AC 53 ms
8,832 KB
testcase_26 AC 55 ms
7,808 KB
testcase_27 AC 53 ms
6,656 KB
testcase_28 AC 59 ms
6,784 KB
testcase_29 AC 52 ms
8,320 KB
testcase_30 AC 50 ms
9,088 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 53 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, bool isLarge, int prevValue, int& ans)
{
    map<int, int> maxValue;
    for(int next : edges[curr]){
        if(next == prev)
            continue;
        int value = solve(next, curr, !isLarge, a[curr], ans);
        if(a[curr] == a[next] || (isLarge ^ (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, true, INF, ans);
    solve(0, -1, false, -INF, ans);
    cout << ans << endl;

    return 0;
}
0