結果

問題 No.763 Noelちゃんと木遊び
ユーザー nebukuro09nebukuro09
提出日時 2018-12-11 09:32:33
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 641 ms
コンパイル使用メモリ 109,836 KB
実行使用メモリ 13,976 KB
最終ジャッジ日時 2023-09-03 21:33:03
合計ジャッジ時間 3,630 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
13,976 KB
testcase_01 AC 45 ms
9,496 KB
testcase_02 AC 103 ms
12,672 KB
testcase_03 AC 70 ms
12,124 KB
testcase_04 AC 52 ms
10,612 KB
testcase_05 AC 69 ms
12,112 KB
testcase_06 AC 126 ms
13,204 KB
testcase_07 AC 123 ms
13,968 KB
testcase_08 AC 74 ms
13,924 KB
testcase_09 AC 48 ms
9,292 KB
testcase_10 AC 20 ms
4,384 KB
testcase_11 AC 130 ms
13,716 KB
testcase_12 AC 117 ms
13,440 KB
testcase_13 AC 114 ms
13,660 KB
testcase_14 AC 99 ms
13,192 KB
testcase_15 AC 70 ms
12,120 KB
testcase_16 AC 13 ms
4,376 KB
testcase_17 AC 71 ms
12,140 KB
testcase_18 AC 128 ms
12,856 KB
testcase_19 AC 120 ms
12,904 KB
testcase_20 AC 117 ms
13,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto G = new int[][](N);
    foreach (_; 0..N-1) {
        auto s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }

    int ans = 0;
    auto D = N.iota.map!(i => G[i].length.to!int).array;
    auto used = new bool[](N);

    DList!int queue;
    foreach (i; 0..N) if (D[i] == 1) queue.insertBack(i);

    while (!queue.empty) {
        int u = queue.front;
        queue.removeFront;

        if (used[u]) continue;
        used[u] = true;
        ans += 1;
        if (D[u] == 0) continue;

        int v = G[u].filter!(v => !used[v]).front;
        used[v] = true;

        foreach (w; G[v]) {
            if (used[w]) continue;
            D[w] -= 1;
            if (D[w] <= 1) queue.insertBack(w);
        }
    }

    ans.writeln;
}
0