結果

問題 No.3206 う し た ウ ニ 木 あ く ん 笑
ユーザー cho435
提出日時 2025-07-18 23:51:54
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 181 ms / 3,000 ms
コード長 2,167 bytes
コンパイル時間 4,060 ms
コンパイル使用メモリ 260,064 KB
実行使用メモリ 89,920 KB
最終ジャッジ日時 2025-07-18 23:52:02
合計ジャッジ時間 7,629 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)

template <class T> bool chmin(T& x, T y) {
	return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
	return x < y ? (x = y, true) : false;
}

struct io_setup {
	io_setup() {
		ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} io_setup;

namespace cho {

template <class S, auto op> struct lr_dp_table {
	std::vector<S> dp_l, dp_r;
	int sz;
	lr_dp_table(const std::vector<S>& v) {
		sz = v.size();
		dp_l.resize(sz);
		dp_r.resize(sz);
		dp_l[0] = v[0];
		for (int i = 1; i < sz; i++) {
			dp_l[i] = op(dp_l[i - 1], v[i]);
		}
		dp_r[sz - 1] = v[sz - 1];
		for (int i = sz - 1; i > 0; i--) {
			dp_r[i - 1] = op(v[i - 1], dp_r[i]);
		}
	}
	S get(int i) {
		assert(0 <= i && i < sz);
		if (i == 0) return dp_r[1];
		if (i == sz - 1) return dp_l[sz - 2];
		return op(dp_l[i - 1], dp_r[i + 1]);
	}
};

}  // namespace cho

int op(int a, int b) {
	return max(a, b);
}
using d_table = cho::lr_dp_table<int, op>;

void solve() {
	int n;
	cin >> n;
	vector<vector<int>> g(n);
	rep(i, 0, n - 1) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> childs(n);
	{
		auto dfs = [&](auto self, int nw, int pr) -> void {
			int res = 0;
			for (int nx : g[nw]) {
				if (nx == pr) continue;
				self(self, nx, nw);
				chmax(res, childs[nx] + 1);
			}
			childs[nw] = res;
		};
		dfs(dfs, 0, -1);
	}
	ll ans = 0;
	{
		auto dfs = [&](auto self, int nw, int pr, int p_val) -> void {
			vector<int> v;
			for (int nx : g[nw]) {
				if (nx == pr) continue;
				v.push_back(childs[nx] + 1);
			}
			v.push_back(p_val);
			{
				d_table d(v);
				int i = 0;
				for (int nx : g[nw]) {
					if (nx == pr) continue;
					self(self, nx, nw, d.get(i) + 1);
					i++;
				}
			}
			sort(v.rbegin(), v.rend());
			rep(i, 0, v.size()) {
				chmax(ans, (i + 1) * v[i] + 1);
			}
		};
		dfs(dfs, 0, -1, 0);
	}
	cout << ans << "\n";
}
int main() {
	int t = 1;
	// cin >> t;
	while (t--) solve();
}
0