結果

問題 No.2531 Coloring Vertices on Namori
ユーザー rlangevinrlangevin
提出日時 2023-11-03 22:50:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 853 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 81,880 KB
実行使用メモリ 106,788 KB
最終ジャッジ日時 2023-11-03 22:50:57
合計ジャッジ時間 7,986 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,624 KB
testcase_01 AC 39 ms
55,624 KB
testcase_02 AC 37 ms
55,624 KB
testcase_03 AC 39 ms
55,624 KB
testcase_04 AC 37 ms
55,624 KB
testcase_05 AC 162 ms
101,308 KB
testcase_06 AC 36 ms
55,624 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 142 ms
106,780 KB
testcase_12 AC 147 ms
106,784 KB
testcase_13 AC 147 ms
106,788 KB
testcase_14 AC 228 ms
101,272 KB
testcase_15 AC 226 ms
101,292 KB
testcase_16 AC 255 ms
101,276 KB
testcase_17 WA -
testcase_18 AC 36 ms
55,624 KB
testcase_19 AC 36 ms
55,624 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N, K = map(int, input().split())
G = [[] for i in range(N)]
for i in range(N):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    G[u].append(v)
    G[v].append(u)
    
from collections import *
Q = deque([0])
dist = [-1] * N
par = [-1] * N
loop = -1
dist[0] = 0
while Q and loop == -1:
    u = Q.popleft()
    for v in G[u]:
        if v == par[u]:
            continue
        if dist[v] != -1:
            loop = dist[u] + dist[v] + 1
            break
        par[v] = u
        dist[v] = dist[u] + 1
        Q.append(v)
        
pre0, pre1 = K, 0
mod = 998244353
for _ in range(loop - 2):
    dp0 = pre1
    dp1 = pre0 * (K - 1) + pre1 * (K - 2)
    pre0 = dp0 % mod
    pre1 = dp1 % mod    

ans = pre0 * pow(K - 1, N - loop + 1, mod) + pre1 * (K - 2) * pow(K - 1, N - loop, mod)
print(ans%mod)
0