結果

問題 No.763 Noelちゃんと木遊び
ユーザー toyontoyon
提出日時 2020-07-09 10:42:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 948 bytes
コンパイル時間 1,523 ms
コンパイル使用メモリ 165,500 KB
実行使用メモリ 18,304 KB
最終ジャッジ日時 2024-04-16 02:25:31
合計ジャッジ時間 4,086 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
18,304 KB
testcase_01 AC 28 ms
7,808 KB
testcase_02 AC 64 ms
9,856 KB
testcase_03 AC 43 ms
8,576 KB
testcase_04 AC 31 ms
7,972 KB
testcase_05 AC 43 ms
8,576 KB
testcase_06 AC 80 ms
10,924 KB
testcase_07 AC 106 ms
10,752 KB
testcase_08 AC 45 ms
8,704 KB
testcase_09 AC 31 ms
7,748 KB
testcase_10 AC 13 ms
6,656 KB
testcase_11 AC 81 ms
10,880 KB
testcase_12 AC 75 ms
10,624 KB
testcase_13 AC 72 ms
10,368 KB
testcase_14 AC 66 ms
9,856 KB
testcase_15 AC 43 ms
8,576 KB
testcase_16 AC 10 ms
6,272 KB
testcase_17 AC 44 ms
8,704 KB
testcase_18 AC 77 ms
10,880 KB
testcase_19 AC 72 ms
10,368 KB
testcase_20 AC 72 ms
10,624 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];
ll dp[100100][2];
// 0: 削除
// 1: 使う

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

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

        // 戻りがけ

        // 使う場合
        dp[cur][1] += max(dp[to][0], dp[to][1] - 1);

        // 使わない場合
        // 子の大きい方を選ぶ
        dp[cur][0] += 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