結果

問題 No.2531 Coloring Vertices on Namori
ユーザー sasa8uyauyasasa8uyauya
提出日時 2023-11-04 18:13:30
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 714 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 81,900 KB
実行使用メモリ 142,340 KB
最終ジャッジ日時 2023-11-04 18:13:36
合計ジャッジ時間 4,988 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
58,016 KB
testcase_01 AC 36 ms
53,552 KB
testcase_02 AC 36 ms
53,552 KB
testcase_03 AC 36 ms
53,552 KB
testcase_04 AC 37 ms
53,552 KB
testcase_05 AC 239 ms
120,960 KB
testcase_06 AC 37 ms
53,552 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

n,k=map(int,input().split())
M=998244353
e=[[] for i in range(n)]
d=[0]*n
for i in range(n):
  a,b=map(int,input().split())
  a-=1
  b-=1
  e[a]+=[b]
  e[b]+=[a]
  d[a]+=1
  d[b]+=1

q1=[0,0]
q2=[0,k]
for i in range(2,n+2):
  q1+=[(q1[i-1]*(k-2)+q2[i-1]*(k-1))%M]
  q2+=[q1[i-1]]

if 1 not in d:
  print(q2[n+1])
  exit()

for i in range(n):
  if d[i]==1:
    S=i
    break

v=[0]*n
v[S]=1
q=[S]
g=[0]*n
f=0
while len(q)>0:
  s=q[-1]
  while g[s]<len(e[s]):
    t=e[s][g[s]]
    if v[t]>0 and t!=q[-2]:
      l=v[s]+1-v[t]
      f=1
      break
    if v[t]==0:
      break
    g[s]+=1
  if f:
    break
  if g[s]<len(e[s]):
    v[t]=v[s]+1
    q+=[t]
  else:
    v[s]=0
    q.pop()

print(q2[l+1]*pow(k-1,n-l,M)%M)
0