結果

問題 No.2531 Coloring Vertices on Namori
ユーザー SSRSSSRS
提出日時 2023-11-03 21:37:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 937 bytes
コンパイル時間 2,222 ms
コンパイル使用メモリ 209,776 KB
実行使用メモリ 16,708 KB
最終ジャッジ日時 2023-11-03 21:37:26
合計ジャッジ時間 8,473 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 1 ms
4,348 KB
testcase_05 AC 125 ms
15,132 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 125 ms
15,132 KB
testcase_08 AC 145 ms
15,132 KB
testcase_09 AC 145 ms
15,132 KB
testcase_10 AC 158 ms
15,132 KB
testcase_11 AC 99 ms
16,708 KB
testcase_12 AC 98 ms
16,708 KB
testcase_13 AC 98 ms
16,708 KB
testcase_14 AC 135 ms
15,132 KB
testcase_15 AC 160 ms
15,132 KB
testcase_16 AC 137 ms
15,132 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 150 ms
15,408 KB
testcase_21 AC 145 ms
15,396 KB
testcase_22 AC 147 ms
15,396 KB
testcase_23 AC 146 ms
15,396 KB
testcase_24 AC 150 ms
15,528 KB
testcase_25 AC 145 ms
15,492 KB
testcase_26 AC 152 ms
15,544 KB
testcase_27 AC 148 ms
15,432 KB
testcase_28 AC 172 ms
15,488 KB
testcase_29 AC 153 ms
15,432 KB
testcase_30 AC 148 ms
15,396 KB
testcase_31 AC 146 ms
15,444 KB
testcase_32 AC 151 ms
15,476 KB
testcase_33 AC 146 ms
15,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main(){
  int N, K;
  cin >> N >> K;
  vector<vector<int>> E(N);
  for (int i = 0; i < N; i++){
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  vector<int> deg(N);
  for (int i = 0; i < N; i++){
    deg[i] = E[i].size();
  }
  queue<int> Q;
  for (int i = 0; i < N; i++){
    if (deg[i] == 1){
      Q.push(i);
    }
  }
  long long ans = 1;
  int cnt = N;
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    ans *= K - 1;
    ans %= MOD;
    cnt--;
    for (int w : E[v]){
      deg[w]--;
      if (deg[w] == 1){
        Q.push(w);
      }
    }
  }
  long long tmp = 1;
  for (int i = 0; i < cnt; i++){
    tmp *= K - 1;
    tmp %= MOD;
  }
  if (cnt % 2 == 0){
    tmp += K - 1;
  } else {
    tmp += MOD - (K - 1);
  }
  tmp %= MOD;
  ans *= tmp;
  ans %= MOD;
  cout << ans << endl;
}
0