結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー firiexp
提出日時 2020-01-31 21:34:47
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,758 bytes
コンパイル時間 888 ms
コンパイル使用メモリ 99,792 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-02 03:32:13
合計ジャッジ時間 2,561 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>
#include <limits>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

class UnionFind {
    int n;
    vector<int> uni;
    int forest_size;
public:
    explicit UnionFind(int n) : n(n), uni(static_cast<u32>(n), -1), forest_size(n) {};

    int root(int a){
        if (uni[a] < 0) return a;
        else return (uni[a] = root(uni[a]));
    }

    bool unite(int a, int b) {
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(uni[a] > uni[b]) swap(a, b);
        uni[a] += uni[b];
        uni[b] = a;
        forest_size--;
        return true;
    }
    int size(){ return forest_size; }
    int size(int i){ return -uni[root(i)]; }
    bool same(int a, int b) { return root(a) == root(b); }
};

int main() {
    int n;
    cin >> n;
    UnionFind uf(n);
    vector<int> as, bs;
    for (int i = 0; i < n-1; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        uf.unite(a, b);
        as.emplace_back(a);
        bs.emplace_back(b);
    }
    if(uf.size() == 1){
        puts("Bob");
        return 0;
    }else if(uf.size() >= 3){
        puts("Alice");
        return 0;
    }

    vector<int> v(n);
    for (int i = 0; i < n-1; ++i) {
        v[as[i]]++;
        v[bs[i]]++;
    }
    for (int i = 0; i < n; ++i) {
        if(uf.size(i) != 1 && v[i] < 2){
            puts("Alice");
            return 0;
        }
    }
    puts("Bob");
    return 0;
}
0