結果

問題 No.763 Noelちゃんと木遊び
ユーザー toyontoyon
提出日時 2020-03-15 08:47:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 934 bytes
コンパイル時間 1,561 ms
コンパイル使用メモリ 170,096 KB
実行使用メモリ 17,764 KB
最終ジャッジ日時 2023-08-16 02:27:39
合計ジャッジ時間 4,322 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
17,764 KB
testcase_01 AC 27 ms
5,592 KB
testcase_02 AC 71 ms
8,820 KB
testcase_03 AC 46 ms
7,096 KB
testcase_04 AC 32 ms
5,908 KB
testcase_05 AC 44 ms
6,920 KB
testcase_06 AC 89 ms
10,440 KB
testcase_07 AC 83 ms
9,968 KB
testcase_08 AC 48 ms
7,256 KB
testcase_09 AC 31 ms
5,916 KB
testcase_10 AC 12 ms
4,376 KB
testcase_11 AC 90 ms
10,524 KB
testcase_12 AC 78 ms
9,736 KB
testcase_13 AC 80 ms
9,292 KB
testcase_14 AC 64 ms
8,784 KB
testcase_15 AC 42 ms
7,120 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 46 ms
7,124 KB
testcase_18 AC 86 ms
10,164 KB
testcase_19 AC 76 ms
9,736 KB
testcase_20 AC 78 ms
9,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
// const int INF = 1e8;
const ll INF = 1LL << 60;
typedef pair<int, int> P;

ll N;
vector<vector<ll>> G;
ll dp[100100][2];
// dp[v][erase] 
// erase 0: 消さない 1: 消す

void dfs(ll v, ll par = -1) {
    dp[v][0] = 1; dp[v][1] = 0; 
    for (ll i = 0; i < G[v].size(); i++) {
        ll next = G[v][i];
        if (next != par) {
            dfs(next, v);
            // 消さない
            dp[v][0] += max(dp[next][0] - 1, dp[next][1]);

            // 消す
            dp[v][1] += max(dp[next][0], dp[next][1]);
        }
    }
}

int main() {
    cin >> N;
    G.resize(N);

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

    dfs(0);

    cout << max(dp[0][0], dp[0][1]) << endl;
}
0