結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー risujirohrisujiroh
提出日時 2020-01-31 21:40:58
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,021 bytes
コンパイル時間 1,644 ms
コンパイル使用メモリ 169,788 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-17 07:28:39
合計ジャッジ時間 2,737 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind {
  vector<int> t;
  int c;
  UnionFind(int n) : t(n, -1), c(n) {}
  int find(int v) { return t[v] < 0 ? v : t[v] = find(t[v]); }
  void unite(int u, int v) {
    if ((u = find(u)) == (v = find(v))) return;
    if (-t[u] < -t[v]) swap(u, v);
    t[u] += t[v];
    t[v] = u;
    --c;
  }
  bool same(int u, int v) { return find(u) == find(v); }
  int size(int v) { return -t[find(v)]; }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  UnionFind uf(n);
  vector<int> d(n);
  for (int _ = n - 1; _--; ) {
    int u, v;
    cin >> u >> v;
    uf.unite(u, v);
    ++d[u], ++d[v];
  }
  if (uf.c == 1) {
    cout << "Bob\n";
  } else if (uf.c >= 3) {
    cout << "Alice\n";
  } else {
    bool b = true;
    for (int v = 0; v < n; ++v) {
      if (not (uf.size(v) == 1 or d[v] == 2)) {
        b = false;
        break;
      }
    }
    if (b) {
      cout << "Bob\n";
    } else {
      cout << "Alice\n";
    }
  }
}
0