結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー finefine
提出日時 2020-01-31 21:42:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 2,190 bytes
コンパイル時間 1,859 ms
コンパイル使用メモリ 179,428 KB
実行使用メモリ 11,032 KB
最終ジャッジ日時 2023-10-17 09:03:36
合計ジャッジ時間 3,392 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 5 ms
4,348 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 6 ms
5,036 KB
testcase_18 AC 16 ms
5,680 KB
testcase_19 AC 18 ms
8,264 KB
testcase_20 AC 24 ms
7,468 KB
testcase_21 AC 38 ms
8,256 KB
testcase_22 AC 48 ms
9,576 KB
testcase_23 AC 67 ms
11,032 KB
testcase_24 AC 65 ms
11,016 KB
testcase_25 AC 71 ms
11,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

typedef pair<int, int> P;

vector< vector<int> > G;
vector<int> ord, low;
vector<P> bridge;

void dfs(int cur, int par, int& t) {
    ord[cur] = ++t;
    low[cur] = ord[cur];
    for (int nex : G[cur]) {
        if (nex == par) continue;
        if (ord[nex] == 0) {
            dfs(nex, cur, t);
            low[cur] = min(low[cur], low[nex]);
            if (ord[cur] < low[nex]) bridge.push_back(minmax(cur, nex));
        } else {
            low[cur] = min(low[cur], ord[nex]);
        }
    }
}

void bridges(int v) {
    ord.resize(v, 0);
    low.resize(v);
    int t = 0;
    dfs(0, -1, t);
}

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

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

    int size(int x) {
        return -data[find(x)];
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int n;
    cin >> n;
    G.resize(n);

    UnionFind uf(n);
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
        uf.unite(u, v);
    }

    bridges(n);
    
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        cnt += (uf.find(i) == i);
    }

    if (bridge.size() > 1) {
        cnt++;
    }
    if (cnt > 1) --cnt;
    cout << (cnt == 1 ? "Bob" : "Alice") << endl;
    return 0;
}
0