結果

問題 No.2531 Coloring Vertices on Namori
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-11-12 04:33:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 698 ms / 2,000 ms
コード長 1,586 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 154,356 KB
最終ジャッジ日時 2023-11-12 04:34:02
合計ジャッジ時間 15,382 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,732 KB
testcase_01 AC 43 ms
55,732 KB
testcase_02 AC 43 ms
55,732 KB
testcase_03 AC 44 ms
55,732 KB
testcase_04 AC 44 ms
55,732 KB
testcase_05 AC 219 ms
141,188 KB
testcase_06 AC 44 ms
55,732 KB
testcase_07 AC 223 ms
141,184 KB
testcase_08 AC 323 ms
141,304 KB
testcase_09 AC 324 ms
141,312 KB
testcase_10 AC 324 ms
141,432 KB
testcase_11 AC 287 ms
154,356 KB
testcase_12 AC 265 ms
154,356 KB
testcase_13 AC 264 ms
154,308 KB
testcase_14 AC 299 ms
141,200 KB
testcase_15 AC 319 ms
141,200 KB
testcase_16 AC 293 ms
141,200 KB
testcase_17 AC 44 ms
55,732 KB
testcase_18 AC 43 ms
55,732 KB
testcase_19 AC 43 ms
55,732 KB
testcase_20 AC 597 ms
128,636 KB
testcase_21 AC 595 ms
128,636 KB
testcase_22 AC 667 ms
128,764 KB
testcase_23 AC 662 ms
130,556 KB
testcase_24 AC 676 ms
130,428 KB
testcase_25 AC 600 ms
128,892 KB
testcase_26 AC 647 ms
128,636 KB
testcase_27 AC 664 ms
130,684 KB
testcase_28 AC 650 ms
128,508 KB
testcase_29 AC 631 ms
128,508 KB
testcase_30 AC 641 ms
128,508 KB
testcase_31 AC 622 ms
128,764 KB
testcase_32 AC 698 ms
130,556 KB
testcase_33 AC 642 ms
128,636 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
C = Counter(root)
ans = (K-1)*(pow(K-1,n-1,mod) - pow(-1,n-1,mod))%mod
#956138134
for v in C.values():
  
  ans *= pow(K-1,v-1,mod)
  ans %= mod
print(ans)
  
0