結果
問題 | No.364 門松木 |
ユーザー |
![]() |
提出日時 | 2016-03-16 02:03:37 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 515 ms / 3,000 ms |
コード長 | 2,819 bytes |
コンパイル時間 | 854 ms |
コンパイル使用メモリ | 99,508 KB |
実行使用メモリ | 100,760 KB |
最終ジャッジ日時 | 2024-10-01 20:10:20 |
合計ジャッジ時間 | 5,190 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 |
コンパイルメッセージ
main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<int>&)’: main.cpp:16:78: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 16 | istream& operator >> (istream& is, vector<int>& vec){for(int& val: vec) scanf("%d", &val); return is;} | ~~~~~^~~~~~~~~~~~ main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<long long int>&)’: main.cpp:17:90: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 17 | istream& operator >> (istream& is, vector<long long>& vec){for(long long& val: vec) scanf("%lld",/*"%I64d",*/ &val); return is;} | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<double>&)’: main.cpp:18:84: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 18 | istream& operator >> (istream& is, vector<double>& vec){for(double& val: vec) scanf("%lf", &val); return is;} | ~~~~~^~~~~~~~~~~~~ main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<std::__cxx11::basic_string<char> >&)’: main.cpp:20:85: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 20 | istream& operator >> (istream& is, vector<string>& vec){for(string& val: vec) {scanf("%s", buff); val=buff;}; return is;} | ~~~~~^~~~~~~~~~~~ main.cpp: In function ‘int main_()’: main.cpp:41:22: warning: ignorin
ソースコード
#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; //input from stdin istream& operator >> (istream& is, vector<int>& vec){for(int& val: vec) scanf("%d", &val); return is;} istream& operator >> (istream& is, vector<long long>& vec){for(long long& val: vec) scanf("%lld",/*"%I64d",*/ &val); return is;} istream& operator >> (istream& is, vector<double>& vec){for(double& val: vec) scanf("%lf", &val); return is;} char buff[200000]; istream& operator >> (istream& is, vector<string>& vec){for(string& val: vec) {scanf("%s", buff); val=buff;}; return is;} 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; }