結果

問題 No.2047 Path Factory
ユーザー asumo0729asumo0729
提出日時 2022-09-18 22:10:10
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,738 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 82,752 KB
最終ジャッジ日時 2023-08-23 18:20:46
合計ジャッジ時間 8,992 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from operator import itemgetter
from collections import defaultdict, deque
import heapq
from heapq import heapify, heappop, _heapify_max, heappush
from bisect import bisect_left, bisect_right
import math
import itertools
import copy
from tracemalloc import start

stdin=sys.stdin
sys.setrecursionlimit(10 ** 7)
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()
Yp=lambda:print('Yes')
Np=lambda:print('No')
inf = 1 << 60
mod = 10 ** 9 + 7
mod = 998244353
eps = 1e-9
sortkey1 = itemgetter(0)
sortkey2 = lambda x: (x[0], x[1])



###############################################################

N = ip()
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = lp()
    u -= 1; v -= 1
    adj[u].append(v)
    adj[v].append(u)
dp = [[0 for _ in range(3)] for _ in range(N)]
visit = [False for _ in range(N)]
start = 0
deq = deque([(~start, -1), (start, -1)])
while deq:
    now, par = deq.pop()
    ## 行きがけ
    if now >= 0:
        visit[now] = True
        for nex in adj[now]:
            if visit[nex]:
                continue
            deq.append((~nex, now))
            deq.append((nex, now))
    ## 帰りがけ
    else:
        x0 = 1; x1 = 0; x2 = 0
        for ch in adj[~now]:
            if ch != par:
                x2 = (x2 * (dp[ch][1] + dp[ch][2]) + x1 * (dp[ch][0] + dp[ch][1])) % mod
                x1 = (x0 * (dp[ch][0] + dp[ch][1]) + x1 * (dp[ch][1] + dp[ch][2])) % mod
                x0 = x0 * (dp[ch][1] + dp[ch][2]) % mod
        dp[~now][0] = x0
        dp[~now][1] = x1
        dp[~now][2] = x2

ans = dp[0][1] + dp[0][2]
ans %= mod
print(ans)
0