結果

問題 No.2531 Coloring Vertices on Namori
ユーザー titia
提出日時 2023-11-05 00:32:33
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 759 ms / 2,000 ms
コード長 708 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 48,512 KB
最終ジャッジ日時 2024-09-25 22:21:34
合計ジャッジ時間 19,295 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=998244353

N,K=map(int,input().split())
DEG=[0]*(N+5)

E=[[] for i in range(N+1)]

for i in range(N):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)
    DEG[x]+=1
    DEG[y]+=1

Q=[]
for i in range(N+1):
    if DEG[i]==1:
        Q.append(i)

#print(DEG)

while Q:
    x=Q.pop()
    DEG[x]-=1
    for to in E[x]:
        DEG[to]-=1
        if DEG[to]==1:
            Q.append(to)

L=0
for i in range(N+5):
    if DEG[i]==2:
        L+=1

DP=[1,0]

for i in range(L-1):
    NDP=[0,0]

    NDP[0]=DP[1]
    NDP[1]=DP[0]*(K-1)+DP[1]*(K-2)

    DP[0]=NDP[0]%mod
    DP[1]=NDP[1]%mod

ANS=K*DP[1]*pow(K-1,N-L,mod)%mod

print(ANS)

    

        
0