結果

問題 No.763 Noelちゃんと木遊び
ユーザー nebukuro09
提出日時 2018-12-11 09:32:33
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 1,016 ms
コンパイル使用メモリ 106,624 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2025-03-22 10:33:08
合計ジャッジ時間 4,310 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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