結果
問題 |
No.364 門松木
|
ユーザー |
|
提出日時 | 2016-10-09 16:55:45 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,795 bytes |
コンパイル時間 | 1,439 ms |
コンパイル使用メモリ | 124,668 KB |
実行使用メモリ | 44,672 KB |
最終ジャッジ日時 | 2024-11-22 00:19:24 |
合計ジャッジ時間 | 25,098 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 WA * 10 TLE * 4 |
ソースコード
#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; }