結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー totori_nyaatotori_nyaa
提出日時 2020-01-31 21:33:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,376 bytes
コンパイル時間 1,752 ms
コンパイル使用メモリ 174,428 KB
実行使用メモリ 9,032 KB
最終ジャッジ日時 2023-10-17 08:51:31
合計ジャッジ時間 3,076 ms
ジャッジサーバーID
(参考情報)
judge15 / 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 5 ms
4,348 KB
testcase_14 AC 5 ms
4,348 KB
testcase_15 AC 5 ms
4,348 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 5 ms
4,348 KB
testcase_18 AC 13 ms
5,072 KB
testcase_19 AC 14 ms
5,072 KB
testcase_20 AC 21 ms
6,128 KB
testcase_21 AC 34 ms
7,712 KB
testcase_22 AC 49 ms
8,768 KB
testcase_23 AC 51 ms
9,032 KB
testcase_24 AC 46 ms
9,032 KB
testcase_25 AC 53 ms
9,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

class UnionFind{
    public:
    //親の番号を格納する。親だった場合は-(その集合のサイズ)
    vector<int> parent;

    UnionFind(int N){
        parent = vector<int>(N,-1);
    }

    int root(int A){
        if(parent[A] < 0) return A;
        return parent[A]=root(parent[A]);
    }

    int size(int A){
        return -parent[root(A)];
    }

    bool unite(int A, int B) {
        A = root(A), B = root(B);
        if(A == B) return false; 

        if(size(A) < size(B)) swap(A,B);
        parent[A] += parent[B];
        parent[B] = A;
        return true;
    }

    bool same(int A, int B){
        return root(A)==root(B);
    } 
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    ll n;
    cin>>n;
    UnionFind uni(n);
    vector<vector<int>> v(n);
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
        uni.unite(a,b);
    }
    bool bob = false;
    if(uni.size(0)==n) bob = true;
    if(uni.size(0)==0 || uni.size(0)==n-1){
        int cnt = 0;
        for(int i=0;i<n;i++){
            if(v[i].size()!=2) cnt++;
        }
        if(cnt==1) bob=1;
    }
    if(bob){
        cout << "Bob" << endl;
    }
    else cout << "Alice" << endl;
}
0