結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー Mayimg
提出日時 2020-02-01 07:24:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 1,715 bytes
コンパイル時間 2,497 ms
コンパイル使用メモリ 198,376 KB
最終ジャッジ日時 2025-01-08 21:35:07
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
class UnionFind {
private:
  int siz;
  vector<int> a;

public:
  UnionFind(int x) : siz(x), a(x, -1) {}

  int root(int x) {
    return a[x] < 0 ? x : a[x] = root(a[x]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    siz--;
    if (a[x] > a[y]) swap(x, y);
    a[x] += a[y];
    a[y] =x;
    return true;
  }

  bool same(int x, int y) {
    return root(x) == root(y);
  }

  int size(int x) {
    return -a[root(x)];
  }
  
  int connected_component() {
  	return siz;
  }
};
const int INF = 1 << 30;
bool dfs (int cur, int from, int c, const vector<vector<int>>& g, vector<int>& order, vector<int>& low) {
  order[cur] = c++;
  low[cur] = order[cur];
  bool res = false;
  for (int nbr : g[cur]) {
    if (order[nbr] == INF) {
      res |= dfs (nbr, cur, c, g, order, low);
      low[cur] = min(low[cur], low[nbr]);
      if (low[nbr] == order[nbr]) {
        res = true;
      }
    } else if (nbr != from) {
      low[cur] = min(low[cur], low[nbr]);
    }
  }
  return res;
}
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  UnionFind uf(n);
  for (int i = 0; i + 1 < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
    uf.unite(u, v);
  }
  vector<int> order(n, INF), low(n, INF);
  bool b = false;
  for (int i = 0; i < n; i++) {
    if (uf.size(i) > 1 && uf.root(i) == i) {
      order[i] = 0;
      b |= dfs (i, -1, 0, g, order, low);
    }
  }
  if (uf.connected_component() > 2 || (uf.connected_component() > 1 && b)) cout << "Alice\n";
  else cout << "Bob\n";
  return 0;
}
0