結果

問題 No.364 門松木
ユーザー koyumeishikoyumeishi
提出日時 2016-03-16 02:03:37
言語 C++11
(gcc 11.4.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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 1 ms
6,820 KB
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 1 ms
6,816 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 1 ms
6,820 KB
testcase_08 AC 267 ms
12,620 KB
testcase_09 AC 373 ms
18,184 KB
testcase_10 AC 24 ms
6,820 KB
testcase_11 AC 368 ms
21,016 KB
testcase_12 AC 24 ms
6,820 KB
testcase_13 AC 57 ms
6,816 KB
testcase_14 AC 55 ms
6,820 KB
testcase_15 AC 55 ms
6,820 KB
testcase_16 AC 22 ms
6,816 KB
testcase_17 AC 236 ms
9,588 KB
testcase_18 AC 36 ms
8,832 KB
testcase_19 AC 402 ms
55,228 KB
testcase_20 AC 25 ms
6,912 KB
testcase_21 AC 31 ms
9,728 KB
testcase_22 AC 27 ms
12,544 KB
testcase_23 AC 52 ms
22,400 KB
testcase_24 AC 29 ms
6,816 KB
testcase_25 AC 61 ms
31,488 KB
testcase_26 AC 515 ms
100,760 KB
testcase_27 AC 372 ms
31,908 KB
testcase_28 AC 61 ms
6,912 KB
testcase_29 AC 31 ms
11,864 KB
testcase_30 AC 31 ms
13,184 KB
testcase_31 AC 2 ms
6,816 KB
testcase_32 AC 2 ms
6,820 KB
testcase_33 AC 31 ms
15,616 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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

ソースコード

diff #

#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;
}
0