結果

問題 No.2531 Coloring Vertices on Namori
ユーザー sasa8uyauyasasa8uyauya
提出日時 2023-11-03 23:52:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,184 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 81,748 KB
実行使用メモリ 156,704 KB
最終ジャッジ日時 2023-11-03 23:52:35
合計ジャッジ時間 11,962 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,492 KB
testcase_01 AC 38 ms
53,492 KB
testcase_02 AC 37 ms
53,492 KB
testcase_03 AC 37 ms
53,492 KB
testcase_04 AC 37 ms
53,492 KB
testcase_05 AC 238 ms
122,708 KB
testcase_06 AC 42 ms
53,492 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 547 ms
156,704 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 345 ms
125,736 KB
testcase_15 AC 350 ms
125,736 KB
testcase_16 AC 390 ms
125,736 KB
testcase_17 WA -
testcase_18 RE -
testcase_19 AC 36 ms
53,492 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 #

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,k*(k-1)%M]
q2=[0,k,0]
for i in range(3,n+2):
  q1+=[(q1[i-1]*(k-2)+q2[i-1]*(k-1))%M]
  q2+=[q1[i-1]]

if 1 not in d:
  if k==2:
    print(2*(n%2==0))
  else:
    if n==3:
      print(k*(k-1)*(k-2)%M)
    else:
      print(q2[n+1])
  exit()

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

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

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

l=len(q)
if k==2:
  print(2*(l%2==0))
else:
  if l==3:
    print(k*(k-1)*(k-2)*pow(k-1,n-l,M)%M)
  else:
    print(q2[l+1]*pow(k-1,n-l,M)%M)
0