結果

問題 No.2531 Coloring Vertices on Namori
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-11-04 12:24:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 474 ms / 2,000 ms
コード長 675 bytes
コンパイル時間 362 ms
コンパイル使用メモリ 81,836 KB
実行使用メモリ 125,404 KB
最終ジャッジ日時 2023-11-04 12:25:11
合計ジャッジ時間 12,474 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,568 KB
testcase_01 AC 37 ms
53,568 KB
testcase_02 AC 37 ms
53,568 KB
testcase_03 AC 36 ms
53,568 KB
testcase_04 AC 36 ms
53,568 KB
testcase_05 AC 248 ms
101,300 KB
testcase_06 AC 36 ms
53,568 KB
testcase_07 AC 251 ms
101,300 KB
testcase_08 AC 385 ms
101,336 KB
testcase_09 AC 378 ms
101,332 KB
testcase_10 AC 404 ms
101,340 KB
testcase_11 AC 272 ms
125,404 KB
testcase_12 AC 274 ms
125,404 KB
testcase_13 AC 269 ms
125,404 KB
testcase_14 AC 367 ms
101,320 KB
testcase_15 AC 343 ms
101,320 KB
testcase_16 AC 349 ms
101,320 KB
testcase_17 AC 37 ms
53,568 KB
testcase_18 AC 36 ms
53,568 KB
testcase_19 AC 38 ms
53,568 KB
testcase_20 AC 438 ms
106,712 KB
testcase_21 AC 440 ms
106,712 KB
testcase_22 AC 439 ms
106,624 KB
testcase_23 AC 439 ms
106,880 KB
testcase_24 AC 444 ms
106,544 KB
testcase_25 AC 474 ms
106,556 KB
testcase_26 AC 435 ms
106,892 KB
testcase_27 AC 442 ms
106,624 KB
testcase_28 AC 431 ms
107,076 KB
testcase_29 AC 435 ms
106,908 KB
testcase_30 AC 456 ms
106,624 KB
testcase_31 AC 431 ms
106,732 KB
testcase_32 AC 437 ms
107,092 KB
testcase_33 AC 463 ms
106,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,k = map(int,input().split())
e = [[] for i in range(n)]
mod = 998244353

count = [0]*n
for _ in range(n):
    u,v = [int(x)-1 for x in input().split()]
    e[u].append(v)
    e[v].append(u)
    count[u] += 1
    count[v] += 1


vis = [0]*n

q = []
for i in range(n):
    if count[i] == 1:
        q.append(i)


while q:
    now = q.pop()
    vis[now] = 1

    for nex in e[now]:
        count[nex] -= 1
        if count[nex] == 1:
            q.append(nex)



assert sum(vis) < n


size = n - sum(vis)
dp = [k,0]

for _ in range(size-1):

    dp = [dp[1],dp[0]*(k-1)+dp[1]*(k-2)]
    dp[0] %= mod
    dp[1] %= mod

ans = dp[1] * pow(k-1,sum(vis),mod)
ans %= mod

print(ans)
0