結果

問題 No.1752 Up-Down Tree
コンテスト
ユーザー kabipoyo
提出日時 2021-11-19 22:30:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,633 bytes
コンパイル時間 2,149 ms
コンパイル使用メモリ 204,780 KB
最終ジャッジ日時 2025-01-25 20:33:59
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 12 WA * 8
権限があれば一括ダウンロードができます

ソースコード

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, 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;
	lint y, z, a=0;
	for (auto t : G[x]) {
		if (t == from) continue;
		tie(y, z) = rec(t, x);
		a += y+z;
		if (y%2 == 0) y--;
		v.pb(y);
	}
	if (v.size() == 0) return {1, 0};
	sort(all(v), greater());
	if (v.size() == 1) {
		if (a-v[0]) return {v[0]+2, a-v[0]-1};
		else return {v[0]+1, a-v[0]};
	}
	else return {v[0]+v[1]+1, a-v[0]-v[1]};
}

int main() {
	lint N;
	cin >> N;
	make_graph(N);

	lint a=0, b=0, c=0, x, y, z;
	tie(x, y) = rec(0, -1);
	std::cout << x << '\n';
}
0