結果

問題 No.763 Noelちゃんと木遊び
ユーザー polylogKpolylogK
提出日時 2018-02-25 01:18:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,169 bytes
コンパイル時間 3,174 ms
コンパイル使用メモリ 168,756 KB
実行使用メモリ 19,488 KB
最終ジャッジ日時 2023-10-24 22:16:04
合計ジャッジ時間 3,693 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
19,488 KB
testcase_01 AC 27 ms
7,664 KB
testcase_02 AC 61 ms
9,432 KB
testcase_03 AC 45 ms
8,444 KB
testcase_04 AC 32 ms
7,848 KB
testcase_05 AC 43 ms
8,380 KB
testcase_06 AC 80 ms
10,024 KB
testcase_07 AC 79 ms
9,912 KB
testcase_08 AC 47 ms
8,544 KB
testcase_09 AC 31 ms
7,796 KB
testcase_10 AC 13 ms
6,920 KB
testcase_11 AC 88 ms
10,084 KB
testcase_12 AC 73 ms
9,728 KB
testcase_13 AC 72 ms
9,672 KB
testcase_14 AC 66 ms
9,312 KB
testcase_15 AC 42 ms
8,412 KB
testcase_16 AC 9 ms
6,696 KB
testcase_17 AC 44 ms
8,428 KB
testcase_18 AC 79 ms
10,040 KB
testcase_19 AC 70 ms
9,764 KB
testcase_20 AC 72 ms
9,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define RREP(i,a,b) for(int i=(a);i>=(b);i--)
#define pq priority_queue
#define P pair<int,int>
#define P2 pair<int,P>
#define P3 pair<int,P2>
typedef long long ll; typedef long double ld;
using namespace std;
const int INF=1e9, MOD=1e9+7, around[]={0,1,1,-1,-1,0,-1,1,0,0};
const int vx[]={1,0,-1,0}, vy[]={0,1,0,-1};
const ll LINF=1e18;
const ld PI=abs(acos(-1));

vector<int> g[100010];
bool used[100010][2];
int dp[100010][2];
//dp[v][0] := vを根とする部分木で, vを消す場合
//dp[v][1] := vを根とする部分木で, vを消さない場合

int dfs(int v, int par, bool f){
	if(used[v][f]) return dp[v][f]; used[v][f] = true;
	
	int ret = f;
	if(f){
		for(auto e:g[v]){
			if(e == par) continue;
			ret += max(dfs(e, v, 0), dfs(e, v, 1) - 1);
		}
	}else{
		for(auto e:g[v]){
			if(e == par) continue;
			ret += max(dfs(e, v, 0), dfs(e, v, 1));
		}
	}

	return dp[v][f] = ret;
}

int main(){
	int n; cin >> n;
	REP(i,0,n - 1){
		int a, b; cin >> a >> b;
		g[a - 1].push_back(b - 1);
		g[b - 1].push_back(a - 1);
	}
	
	cout << max(dfs(0, -1, 0), dfs(0, -1, 1)) << endl;
	return 0;
}
0