結果

問題 No.2531 Coloring Vertices on Namori
ユーザー navel_tosnavel_tos
提出日時 2023-11-04 16:09:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,365 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 81,780 KB
実行使用メモリ 160,044 KB
最終ジャッジ日時 2023-11-04 16:09:28
合計ジャッジ時間 16,553 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,592 KB
testcase_01 AC 37 ms
53,592 KB
testcase_02 AC 37 ms
53,592 KB
testcase_03 AC 37 ms
53,592 KB
testcase_04 AC 37 ms
53,592 KB
testcase_05 AC 387 ms
156,360 KB
testcase_06 AC 37 ms
53,592 KB
testcase_07 WA -
testcase_08 AC 630 ms
158,196 KB
testcase_09 AC 614 ms
158,196 KB
testcase_10 AC 601 ms
158,228 KB
testcase_11 AC 285 ms
132,808 KB
testcase_12 AC 279 ms
132,808 KB
testcase_13 AC 280 ms
132,812 KB
testcase_14 AC 575 ms
160,044 KB
testcase_15 AC 556 ms
160,044 KB
testcase_16 AC 584 ms
160,044 KB
testcase_17 WA -
testcase_18 AC 37 ms
53,592 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 659 ms
126,912 KB
testcase_24 AC 631 ms
129,752 KB
testcase_25 WA -
testcase_26 AC 634 ms
123,408 KB
testcase_27 AC 671 ms
124,640 KB
testcase_28 AC 633 ms
124,268 KB
testcase_29 AC 660 ms
136,940 KB
testcase_30 AC 636 ms
125,656 KB
testcase_31 AC 671 ms
125,824 KB
testcase_32 WA -
testcase_33 AC 672 ms
124,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#yukicoder411G

#UnionFind
class UnionFind:
    def __init__(self,N): self._parent=[-1 for i in[0]*N]
    def find(self,v):  #頂点vの親を探し、経路圧縮する
        vertices=[]
        while self._parent[v]>=0: vertices.append(v);v=self._parent[v]
        for i in vertices: self._parent[i]=v
        return v
    def unite(self,x,y):  #頂点xとyを併合し、併合の有無を返す
        x,y = self.find(x),self.find(y)
        if x==y: return 0
        if self._parent[x]>self._parent[y]: x,y=y,x  #負値で管理
        self._parent[x]+=self._parent[y]; self._parent[y]=x; return 1
    def same(self,x,y):return self.find(x)==self.find(y)   #xとyは同一集合か返す
    def size(self,x):  return -self._parent[self.find(x)]  #xの集合のサイズを求める


#入力受取  UFで頂点を結合するが、「ここを追加すると閉路になる」という辺のみ除外する
N,K=map(int,input().split()); G=[[] for _ in range(N)]
UF=UnionFind(N); MOD=998244353; ExS,ExG=0,0
for _ in range(N):
    u,v=map(lambda x: int(x)-1,input().split())
    if UF.same(u,v): ExS,ExG=u,v
    else: UF.unite(u,v); G[u].append(v); G[v].append(u)

#なもりグラフのサイクルを検知  P[i]: 頂点iの親 とすれば復元可能
P=[-1]*N; visit=[(ExS,-1)]
for now,back in visit:
    P[now]=back
    for next in G[now]:
        if next!=back: visit.append((next,now))
cycle=[ExG]
for now in cycle:
    if now==ExS: break
    cycle.append(P[now])

#ようやく頂点を追加してグラフを完成させる
G[ExS].append(ExG); G[ExG].append(ExS)

#DP[i]: 頂点iを特定の色で塗ると決めたときの場合の数
CS=set(cycle)
DP=[1 for _ in range(N)]
for i in cycle:
    visit=[(i,i)]
    for now,back in visit:
        for next in G[now]:
            if next!=back and next not in CS: visit.append((next,now))
    for now,back in visit[::-1]:
        if now in CS: continue
        DP[back]=DP[back]*DP[now]%MOD*(K-1)%MOD

#sDP[i][x]: サイクルのi番目の要素について、色0で塗るならx=0
sDP=[[1]*2 for _ in range(len(cycle)+1)]
sDP[0][0]=DP[cycle[-1]]; sDP[0][1]=0  #cycle[-1]を色0で塗った、と仮定する
for i in range(len(cycle)):
    sDP[i+1][0]=sDP[i][1]*DP[cycle[i]]%MOD
    sDP[i+1][1]=( sDP[i][0]*(K-1)%MOD+sDP[i][1]*(K-2) )%MOD*DP[cycle[i]]%MOD

#答えを出力
print(sDP[-1][0]*K%MOD)
0