結果

問題 No.763 Noelちゃんと木遊び
ユーザー t98slidert98slider
提出日時 2022-12-24 01:18:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 708 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 171,004 KB
実行使用メモリ 17,016 KB
最終ジャッジ日時 2023-08-11 12:19:23
合計ジャッジ時間 4,508 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
17,016 KB
testcase_01 AC 14 ms
5,172 KB
testcase_02 AC 31 ms
8,008 KB
testcase_03 AC 22 ms
6,424 KB
testcase_04 AC 16 ms
5,468 KB
testcase_05 AC 21 ms
6,296 KB
testcase_06 AC 38 ms
9,108 KB
testcase_07 AC 37 ms
8,896 KB
testcase_08 AC 23 ms
6,492 KB
testcase_09 AC 15 ms
5,500 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 40 ms
9,064 KB
testcase_12 AC 38 ms
8,296 KB
testcase_13 AC 37 ms
8,500 KB
testcase_14 AC 31 ms
7,728 KB
testcase_15 AC 21 ms
6,456 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 22 ms
6,532 KB
testcase_18 AC 38 ms
9,060 KB
testcase_19 AC 34 ms
8,524 KB
testcase_20 AC 34 ms
8,568 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, {-1, -1});
    function<void(int,int)> dfs = [&](int v, int p){
        dp[v][0] = 0;
        dp[v][1] = 1;
        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