結果

問題 No.2531 Coloring Vertices on Namori
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-11-12 03:57:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 732 ms / 2,000 ms
コード長 1,693 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 154,292 KB
最終ジャッジ日時 2023-11-12 03:58:08
合計ジャッジ時間 15,868 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,732 KB
testcase_01 AC 45 ms
55,732 KB
testcase_02 AC 45 ms
55,732 KB
testcase_03 AC 45 ms
55,732 KB
testcase_04 AC 45 ms
55,732 KB
testcase_05 AC 227 ms
144,428 KB
testcase_06 AC 44 ms
55,732 KB
testcase_07 AC 225 ms
144,424 KB
testcase_08 AC 362 ms
144,420 KB
testcase_09 AC 352 ms
144,420 KB
testcase_10 AC 352 ms
144,524 KB
testcase_11 AC 308 ms
154,292 KB
testcase_12 AC 275 ms
154,276 KB
testcase_13 AC 274 ms
154,292 KB
testcase_14 AC 320 ms
144,440 KB
testcase_15 AC 335 ms
144,436 KB
testcase_16 AC 318 ms
144,436 KB
testcase_17 AC 45 ms
55,732 KB
testcase_18 AC 46 ms
55,732 KB
testcase_19 AC 46 ms
55,732 KB
testcase_20 AC 670 ms
128,764 KB
testcase_21 AC 655 ms
128,764 KB
testcase_22 AC 676 ms
128,892 KB
testcase_23 AC 681 ms
130,684 KB
testcase_24 AC 699 ms
130,556 KB
testcase_25 AC 636 ms
128,892 KB
testcase_26 AC 664 ms
128,764 KB
testcase_27 AC 692 ms
130,812 KB
testcase_28 AC 665 ms
128,636 KB
testcase_29 AC 653 ms
128,636 KB
testcase_30 AC 662 ms
128,636 KB
testcase_31 AC 631 ms
128,892 KB
testcase_32 AC 732 ms
130,684 KB
testcase_33 AC 675 ms
128,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

N,K = map(int,input().split())
e = [[] for _ in range(N)]
for _ in range(N):
  u,v = map(int,input().split())
  u -= 1
  v -= 1
  e[u].append(v)
  e[v].append(u)
def namori_decomposition(e):
    
    
    ## cycle,root
    ## root : その頂点が属するcycle上の点を返す
    
    
    n = len(e)
    deg = [len(e[i]) for i in range(n)]
    v = deque()
    for i in range(n):
        if deg[i]==1:
            v.append(i)
            
    cycle = [True]*n
    while v:
        x = v.popleft()
        cycle[x]=False
        for ix in e[x]:
            deg[ix] -= 1
            if deg[ix] == 1:
                v.append(ix)
                
    
    E = [[] for _ in range(n)]
    for i in range(n):
        for j in e[i]:
            if cycle[i]&cycle[j]:
                continue
            E[i].append(j)
            E[j].append(i)
                
    root = [-1]*n
    for i in range(n):
        if cycle[i]:
            v.append(i)
            root[i] = i
            while v:
                x = v.popleft()
                for ix in E[x]:
                    if root[ix]==-1:
                        root[ix] = i
                        v.append(ix)
    return cycle,root

cycle,root = namori_decomposition(e)

n = sum(cycle)
mod = 998244353
dp = [[0]*(n+1) for _ in range(2)]
dp[1][1] = K
for i in range(1,n):
  
  dp[1][i+1] = dp[0][i]%mod
  dp[0][i+1] = (dp[0][i]*(K-2) + dp[1][i]*(K-1))%mod
  
C = Counter(root)
ans = dp[0][n]
for v in C.values():
  
  ans *= pow(K-1,v-1,mod)
  ans %= mod
print(ans)
  
0