結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー okok
提出日時 2020-01-31 21:47:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,998 bytes
コンパイル時間 794 ms
コンパイル使用メモリ 86,728 KB
実行使用メモリ 6,396 KB
最終ジャッジ日時 2023-10-17 09:10:09
合計ジャッジ時間 1,977 ms
ジャッジサーバーID
(参考情報)
judge14 / 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 1 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 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 7 ms
4,348 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 13 ms
4,812 KB
testcase_21 AC 20 ms
5,604 KB
testcase_22 AC 25 ms
6,132 KB
testcase_23 AC 26 ms
6,396 KB
testcase_24 AC 25 ms
6,396 KB
testcase_25 AC 26 ms
6,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

class union_find
{
	int  _setnum;
	vector<int> par, nume;
public:
	union_find(){
	}
	
	union_find(int x){
		par.resize(x);
		nume.resize(x);
		init();
	}
	
	~union_find(){
		//
		
	}
	
	void clear(){
		_setnum = 0;
		par.clear();
		nume.clear();
	}
	
	void init(){
		_setnum = par.size();
		for(int i = 0; i < par.size(); i++){
			par[i] = i;
			nume[i] = 1;
		}
	}
	
	void resize(int x){
		
		par.resize(x);
		nume.resize(x);
		init();
	}

	int find(int x){
		return par[x] == x ? x : par[x] = find(par[x]);
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
	
		if(x == y)return;
		
		_setnum--;
		
		if(nume[x] > nume[y]) std::swap(x,y);
		
		par[x] = y;
		nume[y] += nume[x];
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}
	
	int numel(int x){
		return nume[find(x)];
	}
	
	int size(){
		return par.size();
	}
	
	int setnum(){
		return _setnum;
	}
};


signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int N;
	union_find uf;
	vector<int> used;
	vector<int> con;
	
	int c = 0;
	cin>>N;
	
	uf.resize(N);
	used.resize(N);
	con.resize(N);
	
	for(int i = 0; i < N-1; i++){
		int u, v;
		
		cin>>u>>v;
		
		uf.unite(u,v);
		
		con[u]++;
		con[v]++;
	}
	
	for(int i = 0; i < N; i++){
		if(used[uf.find(i)]) continue;
		used[uf.find(i)] = true;
		
		if(uf.numel(i) == 1) c++;
	}
	
	if(c == 1 && uf.setnum() == 2){
		bool flag = true;
		for(int i = 0; i < N; i++){
			if(uf.numel(i) == 1) continue;
			if(con[i] != 2) flag = false;
		}
		
		if(flag) cout<<"Bob"<<endl;
		else cout<<"Alice"<<endl;
	}
	else if(uf.setnum() == 1) cout<<"Bob"<<endl;
	else cout<<"Alice"<<endl;
	
	
	return 0;
}
0