結果

問題 No.2531 Coloring Vertices on Namori
ユーザー 👑 timitimi
提出日時 2024-02-13 15:52:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 423 ms / 2,000 ms
コード長 648 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 128,608 KB
最終ジャッジ日時 2024-02-13 15:53:03
合計ジャッジ時間 13,034 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,612 KB
testcase_01 AC 38 ms
55,612 KB
testcase_02 AC 39 ms
55,608 KB
testcase_03 AC 38 ms
55,612 KB
testcase_04 AC 38 ms
55,612 KB
testcase_05 AC 243 ms
128,224 KB
testcase_06 AC 68 ms
55,612 KB
testcase_07 AC 225 ms
128,224 KB
testcase_08 AC 333 ms
128,608 KB
testcase_09 AC 334 ms
128,608 KB
testcase_10 AC 362 ms
128,608 KB
testcase_11 AC 219 ms
106,712 KB
testcase_12 AC 218 ms
106,712 KB
testcase_13 AC 219 ms
106,704 KB
testcase_14 AC 288 ms
128,096 KB
testcase_15 AC 319 ms
128,096 KB
testcase_16 AC 290 ms
128,096 KB
testcase_17 AC 38 ms
55,608 KB
testcase_18 AC 38 ms
55,608 KB
testcase_19 AC 38 ms
55,612 KB
testcase_20 AC 373 ms
102,496 KB
testcase_21 AC 412 ms
102,496 KB
testcase_22 AC 374 ms
102,496 KB
testcase_23 AC 383 ms
102,496 KB
testcase_24 AC 423 ms
102,496 KB
testcase_25 AC 374 ms
102,496 KB
testcase_26 AC 374 ms
102,496 KB
testcase_27 AC 401 ms
102,496 KB
testcase_28 AC 374 ms
102,496 KB
testcase_29 AC 378 ms
102,624 KB
testcase_30 AC 394 ms
102,496 KB
testcase_31 AC 374 ms
102,624 KB
testcase_32 AC 378 ms
102,496 KB
testcase_33 AC 381 ms
102,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K=map(int, input().split())
D=[[] for i in range(N)]
E=[0]*N
for i in range(N):
  u,v=map(int, input().split())
  u-=1;v-=1
  D[u].append(v);D[v].append(u)
  E[u]+=1;E[v]+=1

from collections import deque
d=deque();V=[-1]*N
for i in range(N):
  if E[i]==1:
    d.append(i)
    V[i]=1

while d:
  now=d.popleft()
  E[now]-=1
  for nex in D[now]:
    if V[nex]==-1:
      E[nex]-=1
      if E[nex]==1:
        V[nex]=0
        d.append(nex)

mod=998244353
e=sum(E)//2
dp1=[1];dp2=[0]
for i in range(e-1):
  p,q=dp1[-1],dp2[-1]
  dp1.append(q)
  c=p*(K-1)+q*(K-2)
  dp2.append(c%mod)
ans=dp2[-1]*K
ans%=mod 
ans*=pow(K-1,N-e,mod)
ans%=mod
print(ans)
0