結果

問題 No.2531 Coloring Vertices on Namori
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-11-12 03:57:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 667 ms / 2,000 ms
コード長 1,693 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 154,716 KB
最終ジャッジ日時 2024-09-26 03:00:18
合計ジャッジ時間 13,939 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
55,680 KB
testcase_01 AC 49 ms
55,808 KB
testcase_02 AC 50 ms
55,936 KB
testcase_03 AC 48 ms
55,936 KB
testcase_04 AC 48 ms
55,552 KB
testcase_05 AC 224 ms
144,896 KB
testcase_06 AC 50 ms
55,936 KB
testcase_07 AC 226 ms
144,640 KB
testcase_08 AC 334 ms
144,640 KB
testcase_09 AC 347 ms
145,152 KB
testcase_10 AC 346 ms
145,152 KB
testcase_11 AC 276 ms
154,472 KB
testcase_12 AC 275 ms
154,716 KB
testcase_13 AC 269 ms
154,352 KB
testcase_14 AC 312 ms
144,896 KB
testcase_15 AC 299 ms
144,896 KB
testcase_16 AC 298 ms
144,768 KB
testcase_17 AC 47 ms
55,296 KB
testcase_18 AC 49 ms
55,936 KB
testcase_19 AC 50 ms
55,552 KB
testcase_20 AC 601 ms
128,896 KB
testcase_21 AC 577 ms
128,896 KB
testcase_22 AC 578 ms
129,280 KB
testcase_23 AC 664 ms
130,944 KB
testcase_24 AC 667 ms
130,688 KB
testcase_25 AC 561 ms
129,152 KB
testcase_26 AC 602 ms
129,280 KB
testcase_27 AC 597 ms
131,072 KB
testcase_28 AC 615 ms
128,768 KB
testcase_29 AC 601 ms
128,896 KB
testcase_30 AC 565 ms
129,152 KB
testcase_31 AC 575 ms
129,280 KB
testcase_32 AC 642 ms
131,072 KB
testcase_33 AC 577 ms
129,280 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