結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| ユーザー |
kibuna
|
| 提出日時 | 2020-01-31 22:14:26 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 23 ms / 2,000 ms |
| コード長 | 1,978 bytes |
| コンパイル時間 | 1,500 ms |
| コンパイル使用メモリ | 172,696 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-17 08:33:40 |
| 合計ジャッジ時間 | 2,738 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;
struct UnionFind {
vector<int> rank, parent, size;
// +1 for 1-indexed nodes
UnionFind(int n) : rank(n + 1, 0), parent(n + 1), size(n + 1, 1) {
iota(parent.begin(), parent.end(), 0); // parent is itself
}
int root(int x) {
if (x != parent[x]) {
parent[x] = root(parent[x]);
}
return parent[x];
}
bool isSame(int x, int y) { return root(x) == root(y); }
bool unite(int x, int y) { return link(root(x), root(y)); }
bool link(int x, int y) {
if (x == y)
return false;
if (rank[x] > rank[y]) {
parent[y] = x;
size[x] += size[y];
} else {
parent[x] = y;
size[y] += size[x];
if (rank[x] == rank[y]) {
rank[y]++;
}
}
return true;
}
int getSize(int x) { return size[root(x)]; }
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
lint n;
cin >> n;
UnionFind uf(n);
vector<lint> deg(n, 0);
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
uf.unite(u, v);
deg[u]++;
deg[v]++;
}
if (uf.getSize(0) == n) {
cout << "Bob"
<< "\n";
} else {
bool ok = true;
int idp = 0;
int tot = 0;
for (int i = 0; i < n; ++i) {
if (deg[i] == 0)
idp++;
else if (deg[i] != 2)
ok = false;
else if (uf.getSize(i) != n - 1) {
ok = false;
}
tot += deg[i];
}
if (ok && idp == 1) {
assert(tot == 2 * (n - 1));
cout << "Bob"
<< "\n";
} else {
cout << "Alice"
<< "\n";
}
}
return 0;
}
kibuna