結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー nebukuro09
提出日時 2018-12-18 00:24:56
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 1,275 bytes
コンパイル時間 852 ms
コンパイル使用メモリ 118,400 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-06-13 02:23:07
合計ジャッジ時間 5,469 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
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;

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;
    }
    auto f = new bool[](N);
    auto g = new bool[](N);
    auto cnt = new int[](N);


    void dfs1(int n, int p) {
        f[n] = true;
        if (p != -1 && G[n].length == 1) {
            return;
        }
        foreach (m; G[n]) {
            if (m == p) continue;
            dfs1(m, n);
            if (f[m]) f[n] = false, cnt[n] += 1;
        }
    }

    void dfs2(int n, int p) {
        if (p == -1) {
            g[n] = f[n];
            foreach (m; G[n]) dfs2(m, n);
            return;
        }

        bool fp = g[p];
        if (!g[p] && f[n] && cnt[p] == 1) {
            fp = true;
        }
        if (fp) {
            cnt[n] += 1;
        }
        g[n] = cnt[n] == 0;

        foreach (m; G[n]) if (m != p) dfs2(m, n);
    }

    dfs1(0, -1);
    dfs2(0, -1);

    g.sum.writeln;
    N.iota.filter!(i => g[i]).each!(i => writeln(i+1));
}
0