結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー ngtkanangtkana
提出日時 2019-09-23 14:18:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 4,934 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 184,136 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2023-10-19 08:15:40
合計ジャッジ時間 3,824 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 9 ms
4,768 KB
testcase_14 AC 9 ms
4,728 KB
testcase_15 AC 9 ms
4,788 KB
testcase_16 AC 9 ms
4,768 KB
testcase_17 AC 9 ms
5,104 KB
testcase_18 AC 27 ms
6,888 KB
testcase_19 AC 31 ms
7,900 KB
testcase_20 AC 48 ms
9,152 KB
testcase_21 AC 79 ms
11,624 KB
testcase_22 AC 110 ms
13,804 KB
testcase_23 AC 127 ms
13,952 KB
testcase_24 AC 112 ms
13,952 KB
testcase_25 AC 126 ms
13,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;
#else
#define debug 0;
#endif

template <typename T>
auto make_vector_impl(size_t sz, T t) {return std::vector<T>(sz, t);}

template <size_t N, typename T, typename U, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));}

template <size_t N, typename T, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {return std::vector<T>(sz);}

template <size_t N, typename T, typename... Args, std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector<N - 1, T>(args...));}

template <typename T, typename Size_t>
auto& at(T& t, Size_t i) {return t.at(i);}

template <typename T, typename Size_t, typename... Args>
auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);}

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template< typename F >
class fixed_point : F {
  public:
    explicit constexpr fixed_point (F&& f) noexcept
      : F(std::forward< F >(f)) {}
    template< typename ... Args >
    constexpr decltype(auto) operator()(Args&& ... args) const
      { return F::operator()(*this, std::forward< Args >(args)...); }
};
template< typename F >
static inline constexpr decltype(auto) fix (F&& f) noexcept
  { return fixed_point< F >{std::forward< F >(f)}; }

std::chrono::system_clock::time_point chrono_begin;

void alice () {
  std::cout << "Alice" << std::endl;
  auto chrono_end  = std::chrono::system_clock::now();
  auto runtime     = std::chrono::duration_cast< std::chrono::milliseconds >(chrono_end-chrono_begin).count();
  debug(runtime);
  exit(0);
}
void bob () {
  std::cout << "Bob" << std::endl;
  auto chrono_end  = std::chrono::system_clock::now();
  auto runtime     = std::chrono::duration_cast< std::chrono::milliseconds >(chrono_end-chrono_begin).count();
  debug(runtime);
  exit(0);
}

auto is_tree(std::vector< std::vector< int > > const &graph) {
  auto n = (int)graph.size();
  auto root = 0;
  std::vector< int >ckd(n, false);

  auto cycle = fix([&](auto&& dfs, int x, int p, int d) -> bool {
    ckd.at(x) = true;
    bool ret = false;
    for (auto const& y : graph.at(x)) {
      if (ckd.at(y)) {
        if (y != p) { return true; }
        else        { continue;    }
      }
      ret |= dfs(y, x, d + 1);
    }
    return ret;
  })(root, root, 0);

  if (cycle) return false;
  auto k = std::accumulate(all(ckd), 0);
  return n == k;
}

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  chrono_begin = std::chrono::system_clock::now();

  int n; std::cin >> n;
  assert(n <= 100'000);
  debug(n);
  auto graph = make_vector< 2, int >(n, 0);
  auto edges = std::set< std::pair< int, int > >{};
  loop(n - 1) {
    int u, v; std::cin >> u >> v;
    graph.at(u).emplace_back(v);
    graph.at(v).emplace_back(u);
    if (u > v)
      { std::swap(u, v); }
    assert(u != v);
    assert(!edges.count({u, v}));
    edges.emplace(u, v);
  }

  if (is_tree(graph))
    { bob(); }

  for (auto const& v : graph) {
    if (v.size() != 2 && v.size() != 0)
      { alice(); }
  }

  auto start = -1;
  rep(i, 0, n) {
    if (graph.at(i).size() == 2)
      { start = i; }
  }
  std::vector< int >ckd(n, false);
  fix ([&](auto dfs, int crr) -> void {
    ckd.at(crr) = true;
    for (auto const& nxt : graph.at(crr)) {
      if (ckd.at(nxt)) continue;
      dfs(nxt);
    }
  })(start);
  auto cnt = std::accumulate(all(ckd), 0);
  if (cnt != n - 1) alice();
  else bob();
  return 0;
}
0