結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー forest3forest3
提出日時 2020-07-22 10:25:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,057 bytes
コンパイル時間 1,585 ms
コンパイル使用メモリ 171,120 KB
実行使用メモリ 8,964 KB
最終ジャッジ日時 2023-09-01 22:05:27
合計ジャッジ時間 4,476 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 9 ms
4,376 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 9 ms
4,380 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 23 ms
5,004 KB
testcase_19 AC 23 ms
4,956 KB
testcase_20 AC 38 ms
5,900 KB
testcase_21 AC 62 ms
7,516 KB
testcase_22 AC 82 ms
8,700 KB
testcase_23 AC 87 ms
8,964 KB
testcase_24 AC 82 ms
8,796 KB
testcase_25 AC 84 ms
8,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind {
  vector< int > data;
 
  UnionFind(int sz) {
    data.assign(sz, -1);
  }
 
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }
 
  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }
 
  int size(int k) {
    return (-data[find(k)]);
  }
};

int main()
{
	int N;
	cin >> N;
	vector<vector<int>> g( N );
	for( int i = 0; i < N - 1; i++ ) {
		int u, v;
		cin >> u >> v;
		g[u].push_back( v );
		g[v].push_back( u );
	}

	UnionFind uf( N );
	for( int i = 0; i < N; i++ ) {
		for( int j : g[i] ) {
			uf.unite( i, j );
		}
	}
	string ans = "Alice";
	if( uf.size( 0 ) == N ) ans = "Bob";
	else if( uf.size( 0 ) == N - 1 ) {
		int cnt = 0;
		for( int i = 0; i < N; i++ ) {
			if( g[i].size() == 0 ) continue;
			if( g[i].size() == 1 ) {
				cnt++;
			}
		}
		if( cnt == 0 ) ans = "Bob";
	}

	cout << ans << endl;
}
0