結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| ユーザー |
|
| 提出日時 | 2020-08-29 12:42:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 93 ms / 2,000 ms |
| コード長 | 2,457 bytes |
| コンパイル時間 | 2,004 ms |
| コンパイル使用メモリ | 188,716 KB |
| 実行使用メモリ | 10,368 KB |
| 最終ジャッジ日時 | 2024-11-14 05:36:11 |
| 合計ジャッジ時間 | 3,334 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
class UnionFind {
public:
vector<ll> par; // 各元の親を表す配列
vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)
// Constructor
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
UnionFind tree(N);
vector<pint> hen(N - 1);
map<ll, bool> ma;
map<int, int> cnt;
set<int> se;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
if (tree.issame(a, b))
ma[tree.root(a)] = true;
tree.merge(a, b);
cnt[a]++;
cnt[b]++;
}
rep(i, N) { se.insert(tree.root(i)); }
if (se.size() == 1) {
cout << "Bob" << endl;
} else if (se.size() == 2) {
bool judge = 1;
for (auto c : cnt) {
if (c.second < 2) {
judge = 0;
cout << "Alice" << endl;
return 0;
}
}
if (judge) {
cout << "Bob" << endl;
}
} else {
cout << "Alice" << endl;
}
return 0;
}