結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー yukarinokiyukarinoki
提出日時 2020-01-31 23:27:49
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,679 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 66,972 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 12:47:03
合計ジャッジ時間 2,136 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
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 7 ms
4,348 KB
testcase_14 AC 7 ms
4,348 KB
testcase_15 AC 7 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 19 ms
4,348 KB
testcase_19 AC 19 ms
4,348 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 65 ms
4,348 KB
testcase_24 AC 63 ms
4,348 KB
testcase_25 AC 65 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct UnionFind {
    vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2

    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main(){
    int n; cin >> n;
    UnionFind tree(n);
    vector<int> v(n);
    
    for(int i=0;i<n-1;i++){
        int a,b; cin >> a >> b;
        tree.unite(a,b);
        v[a]++; v[b]++;
    }
    sort(v.begin(), v.end());
    if(v[0]==0){
        bool ff = true;
        for(int i=1;i<n;i++) if(v[i]!=2 && v[i]!=0) ff = false;
        if(ff){
            cout << "Bob" << endl;
            return 0;
        }
    }
    
    bool f = false;
    for(int i=1;i<n;i++){
        if(!tree.same(0,i)){
            f = true;
            break;
        }
    }
    
    if(f) cout << "Alice" << endl;
    else cout << "Bob" << endl;
    return 0;
}
0