結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー misora192misora192
提出日時 2020-04-19 17:40:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,638 bytes
コンパイル時間 1,831 ms
コンパイル使用メモリ 171,296 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 18:37:31
合計ジャッジ時間 3,729 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,948 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 4 ms
6,940 KB
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 AC 4 ms
6,944 KB
testcase_18 AC 8 ms
6,944 KB
testcase_19 AC 9 ms
6,944 KB
testcase_20 AC 14 ms
6,940 KB
testcase_21 AC 23 ms
6,944 KB
testcase_22 AC 29 ms
6,940 KB
testcase_23 AC 25 ms
6,940 KB
testcase_24 AC 24 ms
6,944 KB
testcase_25 AC 26 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;

// union-find tree with size(struct)
struct UnionFind{
    vector<int> par; // 親ノード
    vector<int> rank; // ランク
	vector<int> size; // 連結成分のサイズ

    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n);
		rank.resize(n);
		size.resize(n);
        for (int i = 0; i < n; ++i){
			par[i] = i;
			rank[i] = 0;
			size[i] = 1;
		}
    }

    int find(int x) {
        if (par[x] == x)  return x;

        int r = find(par[x]);
        return par[x] = r;
    }

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

    bool unite(int x, int y) {
	    x = find(x);
		y = find(y);
	    if (x == y) return false;

	    if (rank[x] < rank[y]) swap(x, y);
	    if (rank[x] == rank[y]) ++rank[x];
	    par[y] = x;
		size[x] += size[y];
	    return true;
    }

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

	int getRank(int x){
		return rank[find(x)];
	}
};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int n;
	cin >> n;

	UnionFind uf(n);
	vector<int> sz(n, 0);
	rep(i, n-1){
		int u, v;
		cin >> u >> v;
		uf.unite(u, v);
		sz[u]++;
		sz[v]++;
	}

	set<int> pars;
	rep(i, n) pars.insert(uf.find(i));
	if(pars.size() == 1){
		cout << "Bob" << endl;
	}else if(pars.size() >= 3){
		cout << "Alice" << endl;
	}else{
		int zero = 0;
		bool two = true;
		rep(i, n){
			if(sz[i] == 0) zero++;
			else if(sz[i] != 2) two = false;
		}

		if(zero == 1 && two){
			cout << "Bob" << endl;
		}else{
			cout << "Alice" << endl;
		}
	}
}
0