結果

問題 No.763 Noelちゃんと木遊び
ユーザー nanophoto12nanophoto12
提出日時 2021-11-18 22:51:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,500 bytes
コンパイル時間 4,481 ms
コンパイル使用メモリ 263,648 KB
実行使用メモリ 19,408 KB
最終ジャッジ日時 2023-08-27 17:30:46
合計ジャッジ時間 6,934 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
19,408 KB
testcase_01 AC 26 ms
5,404 KB
testcase_02 AC 59 ms
8,644 KB
testcase_03 AC 40 ms
6,776 KB
testcase_04 AC 29 ms
5,684 KB
testcase_05 AC 39 ms
6,920 KB
testcase_06 AC 72 ms
9,652 KB
testcase_07 AC 69 ms
9,524 KB
testcase_08 AC 42 ms
7,032 KB
testcase_09 AC 28 ms
5,632 KB
testcase_10 AC 12 ms
4,380 KB
testcase_11 AC 74 ms
9,952 KB
testcase_12 AC 66 ms
9,156 KB
testcase_13 AC 64 ms
9,408 KB
testcase_14 AC 56 ms
8,320 KB
testcase_15 AC 40 ms
6,780 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 40 ms
7,032 KB
testcase_18 AC 73 ms
10,000 KB
testcase_19 AC 67 ms
9,060 KB
testcase_20 AC 66 ms
9,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)

template<typename T>
static inline void chmin(T& ref, const T  value) {
	if (ref > value) ref = value;
}

template<typename T>
static inline void chmax(T& ref, const T value) {
	if (ref < value) ref = value;
}

#include <atcoder/all>
using namespace atcoder;
typedef modint1000000007 mint;

int main() {
	ll n;
	cin >> n;
	vector<vector<int>> g(n);
	rep(i, n - 1) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector<P> dp(n, P{ -1,-1 });
	function<P(int, int)> dfs = [&](int current, int parent) {
		if (dp[current].first != -1) {
			return dp[current];
		}
		int cs = 0;
		for (auto x : g[current]) {
			if (x == parent) continue;
			cs++;;
		}
		if (cs == 0) {
			dp[current] = P{ 0, 1 };
			return dp[current];
		}
		ll cut = 0;
		for (auto x : g[current]) {
			if (x == parent) continue;
			P p = dfs(x, current);
			cut += max(p.first, p.second);
		}
		ll notCut = 1;
		for (auto x : g[current]) {
			if (x == parent) continue;
			P p = dfs(x, current);
			notCut += max(p.first, p.second - 1);
		}
		dp[current] = { cut, notCut };
		return dp[current];
	};
	P v = dfs(0, -1);
	cout << max(v.first, v.second) << endl;
	return 0;
}
0