結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー kusaf_
提出日時 2024-09-01 03:11:32
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,597 bytes
コンパイル時間 3,434 ms
コンパイル使用メモリ 263,680 KB
実行使用メモリ 19,748 KB
最終ジャッジ日時 2024-09-01 03:11:37
合計ジャッジ時間 5,588 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/dsu>
using namespace std;
using namespace atcoder;
using ll = long long;

struct Lowlink {
  const vector<vector<ll>> g;
  ll N;
  vector<ll> ord, low;
  vector<ll> articulation;  // 関節点
  vector<pair<ll, ll>> bridge;  // 橋 (辺(u, v)が u < v となるよう格納)
  Lowlink(const vector<vector<ll>> &g): g(g), N(g.size()), ord(N, -1), low(N, -1) {
    for(ll i = 0, k = 0; i < N; i++) {
      if(ord[i] == -1) { k = dfs(i, k, -1); }
    }
  }
  ll dfs(ll idx, ll k, ll par) {
    low[idx] = (ord[idx] = k++);
    ll cnt = 0;
    bool arti = false, second = false;
    for(auto &to : g[idx]) {
      if(ord[to] == -1) {
        cnt++;
        k = dfs(to, k, idx);
        low[idx] = min(low[idx], low[to]);
        arti |= (par != -1) && (low[to] >= ord[idx]);
        if(ord[idx] < low[to]) { bridge.emplace_back(minmax(idx, to)); }
      }
      else if(to != par || second) { low[idx] = min(low[idx], ord[to]); }
      else { second = true; }
    }
    arti |= par == -1 && cnt > 1;
    if(arti) { articulation.emplace_back(idx); }
    return k;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N;
  cin >> N;

  dsu u(N);
  vector<vector<ll>> g(N);
  ll c = N;
  for(ll i = 0; i < N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    if(!u.same(a, b)) {
      u.merge(a, b);
      c--;
    }
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  if(c == 1) { cout << "Bob\n"; }
  else if(c == 2) {
    Lowlink L(g);
    cout << (L.bridge.empty() ? "Bob" : "Alice") << "\n";
  }
  else { cout << "Alice\n"; }
}
0