結果

問題 No.2531 Coloring Vertices on Namori
ユーザー mkawa2mkawa2
提出日時 2023-11-03 23:12:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 1,624 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 81,856 KB
実行使用メモリ 131,140 KB
最終ジャッジ日時 2023-11-03 23:12:56
合計ジャッジ時間 10,527 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,564 KB
testcase_01 AC 36 ms
53,564 KB
testcase_02 AC 36 ms
53,564 KB
testcase_03 AC 36 ms
53,564 KB
testcase_04 AC 36 ms
53,564 KB
testcase_05 AC 251 ms
131,140 KB
testcase_06 AC 37 ms
53,564 KB
testcase_07 AC 229 ms
131,140 KB
testcase_08 AC 380 ms
129,992 KB
testcase_09 AC 377 ms
130,116 KB
testcase_10 AC 418 ms
130,104 KB
testcase_11 AC 215 ms
125,764 KB
testcase_12 AC 211 ms
125,768 KB
testcase_13 AC 213 ms
125,768 KB
testcase_14 AC 368 ms
130,820 KB
testcase_15 AC 345 ms
130,812 KB
testcase_16 AC 345 ms
130,820 KB
testcase_17 AC 36 ms
53,564 KB
testcase_18 AC 36 ms
53,564 KB
testcase_19 AC 36 ms
53,564 KB
testcase_20 AC 363 ms
100,384 KB
testcase_21 AC 328 ms
100,160 KB
testcase_22 AC 302 ms
100,124 KB
testcase_23 AC 338 ms
100,232 KB
testcase_24 AC 347 ms
100,240 KB
testcase_25 AC 323 ms
100,084 KB
testcase_26 AC 313 ms
100,156 KB
testcase_27 AC 355 ms
100,132 KB
testcase_28 AC 331 ms
100,152 KB
testcase_29 AC 376 ms
100,144 KB
testcase_30 AC 301 ms
100,116 KB
testcase_31 AC 318 ms
100,240 KB
testcase_32 AC 294 ms
100,012 KB
testcase_33 AC 290 ms
100,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

def find_loop(to, root=0):
    n = len(to)
    fin = [0]*n
    res = []
    st = [(root, -1, True)]
    while st:
        u, par, first = st.pop()
        if res:
            if u == res[0]: return res
            if first: continue
            res.append(u)
        elif first:
            fin[u] = 1
            st.append((u, par, False))
            for v in to[u]:
                if v == par:
                    continue
                if fin[v]:
                    res.append(v)
                    break
                st.append((v, u, True))
        else:
            fin[u] = 2
    return res

n,k=LI()
to=[[] for _ in range(n)]
for _ in range(n):
    u,v=LI1()
    to[u].append(v)
    to[v].append(u)

loop=find_loop(to,0)

x,y=k,0
for _ in range(len(loop)-1):
    x,y=y,(x*(k-1)%md+y*(k-2)%md)%md

ans=pow(k-1,n-len(loop),md)*y%md
print(ans)
0