結果

問題 No.3206 う し た ウ ニ 木 あ く ん 笑
ユーザー 00 Sakuda
提出日時 2025-07-18 22:41:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,429 bytes
コンパイル時間 2,172 ms
コンパイル使用メモリ 210,884 KB
実行使用メモリ 170,320 KB
最終ジャッジ日時 2025-07-18 22:41:54
合計ジャッジ時間 10,369 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 WA * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MN = 1e6;
vector<vector<int>> G(MN);
int N;
vector<int> dp1(MN, 0);
vector<int> dist(MN, 0);
void dfs(int v, int par) {
	for (auto nv : G[v]) {
		if (nv == par) continue;
		dfs(nv, v);
		dist[nv] = dist[v] + 1;
		dp1[v] = max(dp1[v], dp1[nv]+1);
	}
}
vector<vector<int>> DP(MN, vector<int>(2, 0));

int res = 1;
void dfs2(int v, int par) {
	//cout << v << " " << DP[v][0] << " " << DP[v][1] << endl;
	multiset<int> st;
	st.insert(0);
	map<int, int> cnts;
	for (auto nv : G[v]) {
		if (nv == par) continue;
		st.insert(dp1[nv]+1);
		cnts[dp1[nv]+1]++;
	}
	if (v == par) {
		for (auto v : cnts) {
			res = max(res, 1 + ((v.first)*v.second));
		}
		//cout << res << endl;
	} else {
		int dd = 1 + max(DP[v][0], DP[v][1]);
		res = max(res, 1 + dd);
	//	cout << res << endl;
		for (auto v : cnts) {
			int d = v.first;
			int c = v.second;
			if (d <= dd) {
				res = max(res, 1 + (d*(c+1)));
			}
		}

	}
	for (auto nv : G[v]) {
		if (nv == par) continue;
		st.erase(st.find(dp1[nv]+1));
		//0
		DP[nv][0] = (*st.rbegin());
		//1
		if (DP[v][1] != 0) DP[nv][1]=DP[v][1]+1;
		if (DP[v][0] != 0) DP[nv][1]=max(DP[nv][1],1+DP[v][0]);
		st.insert(dp1[nv]+1);
		dfs2(nv, v);
	}
}
int main() {
	cin >> N;
	for (int i = 0;i < N-1;i++) {
		int u, v;cin >> u >> v;
		u--;v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(0, 0);
	dfs2(0, 0);
	cout << res << endl;
}
0