結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー nebukuro09nebukuro09
提出日時 2018-12-18 00:24:56
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 1,275 bytes
コンパイル時間 2,018 ms
コンパイル使用メモリ 104,740 KB
実行使用メモリ 13,976 KB
最終ジャッジ日時 2023-09-03 21:43:43
合計ジャッジ時間 5,755 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 96 ms
12,392 KB
testcase_08 AC 48 ms
9,268 KB
testcase_09 AC 57 ms
8,760 KB
testcase_10 AC 42 ms
5,952 KB
testcase_11 AC 155 ms
13,412 KB
testcase_12 AC 161 ms
13,444 KB
testcase_13 AC 150 ms
13,188 KB
testcase_14 AC 149 ms
13,220 KB
testcase_15 AC 161 ms
13,148 KB
testcase_16 AC 164 ms
13,924 KB
testcase_17 AC 165 ms
13,680 KB
testcase_18 AC 146 ms
13,976 KB
testcase_19 AC 147 ms
13,960 KB
testcase_20 AC 147 ms
13,972 KB
testcase_21 AC 139 ms
13,396 KB
20evil_special_uni1.txt AC 153 ms
13,436 KB
20evil_special_uni2.txt AC 145 ms
14,432 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;

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