結果

問題 No.763 Noelちゃんと木遊び
ユーザー peroonperoon
提出日時 2019-04-11 16:25:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,711 bytes
コンパイル時間 900 ms
コンパイル使用メモリ 101,256 KB
実行使用メモリ 16,300 KB
最終ジャッジ日時 2023-09-25 20:03:32
合計ジャッジ時間 3,858 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
16,300 KB
testcase_01 AC 14 ms
5,748 KB
testcase_02 AC 33 ms
8,788 KB
testcase_03 AC 22 ms
6,996 KB
testcase_04 AC 17 ms
5,908 KB
testcase_05 AC 22 ms
7,012 KB
testcase_06 AC 42 ms
10,108 KB
testcase_07 AC 40 ms
9,972 KB
testcase_08 AC 24 ms
7,260 KB
testcase_09 AC 17 ms
5,952 KB
testcase_10 AC 7 ms
4,500 KB
testcase_11 AC 46 ms
10,368 KB
testcase_12 AC 39 ms
9,588 KB
testcase_13 AC 38 ms
9,324 KB
testcase_14 AC 34 ms
8,860 KB
testcase_15 AC 23 ms
6,964 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 23 ms
7,068 KB
testcase_18 AC 44 ms
10,176 KB
testcase_19 AC 38 ms
9,760 KB
testcase_20 AC 38 ms
9,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<complex>
#include<ctype.h>
#include<iomanip>
#include<iostream>
#include<fstream>
#include<map>
#include<math.h>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<string>
#include<vector>

using namespace std;
typedef long long ll;

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}

const int N_MAX = 100010;
vector<vector<ll> > G;
ll dp[N_MAX][2]; 
// dp[i][0] : iを削除する場合の最大値
// dp[i][0] : iを削除しない場合の最大値

// dfs(i)を呼ぶと
// dp[i][0]
// dp[i][1]
// が埋まるとする
void dfs(ll i, ll parent){
    // leaf
    if(G[i].size()==1 && G[i][0]==parent){
        dp[i][0] = 0;
        dp[i][1] = 1;
        return;
    }

    dp[i][0] = 0;
    dp[i][1] = 1;
    
    for(auto to : G[i]){
        if(to==parent) continue;
        dfs(to, i);

        dp[i][0] += max(dp[to][0], dp[to][1]);
        dp[i][1] += max(dp[to][0], dp[to][1]-1);
    }
}

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

    // input
    ll N;
    cin >> N;

    G.resize(N);

    FOR(i, 0, N-1){
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    dfs(0, -1);

    ll ans = max(dp[0][0], dp[0][1]);
    p(ans);

    return 0;
}
0