結果

問題 No.364 門松木
ユーザー mamekinmamekin
提出日時 2016-10-09 20:10:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 3,000 ms
コード長 1,674 bytes
コンパイル時間 1,964 ms
コンパイル使用メモリ 114,460 KB
実行使用メモリ 14,656 KB
最終ジャッジ日時 2023-08-14 05:46:50
合計ジャッジ時間 4,030 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 54 ms
6,336 KB
testcase_09 AC 54 ms
6,312 KB
testcase_10 AC 50 ms
6,184 KB
testcase_11 AC 55 ms
6,152 KB
testcase_12 AC 50 ms
6,316 KB
testcase_13 AC 49 ms
6,320 KB
testcase_14 AC 48 ms
6,260 KB
testcase_15 AC 48 ms
6,256 KB
testcase_16 AC 46 ms
6,012 KB
testcase_17 AC 55 ms
6,240 KB
testcase_18 AC 54 ms
6,152 KB
testcase_19 AC 57 ms
6,692 KB
testcase_20 AC 52 ms
6,504 KB
testcase_21 AC 54 ms
6,260 KB
testcase_22 AC 39 ms
5,888 KB
testcase_23 AC 57 ms
8,100 KB
testcase_24 AC 60 ms
6,192 KB
testcase_25 AC 58 ms
8,308 KB
testcase_26 AC 59 ms
7,452 KB
testcase_27 AC 56 ms
6,576 KB
testcase_28 AC 64 ms
6,504 KB
testcase_29 AC 58 ms
8,100 KB
testcase_30 AC 59 ms
8,632 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 53 ms
14,656 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