結果

問題 No.2214 Products on Tree
ユーザー ShirotsumeShirotsume
提出日時 2022-11-25 10:23:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,449 ms / 3,000 ms
コード長 3,520 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 82,428 KB
実行使用メモリ 442,412 KB
最終ジャッジ日時 2024-10-07 08:56:04
合計ジャッジ時間 27,677 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,784 KB
testcase_01 AC 44 ms
54,656 KB
testcase_02 AC 196 ms
76,920 KB
testcase_03 AC 994 ms
158,204 KB
testcase_04 AC 973 ms
158,688 KB
testcase_05 AC 954 ms
151,780 KB
testcase_06 AC 667 ms
127,872 KB
testcase_07 AC 262 ms
91,904 KB
testcase_08 AC 207 ms
87,296 KB
testcase_09 AC 303 ms
98,324 KB
testcase_10 AC 666 ms
127,704 KB
testcase_11 AC 380 ms
104,704 KB
testcase_12 AC 452 ms
107,904 KB
testcase_13 AC 1,139 ms
161,744 KB
testcase_14 AC 971 ms
160,520 KB
testcase_15 AC 968 ms
161,092 KB
testcase_16 AC 977 ms
159,072 KB
testcase_17 AC 947 ms
158,256 KB
testcase_18 AC 644 ms
134,496 KB
testcase_19 AC 729 ms
140,484 KB
testcase_20 AC 639 ms
134,196 KB
testcase_21 AC 495 ms
121,088 KB
testcase_22 AC 882 ms
157,540 KB
testcase_23 AC 42 ms
54,656 KB
testcase_24 AC 41 ms
54,784 KB
testcase_25 AC 41 ms
54,784 KB
testcase_26 AC 42 ms
54,784 KB
testcase_27 AC 42 ms
54,912 KB
testcase_28 AC 966 ms
324,580 KB
testcase_29 AC 268 ms
97,280 KB
testcase_30 AC 683 ms
165,768 KB
testcase_31 AC 934 ms
165,172 KB
testcase_32 AC 954 ms
162,884 KB
testcase_33 AC 1,449 ms
442,412 KB
testcase_34 AC 1,425 ms
442,396 KB
testcase_35 AC 1,408 ms
305,536 KB
testcase_36 AC 1,381 ms
305,312 KB
testcase_37 AC 722 ms
157,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque, Counter
sys.setrecursionlimit(5 * 10 ** 5)
from pypyjit import set_param
set_param('max_unroll_recursion=-1')
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353

def solve(n, AB):
    dp = [[0] * 2 for _ in range(n)]
    s = set()
    graph = [[] for _ in range(n)]
    for a, b in AB:
        graph[a - 1].append(b - 1)
        graph[b - 1].append(a - 1)
    s.add(0)
    def dfs(now):
        dp[now][0] = dp[now][1] = 1
        des = []
        for to in graph[now]:
            if to not in s:
                des.append(to)
                s.add(to)
                dfs(to)

        for to in des:
            dp[now][0] *= (dp[to][0] + dp[to][1])
            dp[now][0] %= mod

        P = dp[now][0]
        dp[now][1] = P
        for to in des:
            P *= pow(dp[to][0] + dp[to][1], mod - 2, mod)
            P %= mod
            P *= dp[to][1]
            P %= mod
            dp[now][1] += P
            dp[now][1] %= mod
            P *= pow(dp[to][1], mod - 2, mod)
            P %= mod
            P *= (dp[to][0] + dp[to][1])
            P %= mod
    dfs(0)
    return dp[0][1]
        
class dsu():
    n=1
    parent_or_size=[-1 for i in range(n)]
    def __init__(self,N):
        self.n=N
        self.num = N
        self.parent_or_size=[-1 for i in range(N)]
    def merge(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        x=self.leader(a)
        y=self.leader(b)
        if x==y:
            return x
        self.num -= 1
        if (-self.parent_or_size[x]<-self.parent_or_size[y]):
            x,y=y,x
        self.parent_or_size[x]+=self.parent_or_size[y]
        self.parent_or_size[y]=x
        return x
    def same(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        return self.leader(a)==self.leader(b)
    def leader(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        if (self.parent_or_size[a]<0):
            return a
        self.parent_or_size[a]=self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]
    def size(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        return -self.parent_or_size[self.leader(a)]
    def groups(self):
        leader_buf=[0 for i in range(self.n)]
        group_size=[0 for i in range(self.n)]
        for i in range(self.n):
            leader_buf[i]=self.leader(i)
            group_size[leader_buf[i]]+=1
        result=[[] for i in range(self.n)]
        for i in range(self.n):
            result[leader_buf[i]].append(i)
        result2=[]
        for i in range(self.n):
            if len(result[i])>0:
                result2.append(result[i])
        return result2
    def count(self):
        return self.num


def solve_gu(n, AB):
    ans = 0
    for bit in range(1 << (n - 1)):
        U = dsu(n)
        for i in range(n - 1):
            if 1 & (bit >> i):
                a, b = AB[i]
                U.merge(a - 1, b - 1)
        cnt = 1
        for v in U.groups():
            cnt *= len(v)
            cnt %= mod
        ans += cnt
        ans %= mod
    return ans


n = ii()

AB = [li() for _ in range(n - 1)]

if n < 21:
    print(solve_gu(n, AB))
else:
    print(solve(n, AB))


0