結果

問題 No.364 門松木
ユーザー koyumeishikoyumeishi
提出日時 2016-03-16 02:03:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 407 ms / 3,000 ms
コード長 2,819 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 100,020 KB
実行使用メモリ 100,624 KB
最終ジャッジ日時 2024-04-09 19:56:15
合計ジャッジ時間 4,534 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 187 ms
12,744 KB
testcase_09 AC 287 ms
18,188 KB
testcase_10 AC 25 ms
6,940 KB
testcase_11 AC 282 ms
20,984 KB
testcase_12 AC 25 ms
6,940 KB
testcase_13 AC 59 ms
6,940 KB
testcase_14 AC 59 ms
6,940 KB
testcase_15 AC 59 ms
6,944 KB
testcase_16 AC 25 ms
6,940 KB
testcase_17 AC 178 ms
9,460 KB
testcase_18 AC 36 ms
8,832 KB
testcase_19 AC 289 ms
55,352 KB
testcase_20 AC 24 ms
6,944 KB
testcase_21 AC 30 ms
9,600 KB
testcase_22 AC 26 ms
12,672 KB
testcase_23 AC 49 ms
22,528 KB
testcase_24 AC 29 ms
6,940 KB
testcase_25 AC 58 ms
31,616 KB
testcase_26 AC 407 ms
100,624 KB
testcase_27 AC 303 ms
31,908 KB
testcase_28 AC 66 ms
7,040 KB
testcase_29 AC 34 ms
11,736 KB
testcase_30 AC 35 ms
13,184 KB
testcase_31 AC 1 ms
6,940 KB
testcase_32 AC 1 ms
6,944 KB
testcase_33 AC 33 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