結果

問題 No.763 Noelちゃんと木遊び
ユーザー Div9851KDiv9851K
提出日時 2019-07-17 10:36:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 770 bytes
コンパイル時間 2,013 ms
コンパイル使用メモリ 145,924 KB
実行使用メモリ 18,148 KB
最終ジャッジ日時 2023-08-22 19:02:55
合計ジャッジ時間 5,647 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
18,148 KB
testcase_01 AC 31 ms
8,356 KB
testcase_02 AC 72 ms
9,744 KB
testcase_03 AC 49 ms
8,956 KB
testcase_04 AC 35 ms
8,532 KB
testcase_05 AC 43 ms
8,912 KB
testcase_06 AC 80 ms
10,412 KB
testcase_07 AC 76 ms
10,164 KB
testcase_08 AC 48 ms
9,060 KB
testcase_09 AC 32 ms
8,552 KB
testcase_10 AC 15 ms
8,008 KB
testcase_11 AC 84 ms
10,452 KB
testcase_12 AC 77 ms
10,040 KB
testcase_13 AC 74 ms
9,892 KB
testcase_14 AC 64 ms
9,680 KB
testcase_15 AC 44 ms
8,992 KB
testcase_16 AC 11 ms
7,640 KB
testcase_17 AC 46 ms
9,036 KB
testcase_18 AC 86 ms
10,264 KB
testcase_19 AC 81 ms
9,960 KB
testcase_20 AC 77 ms
10,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
vector<int> G[100000];
ll memo[100000][2];
ll solve(int v, int f, int p) {
	if (memo[v][f] != -1) return memo[v][f];
	ll ret = 0;
	if (f == 0) {
		ret++;
		for (int to : G[v]) {
			if (to == p) continue;
			ret += max(solve(to, 0, v) - 1, solve(to, 1, v));
		}
	}
	else {
		for (int to : G[v]) {
			if (to == p) continue;
			ret += max(solve(to, 0, v), solve(to, 1, v));
		}
	}
	return memo[v][f] = ret;
}
 int main() {
	 int N;
	 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);
	 }
	 memset(memo, -1, sizeof(memo));
	 ll ans = max(solve(0, 0, -1), solve(0, 1, -1));
	 cout << ans << endl;
}
0