結果
問題 | No.364 門松木 |
ユーザー | koyumeishi |
提出日時 | 2016-03-16 02:02:32 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 392 ms / 3,000 ms |
コード長 | 2,317 bytes |
コンパイル時間 | 789 ms |
コンパイル使用メモリ | 98,120 KB |
実行使用メモリ | 100,632 KB |
最終ジャッジ日時 | 2024-04-09 19:56:09 |
合計ジャッジ時間 | 5,157 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 184 ms
12,616 KB |
testcase_09 | AC | 273 ms
18,240 KB |
testcase_10 | AC | 26 ms
6,940 KB |
testcase_11 | AC | 263 ms
20,980 KB |
testcase_12 | AC | 26 ms
6,944 KB |
testcase_13 | AC | 60 ms
6,944 KB |
testcase_14 | AC | 60 ms
6,940 KB |
testcase_15 | AC | 59 ms
6,940 KB |
testcase_16 | AC | 27 ms
6,948 KB |
testcase_17 | AC | 168 ms
9,584 KB |
testcase_18 | AC | 38 ms
8,832 KB |
testcase_19 | AC | 287 ms
55,352 KB |
testcase_20 | AC | 27 ms
7,040 KB |
testcase_21 | AC | 33 ms
9,600 KB |
testcase_22 | AC | 26 ms
12,672 KB |
testcase_23 | AC | 48 ms
22,400 KB |
testcase_24 | AC | 31 ms
6,944 KB |
testcase_25 | AC | 57 ms
31,616 KB |
testcase_26 | AC | 392 ms
100,632 KB |
testcase_27 | AC | 299 ms
31,912 KB |
testcase_28 | AC | 65 ms
7,040 KB |
testcase_29 | AC | 33 ms
11,860 KB |
testcase_30 | AC | 32 ms
13,184 KB |
testcase_31 | AC | 1 ms
6,944 KB |
testcase_32 | AC | 1 ms
6,940 KB |
testcase_33 | AC | 31 ms
15,616 KB |
コンパイルメッセージ
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; }