結果

問題 No.2531 Coloring Vertices on Namori
ユーザー AngrySadEightAngrySadEight
提出日時 2023-10-19 02:22:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 651 ms / 2,000 ms
コード長 1,711 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 81,912 KB
実行使用メモリ 114,748 KB
最終ジャッジ日時 2023-10-19 02:22:29
合計ジャッジ時間 16,424 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,556 KB
testcase_01 AC 44 ms
55,556 KB
testcase_02 AC 45 ms
55,556 KB
testcase_03 AC 45 ms
55,556 KB
testcase_04 AC 44 ms
55,556 KB
testcase_05 AC 368 ms
112,648 KB
testcase_06 AC 44 ms
55,556 KB
testcase_07 AC 334 ms
112,644 KB
testcase_08 AC 533 ms
106,860 KB
testcase_09 AC 560 ms
107,136 KB
testcase_10 AC 542 ms
107,612 KB
testcase_11 AC 280 ms
114,748 KB
testcase_12 AC 291 ms
114,744 KB
testcase_13 AC 279 ms
114,748 KB
testcase_14 AC 466 ms
110,648 KB
testcase_15 AC 502 ms
110,568 KB
testcase_16 AC 462 ms
110,676 KB
testcase_17 AC 44 ms
55,556 KB
testcase_18 AC 45 ms
55,556 KB
testcase_19 AC 43 ms
55,556 KB
testcase_20 AC 645 ms
112,164 KB
testcase_21 AC 604 ms
110,380 KB
testcase_22 AC 636 ms
112,908 KB
testcase_23 AC 617 ms
113,068 KB
testcase_24 AC 650 ms
111,704 KB
testcase_25 AC 619 ms
111,856 KB
testcase_26 AC 630 ms
112,840 KB
testcase_27 AC 617 ms
112,316 KB
testcase_28 AC 632 ms
113,400 KB
testcase_29 AC 621 ms
111,828 KB
testcase_30 AC 651 ms
112,264 KB
testcase_31 AC 638 ms
112,132 KB
testcase_32 AC 617 ms
112,320 KB
testcase_33 AC 640 ms
112,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


class UnionFind:

    def __init__(self, N):
        self.tree_size = N
        self.parent = [i for i in range(N)]
        self.rank = [1 for _ in range(N)]

    def root(self, a):
        now = a
        while now != self.parent[now]:
            now = self.parent[now]
        return now

    def same(self, a, b):
        return self.root(a) == self.root(b)

    def merge(self, a, b):
        rta = self.root(a)
        rtb = self.root(b)
        if rta != rtb:
            if self.rank[rta] == self.rank[rtb]:
                self.parent[rtb] = self.parent[rta]
                self.rank[rta] += 1
            elif self.rank[rta] > self.rank[rtb]:
                self.parent[rtb] = self.parent[rta]
            else:
                self.parent[rta] = self.parent[rtb]


mod = 998244353

N, K = map(int, input().split())
graph = [[] for _ in range(N)]
tree = UnionFind(N)
dist = [N + 2 for _ in range(N)]
cnt = 0
dq = deque()
for i in range(N):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if tree.same(u, v):
        dist[u] = 0
        dq.append(u)
        while len(dq) > 0:
            p = dq.popleft()
            for x in graph[p]:
                if dist[x] < N:
                    continue
                dist[x] = dist[p] + 1
                dq.append(x)
        cnt = dist[v] + 1
        break
    else:
        graph[u].append(v)
        graph[v].append(u)
        tree.merge(u, v)


dp = [[0, 0] for _ in range(N)]
dp[0][0] = K

for i in range(1, N):
    dp[i][0] = dp[i - 1][1]
    dp[i][1] = (dp[i - 1][0] * (K - 1) + dp[i - 1][1] * (K - 2)) % mod

ans = dp[cnt - 1][1]
for i in range(N - cnt):
    ans = (ans * (K - 1)) % mod
print(ans)
0