結果
問題 | No.2214 Products on Tree |
ユーザー |
|
提出日時 | 2023-02-10 21:47:31 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 566 ms / 3,000 ms |
コード長 | 1,213 bytes |
コンパイル時間 | 393 ms |
コンパイル使用メモリ | 82,472 KB |
実行使用メモリ | 152,720 KB |
最終ジャッジ日時 | 2024-07-07 16:01:03 |
合計ジャッジ時間 | 14,946 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
import sys,random,bisectfrom collections import deque,defaultdictfrom heapq import heapify,heappop,heappushfrom itertools import permutationsfrom math import gcdfrom fractions import Fractioninput = lambda :sys.stdin.readline().rstrip()mi = lambda :map(int,input().split())li = lambda :list(mi())mod = 998244353N = int(input())edge = [[] for v in range(N)]for _ in range(N-1):a,b = mi()edge[a-1].append(b-1)edge[b-1].append(a-1)parent = [-1] * Ntopo = []deq = deque([0])while deq:v = deq.popleft()topo.append(v)for nv in edge[v]:if nv == parent[v]:continueparent[nv] = vdeq.append(nv)dp = [[0,0] for v in range(N)]for v in topo[::-1]:dp[v] = [1,1]for nv in edge[v]:if nv == parent[v]:continuea,b = dp[v]c,d = dp[nv]ndp = [0,0]"""つなげる場合"""ndp[0] += a * c % modndp[1] += b * c % mod + a * d % mod"""つなげない場合"""ndp[0] += a * (d) % modndp[1] += b * (d) % modndp[0] %= modndp[1] %= moddp[v] = ndpprint(dp[0][1])