結果

問題 No.763 Noelちゃんと木遊び
ユーザー toyontoyon
提出日時 2020-07-05 12:24:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 1,075 bytes
コンパイル時間 1,564 ms
コンパイル使用メモリ 169,892 KB
実行使用メモリ 18,556 KB
最終ジャッジ日時 2023-10-22 07:07:03
合計ジャッジ時間 5,123 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
18,556 KB
testcase_01 AC 29 ms
8,724 KB
testcase_02 AC 71 ms
10,512 KB
testcase_03 AC 46 ms
9,600 KB
testcase_04 AC 34 ms
8,964 KB
testcase_05 AC 45 ms
9,536 KB
testcase_06 AC 86 ms
11,124 KB
testcase_07 AC 83 ms
11,000 KB
testcase_08 AC 49 ms
9,688 KB
testcase_09 AC 33 ms
8,900 KB
testcase_10 AC 14 ms
7,740 KB
testcase_11 AC 90 ms
11,196 KB
testcase_12 AC 78 ms
10,796 KB
testcase_13 AC 76 ms
10,740 KB
testcase_14 AC 66 ms
10,404 KB
testcase_15 AC 46 ms
9,568 KB
testcase_16 AC 9 ms
7,448 KB
testcase_17 AC 45 ms
9,584 KB
testcase_18 AC 90 ms
11,144 KB
testcase_19 AC 79 ms
10,824 KB
testcase_20 AC 79 ms
10,836 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;
typedef pair<int, int> P;

const ll INF = 1LL << 60;

ll N;
vector<ll> g[100100];

// i: 何個目の頂点か
// j: その頂点を消すかどうか
ll dp[100100][2];

void dfs(int cur, int par = -1) {
    dp[cur][0] = 1;
    dp[cur][1] = 0;

    for (int i = 0; i < g[cur].size(); i++) {
        int to = g[cur][i];
        if (to == par) continue;

        dfs(to, cur);


        // cur を消さない場合
        // cur も to も消えない場合、連結するので頂点が -1 される
        dp[cur][0] += max(dp[to][0] - 1, dp[to][1]);

        // cur を消した場合
        // 子の要素数をとればOK
        dp[cur][1] += max(dp[to][0], dp[to][1]);
    }
}

int main() {
    cin >> 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);

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