結果

問題 No.763 Noelちゃんと木遊び
ユーザー oevloevl
提出日時 2019-08-16 05:06:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 673 bytes
コンパイル時間 1,686 ms
コンパイル使用メモリ 172,472 KB
実行使用メモリ 15,684 KB
最終ジャッジ日時 2023-10-19 23:32:31
合計ジャッジ時間 4,815 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
15,684 KB
testcase_01 AC 29 ms
5,388 KB
testcase_02 AC 72 ms
8,292 KB
testcase_03 AC 48 ms
6,708 KB
testcase_04 AC 33 ms
5,652 KB
testcase_05 AC 47 ms
6,444 KB
testcase_06 AC 91 ms
9,348 KB
testcase_07 AC 85 ms
9,084 KB
testcase_08 AC 49 ms
6,708 KB
testcase_09 AC 31 ms
5,652 KB
testcase_10 AC 13 ms
4,348 KB
testcase_11 AC 93 ms
9,348 KB
testcase_12 AC 82 ms
8,820 KB
testcase_13 AC 81 ms
8,556 KB
testcase_14 AC 72 ms
8,028 KB
testcase_15 AC 50 ms
6,708 KB
testcase_16 AC 9 ms
4,348 KB
testcase_17 AC 48 ms
6,708 KB
testcase_18 AC 93 ms
9,348 KB
testcase_19 AC 82 ms
8,820 KB
testcase_20 AC 84 ms
8,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)

int N;
vector<vector<int>> G;
int dp[101010][2];

void dfs(int cur, int pre = -1) {
    dp[cur][0] = 1;
    dp[cur][1] = 0;
    for(auto &to : G[cur]) {
        if(to == pre) continue;
        dfs(to, cur);
        dp[cur][0] += max(dp[to][0] - 1, dp[to][1]);
        dp[cur][1] += max(dp[to][0], dp[to][1]);
    }
}

int main() {
    cin >> N;
    G.resize(N);
    rep(i, 0, N - 1) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(0);
    cout << max(dp[0][0], dp[0][1]) << endl;
    return 0;
}
0