結果

問題 No.763 Noelちゃんと木遊び
ユーザー t98slidert98slider
提出日時 2022-12-24 01:19:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 662 bytes
コンパイル時間 1,489 ms
コンパイル使用メモリ 172,380 KB
実行使用メモリ 17,044 KB
最終ジャッジ日時 2023-08-11 12:19:27
合計ジャッジ時間 3,234 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
17,044 KB
testcase_01 AC 14 ms
5,148 KB
testcase_02 AC 31 ms
8,120 KB
testcase_03 AC 20 ms
6,716 KB
testcase_04 AC 15 ms
5,644 KB
testcase_05 AC 21 ms
6,460 KB
testcase_06 AC 39 ms
9,160 KB
testcase_07 AC 37 ms
8,788 KB
testcase_08 AC 23 ms
6,416 KB
testcase_09 AC 15 ms
5,380 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 39 ms
9,092 KB
testcase_12 AC 35 ms
8,264 KB
testcase_13 AC 35 ms
8,268 KB
testcase_14 AC 32 ms
8,044 KB
testcase_15 AC 21 ms
6,440 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 20 ms
6,484 KB
testcase_18 AC 38 ms
9,092 KB
testcase_19 AC 35 ms
8,736 KB
testcase_20 AC 34 ms
8,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, u, v;
    cin >> n;
    vector<vector<int>> g(n);
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<array<int,2>> dp(n, {0, 1});
    function<void(int,int)> dfs = [&](int v, int p){
        for(auto &&u:g[v]){
            if(u == p)continue;
            dfs(u, v);
            dp[v][0] += max(dp[u][0], dp[u][1]);
            dp[v][1] += dp[u][0];
        }
    };
    dfs(0, -1);
    cout << max(dp[0][0], dp[0][1]) << '\n';
}
0