結果

問題 No.2531 Coloring Vertices on Namori
ユーザー risujirohrisujiroh
提出日時 2023-11-03 21:28:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 3,353 bytes
コンパイル時間 3,253 ms
コンパイル使用メモリ 261,348 KB
実行使用メモリ 22,628 KB
最終ジャッジ日時 2023-11-03 21:28:52
合計ジャッジ時間 9,691 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 68 ms
22,628 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 68 ms
22,628 KB
testcase_08 AC 120 ms
22,628 KB
testcase_09 AC 124 ms
22,628 KB
testcase_10 AC 132 ms
22,628 KB
testcase_11 AC 50 ms
15,772 KB
testcase_12 AC 50 ms
15,772 KB
testcase_13 AC 51 ms
15,772 KB
testcase_14 AC 112 ms
22,628 KB
testcase_15 AC 112 ms
22,628 KB
testcase_16 AC 118 ms
22,628 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 117 ms
14,972 KB
testcase_21 AC 148 ms
14,972 KB
testcase_22 AC 114 ms
14,972 KB
testcase_23 AC 123 ms
14,972 KB
testcase_24 AC 107 ms
14,972 KB
testcase_25 AC 107 ms
14,972 KB
testcase_26 AC 107 ms
14,972 KB
testcase_27 AC 108 ms
14,972 KB
testcase_28 AC 108 ms
14,972 KB
testcase_29 AC 107 ms
14,972 KB
testcase_30 AC 104 ms
14,972 KB
testcase_31 AC 125 ms
14,972 KB
testcase_32 AC 122 ms
14,972 KB
testcase_33 AC 117 ms
14,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

namespace {

using Fp = atcoder::modint998244353;

void solve() {
  int n, k;
  scan(n, k);
  vector<vector<int>> g(n);
  for (int _ = n; _--;) {
    int i, j;
    scan(i, j);
    --i, --j;
    g[i].push_back(j);
    g[j].push_back(i);
  }
  vector<int> depth(n, -1);
  depth[0] = 0;
  int c = -1;
  fix([&](auto self, int i) -> void {
    for (int j : g[i]) {
      if (depth[j] == -1) {
        depth[j] = depth[i] + 1;
        self(j);
      } else if (depth[j] + 1 < depth[i]) {
        assert(c == -1);
        c = depth[i] - depth[j] + 1;
      }
    }
  })(0);
  vector<Fp> f(c + 1);
  f[0] = k;
  for (int i : rep1(2, c)) {
    f[i] = f[i - 1] * (k - 2) + f[i - 2] * (k - 1);
  }
  Fp ans = f[c];
  ans *= Fp(k - 1).pow(n - c);
  print(ans);
}

}  // namespace

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

  solve();
}

#else  // __INCLUDE_LEVEL__

#include <bits/stdc++.h>

using namespace std;

#include <atcoder/modint>

namespace std {

template <class T1, class T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
  return is >> p.first >> p.second;
}

template <class... Ts>
istream& operator>>(istream& is, tuple<Ts...>& t) {
  return apply([&is](auto&... xs) -> istream& { return (is >> ... >> xs); }, t);
}

template <class R, enable_if_t<!is_same_v<R, string&>>* = nullptr>
auto operator>>(istream& is, R&& r) -> decltype(is >> *begin(r)) {
  for (auto&& e : r) {
    is >> e;
  }
  return is;
}

template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << p.first << ' ' << p.second;
}

template <class... Ts>
ostream& operator<<(ostream& os, const tuple<Ts...>& t) {
  auto f = [&os](const auto&... xs) -> ostream& {
    [[maybe_unused]] auto sep = "";
    ((os << exchange(sep, " ") << xs), ...);
    return os;
  };
  return apply(f, t);
}

template <class R, enable_if_t<!is_convertible_v<R, string_view>>* = nullptr>
auto operator<<(ostream& os, R&& r) -> decltype(os << *begin(r)) {
  auto sep = "";
  for (auto&& e : r) {
    os << exchange(sep, " ") << e;
  }
  return os;
}

}  // namespace std

namespace atcoder {

template <class T, internal::is_modint_t<T>* = nullptr>
istream& operator>>(istream& is, T& x) {
  int v;
  is >> v;
  x = T::raw(v);
  return is;
}

template <class T, internal::is_modint_t<T>* = nullptr>
ostream& operator<<(ostream& os, const T& x) {
  return os << x.val();
}

}  // namespace atcoder

template <class... Ts>
void scan(Ts&&... xs) {
  (cin >> ... >> xs);
}

template <class... Ts>
void print(Ts&&... xs) {
  cout << tie(xs...) << '\n';
}

template <class F>
class fix {
 public:
  explicit fix(F f) : f_(move(f)) {}

  template <class... Ts>
  decltype(auto) operator()(Ts&&... xs) const {
    return f_(ref(*this), forward<Ts>(xs)...);
  }

 private:
  F f_;
};

inline auto rep(int l, int r) { return views::iota(min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }
inline auto per(int l, int r) { return rep(l, r) | views::reverse; }
inline auto per(int n) { return per(0, n); }
inline auto per1(int l, int r) { return per(l, r + 1); }
inline auto per1(int n) { return per(1, n + 1); }

inline auto len = ranges::ssize;

#endif  // __INCLUDE_LEVEL__
0