結果
| 問題 |
No.364 門松木
|
| コンテスト | |
| ユーザー |
koyumeishi
|
| 提出日時 | 2016-03-16 02:02:32 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 540 ms / 3,000 ms |
| コード長 | 2,317 bytes |
| コンパイル時間 | 890 ms |
| コンパイル使用メモリ | 97,308 KB |
| 実行使用メモリ | 100,624 KB |
| 最終ジャッジ日時 | 2024-10-01 20:10:10 |
| 合計ジャッジ時間 | 6,502 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
コンパイルメッセージ
main.cpp: In function ‘int main_()’:
main.cpp:35:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
35 | scanf("%d%d", &u,&v);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
using namespace std;
template<typename T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
template<typename T> istream& operator , (istream& is, T& val){ return is >> val;}
template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"\n":" ");return os;}
template<typename T> ostream& operator , (ostream& os, T& val){ return os << " " << val;}
template<typename T> ostream& operator >> (ostream& os, T& val){ return os << " " << val;}
using namespace std;
int main_(){
int n;
cin >> n;
vector<int> a(n);
cin >> a;
int max_a = *max_element(a.begin(), a.end());
vector<vector<int>> G(n);
for(int i=0; i<n-1; i++){
int u,v;
//cin >> u,v;
scanf("%d%d", &u,&v);
u--; v--;
G[u].push_back(v);
G[v].push_back(u);
}
long long ans = 0;
function<long long (int, int)> dfs = [&](int pos, int last) -> long long{
vector<long long> dp(max_a+1, 0);
vector<int> used(max_a+1, 0);
for(auto& next: G[pos]){
if(next == last) continue;
long long tmp = dfs(next, pos);
dp[a[next]] = max(dp[a[next]], tmp);
ans = max(ans, dp[a[next]]);
if(a[pos] != a[next]) used[a[next]] = 1;
}
long long ret_small = 0;
long long cnt_small = 0;
for(int i=1; i<a[pos]; i++){
ret_small += dp[i];
cnt_small += used[i];
}
ans = max( ans, ret_small + cnt_small*(cnt_small-1)/2LL );
long long ret_big = 0;
long long cnt_big = 0;
for(int i=a[pos]+1; i<=max_a; i++){
ret_big += dp[i];
cnt_big += used[i];
}
ans = max(ans, ret_big + cnt_big*(cnt_big-1)/2LL );
if(last==-1) return 0;
if(a[pos] == a[last]) return 0;
long long ret = a[pos]>a[last] ? ret_small : ret_big;
long long cnt = a[pos]>a[last] ? cnt_small : cnt_big;
ret -= dp[a[last]];
cnt -= used[a[last]];
ret = ret + cnt + cnt*(cnt-1)/2;
return ret;
};
ans = max(ans, dfs(0, -1));
cout << ans << endl;
return 0;
}
#include <ctime>
int main(){
//auto t = clock();
main_();
//cerr << (clock()-t) << "[ms]" << endl;
return 0;
}
koyumeishi