結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー VvyLwVvyLw
提出日時 2024-06-20 18:39:45
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,813 bytes
コンパイル時間 3,782 ms
コンパイル使用メモリ 142,744 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-20 18:39:51
合計ジャッジ時間 5,527 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,816 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 6 ms
6,944 KB
testcase_14 AC 6 ms
6,944 KB
testcase_15 AC 6 ms
6,948 KB
testcase_16 AC 5 ms
6,944 KB
testcase_17 AC 5 ms
6,940 KB
testcase_18 AC 16 ms
6,944 KB
testcase_19 AC 17 ms
6,944 KB
testcase_20 AC 25 ms
6,940 KB
testcase_21 AC 40 ms
6,940 KB
testcase_22 AC 51 ms
6,944 KB
testcase_23 AC 53 ms
6,944 KB
testcase_24 AC 52 ms
6,944 KB
testcase_25 AC 53 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio: readf, readln, writeln;
import std.conv: to;
import std.string: chomp;

void main() {
    const n = readln.chomp.to!int;
    auto uf = new UnionFind(n);
    auto cnt = new int[n];
    bool gg = true;
    foreach(_; 0..n - 1) {
        int u, v;
        readf("%d %d\n", u, v);
        gg &= uf.unite(u, v);
        cnt[u]++;
        cnt[v]++;
    }
    if(gg) {
        writeln("Bob");
        return;
    }
    foreach(e; cnt) {
        if(e >= 1 && e != 2) {
            writeln("Alice");
            return;
        }
    }
    writeln(gg || uf.size(0) == n - 1 || uf.size(1) == n - 1 ? "Bob" : "Alice");
}

class UnionFind {
import std.algorithm: swap, filter;
import std.array: array;
import std.range;
private:
    int n;
    int[] par;
public:
    this(const int n) {
        this.n = n;
        par = new int[n];
        par[] = -1;
    }
    int length() const { return n; }
    ref auto opIndex(int i) {
        while(par[i] >= 0) {
            const p = par[par[i]];
            if(p < 0) {
                return par[i];
            }
            i = par[i] = p;
        }
        return i;
    }
    int size(const int i){ return -par[this[i]]; }
    bool unite(int i, int j) {
        i = this[i];
        j = this[j];
        if(i == j) {
            return false;
        }
        if(i > j) {
            swap(i, j);
        }
        par[i] += par[j];
        par[j] = i;
        return true;
    }
    int[][] groups() {
        int[][] res = new int[][n];
        foreach(i; 0..n) {
            res[this[i]] ~= i;
        }
        return res.filter!(a => !a.empty).array;
    }
}

bool isBipartite(UnionFind uf) {
    assert(uf.length % 2 == 0);
    const n = uf.length / 2;
    bool ok = true;
    foreach(i; 0..n) {
        ok &= uf[i] != uf[i + n];
    }
    return ok;
}
0