結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー kibunakibuna
提出日時 2020-01-31 22:14:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,978 bytes
コンパイル時間 1,703 ms
コンパイル使用メモリ 172,088 KB
実行使用メモリ 5,300 KB
最終ジャッジ日時 2023-10-17 10:07:09
合計ジャッジ時間 3,079 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 9 ms
4,348 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 14 ms
4,348 KB
testcase_21 AC 21 ms
4,772 KB
testcase_22 AC 27 ms
5,036 KB
testcase_23 AC 26 ms
5,300 KB
testcase_24 AC 26 ms
5,300 KB
testcase_25 AC 27 ms
5,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

struct UnionFind {
    vector<int> rank, parent, size;

    // +1 for 1-indexed nodes
    UnionFind(int n) : rank(n + 1, 0), parent(n + 1), size(n + 1, 1) {
        iota(parent.begin(), parent.end(), 0); // parent is itself
    }
    int root(int x) {
        if (x != parent[x]) {
            parent[x] = root(parent[x]);
        }
        return parent[x];
    }
    bool isSame(int x, int y) { return root(x) == root(y); }
    bool unite(int x, int y) { return link(root(x), root(y)); }
    bool link(int x, int y) {
        if (x == y)
            return false;
        if (rank[x] > rank[y]) {
            parent[y] = x;
            size[x] += size[y];
        } else {
            parent[x] = y;
            size[y] += size[x];
            if (rank[x] == rank[y]) {
                rank[y]++;
            }
        }
        return true;
    }
    int getSize(int x) { return size[root(x)]; }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    lint n;
    cin >> n;
    UnionFind uf(n);
    vector<lint> deg(n, 0);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        uf.unite(u, v);
        deg[u]++;
        deg[v]++;
    }
    if (uf.getSize(0) == n) {
        cout << "Bob"
             << "\n";
    } else {
        bool ok = true;
        int idp = 0;
        int tot = 0;
        for (int i = 0; i < n; ++i) {
            if (deg[i] == 0)
                idp++;
            else if (deg[i] != 2)
                ok = false;
            else if (uf.getSize(i) != n - 1) {
                ok = false;
            }
            tot += deg[i];
        }
        if (ok && idp == 1) {
            assert(tot == 2 * (n - 1));
            cout << "Bob"
                 << "\n";
        } else {
            cout << "Alice"
                 << "\n";
        }
    }
    return 0;
}
0