結果
問題 | No.2531 Coloring Vertices on Namori |
ユーザー | detteiuu |
提出日時 | 2024-11-14 22:17:31 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 799 ms / 2,000 ms |
コード長 | 1,991 bytes |
コンパイル時間 | 394 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 237,852 KB |
最終ジャッジ日時 | 2024-11-14 22:17:57 |
合計ジャッジ時間 | 17,564 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
55,748 KB |
testcase_01 | AC | 42 ms
54,992 KB |
testcase_02 | AC | 42 ms
55,360 KB |
testcase_03 | AC | 41 ms
54,732 KB |
testcase_04 | AC | 42 ms
54,860 KB |
testcase_05 | AC | 461 ms
211,740 KB |
testcase_06 | AC | 41 ms
55,344 KB |
testcase_07 | AC | 475 ms
211,476 KB |
testcase_08 | AC | 778 ms
237,852 KB |
testcase_09 | AC | 796 ms
236,444 KB |
testcase_10 | AC | 799 ms
226,832 KB |
testcase_11 | AC | 282 ms
109,492 KB |
testcase_12 | AC | 314 ms
109,180 KB |
testcase_13 | AC | 289 ms
109,312 KB |
testcase_14 | AC | 719 ms
218,752 KB |
testcase_15 | AC | 724 ms
218,756 KB |
testcase_16 | AC | 703 ms
218,632 KB |
testcase_17 | AC | 42 ms
55,444 KB |
testcase_18 | AC | 42 ms
54,984 KB |
testcase_19 | AC | 43 ms
55,596 KB |
testcase_20 | AC | 619 ms
105,344 KB |
testcase_21 | AC | 594 ms
105,216 KB |
testcase_22 | AC | 614 ms
105,224 KB |
testcase_23 | AC | 590 ms
105,732 KB |
testcase_24 | AC | 625 ms
105,352 KB |
testcase_25 | AC | 598 ms
104,980 KB |
testcase_26 | AC | 623 ms
104,832 KB |
testcase_27 | AC | 618 ms
105,404 KB |
testcase_28 | AC | 629 ms
104,996 KB |
testcase_29 | AC | 606 ms
105,160 KB |
testcase_30 | AC | 676 ms
105,712 KB |
testcase_31 | AC | 623 ms
106,076 KB |
testcase_32 | AC | 664 ms
105,240 KB |
testcase_33 | AC | 640 ms
105,364 KB |
ソースコード
from types import GeneratorType from collections import deque def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc N, K = map(int, input().split()) G = [[] for _ in range(N)] for _ in range(N): u, v = map(int, input().split()) G[u-1].append(v-1) G[v-1].append(u-1) MOD = 998244353 @bootstrap def dfs(n, pre): visited[n] = True stack.append(n) for v in G[n]: if not visited[v]: yield dfs(v, n) elif v != pre and not finished[v]: for s in stack: cycle.append(s) cycle.append(v) finished[n] = True stack.pop() yield stack = [] cycle = [] visited = [False]*N finished = [False]*N dfs(0, -1) c = cycle.pop() cycle = cycle[::-1] while cycle[-1] != c: cycle.pop() dp = [[0]*2 for _ in range(len(cycle))] dp[0][0] = 1 for i in range(len(cycle)-1): dp[i+1][1] += dp[i][0]*(K-1) dp[i+1][1] %= MOD dp[i+1][0] += dp[i][1] dp[i+1][0] %= MOD dp[i+1][1] += dp[i][1]*(K-2) dp[i+1][1] %= MOD ans = dp[-1][1]*K%MOD S = set(cycle) visited = [False]*N for c in cycle: visited[c] = True for c in cycle: for v in G[c]: if v not in S: que = deque() que.append(v) visited[v] = True cnt = 1 while que: n = que.popleft() for v in G[n]: if not visited[v]: visited[v] = True que.append(v) cnt += 1 ans *= pow(K-1, cnt, MOD) ans %= MOD print(ans)