結果

問題 No.364 門松木
ユーザー ryofjtryofjt
提出日時 2016-09-12 18:17:47
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,173 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 51,416 KB
最終ジャッジ日時 2023-08-25 02:55:08
合計ジャッジ時間 2,410 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:9:9: error: ‘vector’ does not name a type; did you mean ‘perror’?
 typedef vector<int> V;
         ^~~~~~
         perror
main.cpp:10:9: error: ‘vector’ does not name a type; did you mean ‘perror’?
 typedef vector<short> VS;
         ^~~~~~
         perror
main.cpp:11:9: error: ‘vector’ does not name a type; did you mean ‘perror’?
 typedef vector<uint> VU;
         ^~~~~~
         perror
main.cpp:15:1: error: ‘V’ does not name a type
 V t[50000];
 ^
main.cpp:19:1: error: ‘VU’ does not name a type
 VU eval(const VS& c, const VU& v, int p){
 ^~
main.cpp: In function ‘uint dfs(int, int)’:
main.cpp:34:2: error: ‘VS’ was not declared in this scope
  VS c(1000, 0);
  ^~
main.cpp:35:2: error: ‘VU’ was not declared in this scope
  VU v(1000, 0);
  ^~
main.cpp:36:13: error: ‘t’ was not declared in this scope
  for(int i: t[x]){
             ^
main.cpp:38:4: error: ‘c’ was not declared in this scope
    c[a[i]] = 1;
    ^
main.cpp:39:4: error: ‘v’ was not declared in this scope
    v[a[i]] = max(dfs(i, a[x]), v[a[i]]);
    ^
main.cpp:42:17: error: ‘c’ was not declared in this scope
  auto e1 = eval(c, v, a[x]);
                 ^
main.cpp:42:20: error: ‘v’ was not declared in this scope
  auto e1 = eval(c, v, a[x]);
                    ^
main.cpp:42:12: error: ‘eval’ was not declared in this scope
  auto e1 = eval(c, v, a[x]);
            ^~~~
main.cpp: In function ‘int main()’:
main.cpp:61:3: error: ‘t’ was not declared in this scope
   t[x].push_back(y);
   ^

ソースコード

diff #

#include <iostream>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef unsigned int uint;
typedef vector<int> V;
typedef vector<short> VS;
typedef vector<uint> VU;

int n;
int a[50000];
V t[50000];
bool u[50000];
uint ans;

VU eval(const VS& c, const VU& v, int p){
	V cs(1000 + 1, 0);
	VU vs(1000 + 1, 0);
	rep(i, 1000){
		cs[i + 1] = cs[i] + c[i];
		vs[i + 1] = vs[i] + v[i];
	}
	uint ca = cs[p], va = vs[p];
	uint cb = cs[1000] - cs[p + 1], vb = vs[1000] - vs[p + 1];
	uint vc = vs[p + 1] - vs[p];
	return {ca * (ca - 1) / 2 + va, cb * (cb - 1) / 2 + vb, vc};
}

uint dfs(int x, int k){
	u[x] = true;
	VS c(1000, 0);
	VU v(1000, 0);
	for(int i: t[x]){
		if(!u[i]){
			c[a[i]] = 1;
			v[a[i]] = max(dfs(i, a[x]), v[a[i]]);
		}
	}
	auto e1 = eval(c, v, a[x]);
	ans = max(*max_element(e1.begin(), e1.end()), ans);
	c[k] = 1;
	v[k] = 0;
	auto e2 = eval(c, v, a[x]);
	return x[a] != k ? e2[x[a] < k] : e2[2];
}

int main(){
	cin >> n;
	rep(i, n){
		cin >> a[i];
		--a[i];
	}
	rep(i, n - 1){
		int x, y;
		cin >> x >> y;
		--x;
		--y;
		t[x].push_back(y);
		t[y].push_back(x);
	}

	dfs(0, 1);
	cout << ans << endl;
	return 0;
}
0