結果

問題 No.763 Noelちゃんと木遊び
ユーザー akun0716akun0716
提出日時 2020-01-05 17:13:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,710 bytes
コンパイル時間 778 ms
コンパイル使用メモリ 83,508 KB
実行使用メモリ 19,748 KB
最終ジャッジ日時 2023-08-14 22:03:45
合計ジャッジ時間 2,875 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
19,748 KB
testcase_01 AC 17 ms
7,480 KB
testcase_02 AC 35 ms
9,828 KB
testcase_03 AC 24 ms
8,516 KB
testcase_04 AC 18 ms
7,928 KB
testcase_05 AC 23 ms
8,412 KB
testcase_06 AC 44 ms
10,884 KB
testcase_07 AC 40 ms
10,540 KB
testcase_08 AC 25 ms
8,720 KB
testcase_09 AC 17 ms
7,728 KB
testcase_10 AC 8 ms
6,484 KB
testcase_11 AC 43 ms
10,764 KB
testcase_12 AC 37 ms
10,228 KB
testcase_13 AC 36 ms
10,140 KB
testcase_14 AC 32 ms
9,784 KB
testcase_15 AC 23 ms
8,684 KB
testcase_16 AC 7 ms
6,268 KB
testcase_17 AC 24 ms
8,492 KB
testcase_18 AC 43 ms
10,912 KB
testcase_19 AC 38 ms
10,296 KB
testcase_20 AC 40 ms
10,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;

int N;
VI G[100010];
int dp[100010][2];

int dfs(int cu, int pa,int co) {

	if (dp[cu][co] != -1)return dp[cu][co];

	if (co == 0)dp[cu][0] = 0;
	else dp[cu][1] = 1;

	REP(i,G[cu].size()){
		int ch = G[cu][i];
		if (ch == pa) continue;

		if (co == 0) {
			dp[cu][0] += max(dfs(ch, cu, 0), dfs(ch, cu, 1));
		}
		else {
			dp[cu][1] += max(dfs(ch, cu, 0), dfs(ch, cu, 1) - 1);
		}
	}

	return dp[cu][co];
}


signed main(){
cin.tie(0);
ios::sync_with_stdio(false);

	
	cin >> N;
	REP(i, N)dp[i][0] = dp[i][1] = -1;
	REP(i, N - 1) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	
	cout << max(dfs(0,-1,0) ,dfs(0,-1,1)) << endl;

	//REP(i, N)cout << dp[i][0] << dp[i][1] << endl;

	return 0;
}

0