結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー a0171610a0171610
提出日時 2020-05-18 22:30:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,455 bytes
コンパイル時間 1,591 ms
コンパイル使用メモリ 171,488 KB
実行使用メモリ 11,328 KB
最終ジャッジ日時 2024-04-09 21:39:13
合計ジャッジ時間 2,841 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 5 ms
6,940 KB
testcase_14 AC 5 ms
6,944 KB
testcase_15 AC 5 ms
6,940 KB
testcase_16 AC 5 ms
6,940 KB
testcase_17 AC 6 ms
6,940 KB
testcase_18 AC 13 ms
6,940 KB
testcase_19 AC 13 ms
6,940 KB
testcase_20 AC 20 ms
7,296 KB
testcase_21 AC 34 ms
9,216 KB
testcase_22 AC 44 ms
10,880 KB
testcase_23 AC 42 ms
11,328 KB
testcase_24 AC 46 ms
11,264 KB
testcase_25 AC 45 ms
11,264 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:65:11: warning: 'b' may be used uninitialized [-Wmaybe-uninitialized]
   65 |           if(a < b) swap(a, b);
      |           ^~
main.cpp:58:23: note: 'b' was declared here
   58 |           int a = -1, b;
      |                       ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;

struct UnionFind{
	vector<lint> rnk,par,num;
	UnionFind(lint N) : rnk(N),par(N),num(N){
		init();
	}
	void init(){
		for(lint i = 0;i < rnk.size();i++){
			rnk[i] = 0;
			par[i] = i;
			num[i] = 1;
		}
	}
	lint find(lint x){
		if(par[x] == x) return x;
		return par[x] = find(par[x]);
	}
	void unite(lint x,lint y){
		x = find(x);
		y = find(y);
		if(x == y) return;
		if(rnk[x] < rnk[y]){
			par[x] = y;
			num[y] += num[x];
		}
		else{
			par[y] = x;
			num[x] += num[y];
			if(rnk[x] == rnk[y]) rnk[x]++;
		}
	}
	bool same(lint x,lint y){
		return (find(x) == find(y));
	}
	lint size(lint x){
		return num[find(x)];
	}
};

signed main(){
	int n; cin >> n;
	UnionFind tree(n);
	vector<vector<int> > G(n);
	for(int i = 1; i < n; i++){
		int a, b; scanf("%d%d", &a, &b);
		tree.unite(a, b);
		G[a].push_back(b); G[b].push_back(a);
	}
	int c = 0;
	for(int i = 0; i < n; i++) if(tree.find(i) == i) c++;
	string ans;
	if(c == 1) ans = "Bob";
	if(c > 2) ans = "Alice";
	if(c == 2){
	  int a = -1, b;
	  for(int i = 0; i < n; i++){
		  if(tree.find(i) == i){
			  if(a < 0) a = tree.size(i);
			  else b = tree.size(i);
		  }
	  }
	  if(a < b) swap(a, b);
	  if(a == n - 1 && b == 1){
		  bool ok = true;
		  for(int i = 0; i < n; i++) if(G[i].size() != 2 && G[i].size() != 0) ok = false;
		  if(ok) ans = "Bob";
		  else ans = "Alice";
		}
		else ans = "Alice";
	}
	cout << ans << endl;
}
0