結果

問題 No.763 Noelちゃんと木遊び
ユーザー nanaenanae
提出日時 2018-12-11 10:22:13
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 1,532 bytes
コンパイル時間 592 ms
コンパイル使用メモリ 93,488 KB
実行使用メモリ 34,036 KB
最終ジャッジ日時 2023-09-03 21:33:36
合計ジャッジ時間 3,434 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
34,036 KB
testcase_01 AC 35 ms
6,956 KB
testcase_02 AC 86 ms
10,564 KB
testcase_03 AC 57 ms
7,736 KB
testcase_04 AC 40 ms
5,460 KB
testcase_05 AC 54 ms
7,768 KB
testcase_06 AC 105 ms
11,808 KB
testcase_07 AC 103 ms
10,060 KB
testcase_08 AC 57 ms
7,736 KB
testcase_09 AC 38 ms
7,296 KB
testcase_10 AC 15 ms
4,376 KB
testcase_11 AC 110 ms
10,612 KB
testcase_12 AC 94 ms
10,340 KB
testcase_13 AC 96 ms
10,544 KB
testcase_14 AC 84 ms
9,288 KB
testcase_15 AC 56 ms
8,308 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 57 ms
7,488 KB
testcase_18 AC 109 ms
11,576 KB
testcase_19 AC 96 ms
11,136 KB
testcase_20 AC 96 ms
9,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum mod = 10^^9 + 7;

void main() {
    int n;
    scan(n);

    auto adj = new int[][](n, 0);

    foreach (i ; 0 .. n - 1) {
        int ui, vi;
        scan(ui, vi);
        ui--, vi--;
        adj[ui] ~= vi;
        adj[vi] ~= ui;
    }

    auto dp = new int[][](n, 2);
    fillAll(dp, -1);

    void dfs(int p, int v) {
        if (dp[v][0] != -1) {
            return;
        }
        int allDelete;
        dp[v][0] = 0;

        foreach (u ; adj[v]) {
            if (u == p) {
                continue;
            }
            dfs(v, u);
            allDelete += dp[u][0];
            dp[v][0] += max(dp[u][0], dp[u][1]);
        }

        dp[v][1] = max(dp[v][0], allDelete + 1);
    }

    dfs(-1, 0);

    debug {
        writefln("%(%s\n%)", dp);
    }

    writeln(max(dp[0][0], dp[0][1]));
}































void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
0