結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| ユーザー |
|
| 提出日時 | 2024-06-20 18:39:45 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 53 ms / 2,000 ms |
| コード長 | 1,813 bytes |
| コンパイル時間 | 3,782 ms |
| コンパイル使用メモリ | 142,744 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-20 18:39:51 |
| 合計ジャッジ時間 | 5,527 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
import std.stdio: readf, readln, writeln;
import std.conv: to;
import std.string: chomp;
void main() {
const n = readln.chomp.to!int;
auto uf = new UnionFind(n);
auto cnt = new int[n];
bool gg = true;
foreach(_; 0..n - 1) {
int u, v;
readf("%d %d\n", u, v);
gg &= uf.unite(u, v);
cnt[u]++;
cnt[v]++;
}
if(gg) {
writeln("Bob");
return;
}
foreach(e; cnt) {
if(e >= 1 && e != 2) {
writeln("Alice");
return;
}
}
writeln(gg || uf.size(0) == n - 1 || uf.size(1) == n - 1 ? "Bob" : "Alice");
}
class UnionFind {
import std.algorithm: swap, filter;
import std.array: array;
import std.range;
private:
int n;
int[] par;
public:
this(const int n) {
this.n = n;
par = new int[n];
par[] = -1;
}
int length() const { return n; }
ref auto opIndex(int i) {
while(par[i] >= 0) {
const p = par[par[i]];
if(p < 0) {
return par[i];
}
i = par[i] = p;
}
return i;
}
int size(const int i){ return -par[this[i]]; }
bool unite(int i, int j) {
i = this[i];
j = this[j];
if(i == j) {
return false;
}
if(i > j) {
swap(i, j);
}
par[i] += par[j];
par[j] = i;
return true;
}
int[][] groups() {
int[][] res = new int[][n];
foreach(i; 0..n) {
res[this[i]] ~= i;
}
return res.filter!(a => !a.empty).array;
}
}
bool isBipartite(UnionFind uf) {
assert(uf.length % 2 == 0);
const n = uf.length / 2;
bool ok = true;
foreach(i; 0..n) {
ok &= uf[i] != uf[i + n];
}
return ok;
}