結果

問題 No.1752 Up-Down Tree
ユーザー kabipoyokabipoyo
提出日時 2021-12-06 01:31:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,940 bytes
コンパイル時間 2,258 ms
コンパイル使用メモリ 216,624 KB
実行使用メモリ 35,052 KB
最終ジャッジ日時 2023-09-21 15:03:46
合計ジャッジ時間 7,676 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
35,052 KB
testcase_01 AC 105 ms
35,044 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 410 ms
23,280 KB
testcase_05 AC 229 ms
27,300 KB
testcase_06 AC 530 ms
19,696 KB
testcase_07 AC 473 ms
19,680 KB
testcase_08 AC 50 ms
6,984 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1 ms
4,380 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef int64_t lint;
#define rep(i, n) for(int i=0; i<n; i++)
#define repx(i, l, n) for(int i=l; i<n; i++)
#define all(v) v.begin(), v.end()
#define show(x) cout << #x << ": " << x << endl;
#define list(x) cout << #x << ": " << x << "  ";
#define pb push_back
using vi = vector<lint>;
using vvi = vector<vector<lint>>;
template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline void drop(T x) { cout << x << endl; exit(0); }
template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; }
constexpr lint LINF = LLONG_MAX/2;
using pint = pair<lint, priority_queue<lint>>;
using vp = vector<pint>;

vector<vector<int>> G;
void make_graph(int N) {
	G.resize(N);
	int x, y;
	rep(i, N-1) {
		cin >> x >> y;
		x--, y--;
		G[x].push_back(y);
		G[y].push_back(x);
	}
}

pint rec(int x, int from) {
	vi v;
	priority_queue<lint> q;
	lint y;
	for (auto t : G[x]) {
		if (t == from) continue;
		priority_queue<lint> p;
		tie(y, p) = rec(t, x);
		if (p.size() > q.size()) swap(p, q);
		while (!p.empty()) {
			q.push(p.top());
			p.pop();
		}
		v.pb(y);
	}
	if (v.size() == 0) return {1, q};
	sort(all(v), greater());
	rep(i, v.size()) {
		if (i <= 1) {
			if (v[i]%2 == 0) {
				v[i]--;
				q.push(1);
			}
		} else {
			q.push(v[i]);
		}
	}
	if (x == 0) {
		if (!q.empty()) {
			return {v[0]+v[1]+1+q.top(), q};
		}
	}
	if (v.size() == 1) {
		if (!q.empty()) {
			v.pb(q.top());
			q.pop();
			return {v[0]+v[1]+1, q};
		}
		else return {v[0]+1, q};
	}
	else return {v[0]+v[1]+1, q};
}

int main() {
	lint N;
	cin >> N;
	make_graph(N);
	cout << rec(0, -1).first << '\n';
}
0