結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,708 KB
testcase_01 AC 38 ms
53,708 KB
testcase_02 AC 38 ms
53,708 KB
testcase_03 AC 39 ms
53,708 KB
testcase_04 AC 38 ms
53,708 KB
testcase_05 AC 378 ms
156,352 KB
testcase_06 AC 38 ms
53,708 KB
testcase_07 WA -
testcase_08 AC 638 ms
158,312 KB
testcase_09 AC 616 ms
158,308 KB
testcase_10 AC 636 ms
158,304 KB
testcase_11 AC 281 ms
132,900 KB
testcase_12 AC 283 ms
132,892 KB
testcase_13 AC 307 ms
132,364 KB
testcase_14 AC 562 ms
160,160 KB
testcase_15 AC 606 ms
160,156 KB
testcase_16 AC 598 ms
160,156 KB
testcase_17 WA -
testcase_18 AC 38 ms
53,708 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 642 ms
127,520 KB
testcase_24 AC 667 ms
129,528 KB
testcase_25 WA -
testcase_26 AC 651 ms
123,544 KB
testcase_27 AC 643 ms
124,744 KB
testcase_28 AC 651 ms
124,660 KB
testcase_29 AC 660 ms
137,064 KB
testcase_30 AC 668 ms
125,772 KB
testcase_31 AC 638 ms
126,068 KB
testcase_32 WA -
testcase_33 AC 626 ms
124,356 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)]; rev=pow(K-1,MOD-2,MOD)
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]+sDP[i][1]*rev%MOD*(K-2))*DP[cycle[i]]%MOD*(K-1)%MOD

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