結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー seriru13seriru13
提出日時 2020-01-31 22:39:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 2,213 bytes
コンパイル時間 2,298 ms
コンパイル使用メモリ 218,348 KB
実行使用メモリ 8,876 KB
最終ジャッジ日時 2023-10-17 11:08:16
合計ジャッジ時間 4,157 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 10 ms
4,348 KB
testcase_14 AC 10 ms
4,348 KB
testcase_15 AC 10 ms
4,348 KB
testcase_16 AC 10 ms
4,348 KB
testcase_17 AC 11 ms
4,348 KB
testcase_18 AC 30 ms
5,324 KB
testcase_19 AC 37 ms
5,324 KB
testcase_20 AC 59 ms
5,972 KB
testcase_21 AC 106 ms
7,556 KB
testcase_22 AC 144 ms
8,612 KB
testcase_23 AC 142 ms
8,876 KB
testcase_24 AC 117 ms
8,876 KB
testcase_25 AC 144 ms
8,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i,n) for(long long i=0; i<n; i++)
#define repr(i,n,m) for(long long i=m; i<n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x) for (auto&& itr : x) { cerr << (itr) << " "; }

template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t;}
template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t;}

/* Union-Find-Tree */
/* 必ず要素数をコンストラクタに入れること */
template <class T = long long>
class Union_Find {
  using size_type = std::size_t;
  using _Tp = T;
public:

  vector<_Tp> par;
  vector<_Tp> rnk;

  // 親の根を返す。値の変更は認めない。
  const _Tp & operator[] (size_type child) {
    find(child);
    return par[child];
  }

  Union_Find (size_type n) {
    par.resize(n), rnk.resize(n);
    for (int i = 0; i < n; ++i) {
      par[i] = i;
      rnk[i] = 0;
    }
  }

  // 木の根を求める
  _Tp find(_Tp x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  // xとyの属する集合を併合
  void merge(_Tp x, _Tp y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (rnk[x] < rnk[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rnk[x] == rnk[y]) rnk[x]++;
    }
  }

  // xとyが同じ集合に属するか否か
  bool same(_Tp x, _Tp y) {
    return find(x) == find(y);
  }
};

int main() {
  ll n;
  cin >> n;
  Union_Find<int> uf(n);
  map<int, int> mp;

  rep(i, n-1) {
    int u, v;
    cin >> u >> v;
    uf.merge(u, v);
    mp[u] += 1;
    mp[v] += 1;
  }

  int root = uf.find(0);
  set<int> st;
  st.insert(root);
  rep(i, n) {
    if (uf.find(i) != root) {
      st.insert(uf.find(i));
    }
  }

  if (st.size() > 2) {
    cout << "Alice" << endl;
  } else {
    if (st.size() == 1) {
      cout << "Bob\n";
    } else {
      rep(i, n) {
        if (mp[i] == 1) {
          cout << "Alice" << endl;
          return 0;
        }
      }
      cout << "Bob" << endl;
    }
  }

}
0