結果

問題 No.2531 Coloring Vertices on Namori
ユーザー PNJPNJ
提出日時 2023-11-25 02:36:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 436 ms / 2,000 ms
コード長 639 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 120,136 KB
最終ジャッジ日時 2023-11-25 02:36:22
合計ジャッジ時間 12,587 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 269 ms
117,224 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 286 ms
117,224 KB
testcase_08 AC 398 ms
117,224 KB
testcase_09 AC 398 ms
117,224 KB
testcase_10 AC 425 ms
117,224 KB
testcase_11 AC 226 ms
120,136 KB
testcase_12 AC 225 ms
120,136 KB
testcase_13 AC 225 ms
120,136 KB
testcase_14 AC 379 ms
117,224 KB
testcase_15 AC 353 ms
117,224 KB
testcase_16 AC 355 ms
117,224 KB
testcase_17 AC 37 ms
53,460 KB
testcase_18 AC 37 ms
53,460 KB
testcase_19 AC 37 ms
53,460 KB
testcase_20 AC 422 ms
104,808 KB
testcase_21 AC 402 ms
104,936 KB
testcase_22 AC 399 ms
104,680 KB
testcase_23 AC 424 ms
105,064 KB
testcase_24 AC 400 ms
104,680 KB
testcase_25 AC 399 ms
104,680 KB
testcase_26 AC 426 ms
105,064 KB
testcase_27 AC 399 ms
104,680 KB
testcase_28 AC 395 ms
105,192 KB
testcase_29 AC 427 ms
105,064 KB
testcase_30 AC 396 ms
104,680 KB
testcase_31 AC 436 ms
105,448 KB
testcase_32 AC 406 ms
105,192 KB
testcase_33 AC 408 ms
104,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K = map(int,input().split())
G = [[] for i in range(N)]
d = [0 for i in range(N)]
mod = 998244353
for i in range(N):
  u,v = map(int,input().split())
  G[u-1].append(v-1)
  G[v-1].append(u-1)
  d[u-1] += 1
  d[v-1] += 1

H = []
for u in range(N):
  if d[u] == 1:
    H.append(u)

c = 0
while len(H):
  u = H.pop()
  for v in G[u]:
    d[v] -= 1
    if d[v] == 1:
      H.append(v)
  c += 1

ans = pow(K-1,c,mod)
r = N - c
dp = [[0,0] for i in range(r)]
dp[0][1] = K
for i in range(r-1):
  dp[i+1][0] += (K-2) * sum(dp[i]) + dp[i][1]
  dp[i+1][0] %= mod
  dp[i+1][1] += dp[i][0]
  dp[i+1][1] %= mod

ans *= dp[-1][0]
ans %= mod
print(ans)
0