結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー minatominato
提出日時 2020-02-01 14:01:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 3,196 bytes
コンパイル時間 2,771 ms
コンパイル使用メモリ 198,212 KB
実行使用メモリ 11,396 KB
最終ジャッジ日時 2023-10-18 23:59:02
合計ジャッジ時間 3,814 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 6 ms
4,348 KB
testcase_14 AC 6 ms
4,348 KB
testcase_15 AC 6 ms
4,348 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 7 ms
4,784 KB
testcase_18 AC 18 ms
5,672 KB
testcase_19 AC 18 ms
7,448 KB
testcase_20 AC 22 ms
6,128 KB
testcase_21 AC 40 ms
7,712 KB
testcase_22 AC 50 ms
8,768 KB
testcase_23 AC 49 ms
9,032 KB
testcase_24 AC 63 ms
11,396 KB
testcase_25 AC 50 ms
9,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
const long double PI = acos(-1.0L);
const long long MOD = 1000000007LL;
// const long long MOD = 998244353LL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////

struct UnionFind {
    vector<int> node;

    UnionFind() = default;
    UnionFind(int x) {init(x);}

    void init(int x) {
        node.assign(x,-1);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (node[x] > node[y]) swap(x,y);
        node[x] += node[y];
        node[y] = x;
        return true; 
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int root(int x) {
        if (node[x] < 0) return x;
        return node[x] = root(node[x]);
    }

    int size(int x) {
        return -node[root(x)];
    }
};

struct LowLink {
    const vector<vector<int>> &G;
    vector<bool> used;
    vector<int> ord,low,articulation;
    vector<pair<int, int>> bridge;

    LowLink (const vector<vector<int>> &G) : G(G) {}

    int dfs(int v, int k, int pv) {
        used[v] = true;
        ord[v] = k++;
        low[v] = ord[v];
        bool is_articulation = false;
        int cnt = 0;
        for (auto &nv : G[v]) {
            if (!used[nv]) {
                ++cnt;
                k = dfs(nv, k, v);
                low[v] = min(low[v],low[nv]);
                is_articulation |= ~pv && low[nv] >= ord[v];
                if (ord[v] < low[nv]) bridge.emplace_back(minmax(v, nv));
            } else if (nv != pv) {
                low[v] = min(low[v],ord[nv]);
            }
        }
        is_articulation |= pv == -1 && cnt > 1;
        if (is_articulation) articulation.emplace_back(v);
        return k;
    }

    virtual void build() {
        used.assign(G.size(), false);
        ord.assign(G.size(), 0);
        low.assign(G.size(), 0);
        int k = 0;
        for (int i = 0; i < G.size(); i++) {
            if (!used[i]) k = dfs(i, k, -1);
        }
    }
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    UnionFind uf(N);
    vector<vector<int>> G(N);
    rep(i,N-1) {
        int u,v; cin >> u >> v;
        uf.merge(u,v);
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    set<int> dic;
    rep(i,N) dic.emplace(uf.root(i));
    if (dic.size() == 1) {
        cout << "Bob" << endl;
    } else if (dic.size() >= 3) {
        cout << "Alice" << endl;
    } else {
        bool ok = true;
        rep(i,N) {
            if (uf.size(i) != 1 && uf.size(i) != N-1) ok = false;
        }
        LowLink LL(G);
        LL.build();
        if (LL.bridge.size()) ok = false;
        cout <<  (ok ? "Bob" : "Alice") << endl;
    }
}
0