結果

問題 No.3373 Partial Complement Tree
コンテスト
ユーザー titia
提出日時 2025-11-21 22:37:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 650 ms / 2,000 ms
コード長 1,279 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 82,812 KB
実行使用メモリ 163,912 KB
最終ジャッジ日時 2025-11-21 22:37:45
合計ジャッジ時間 11,458 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=998244353

T=int(input())

for tests in range(T):
    N=int(input())

    E=[[] for i in range(N)]

    for i in range(N-1):
        x,y=map(int,input().split())
        x-=1
        y-=1
        E[x].append(y)
        E[y].append(x)
        
    ROOT=0

    QUE=[ROOT] 
    Parent=[-1]*N
    Parent[ROOT]=N # ROOTの親を定めておく.
    Child=[[] for i in range(N)]
    TOP_SORT=[] # トポロジカルソート

    while QUE: # トポロジカルソートと同時に親を見つける
        x=QUE.pop()
        TOP_SORT.append(x)
        for to in E[x]:
            if Parent[to]==-1:
                Parent[to]=x
                Child[x].append(to)
                QUE.append(to)

    DP=[[0]*4 for i in range(N)]

    ANS=0

    for x in TOP_SORT[::-1]:
        DP[x][0]=1
        for c in Child[x]:
            DP[x][1]+=DP[c][0]
            DP[x][2]+=DP[c][1]
            DP[x][3]+=DP[c][2]

        ANS+=DP[x][3]

        X=[]
        Y=[]

        for c in Child[x]:
            X.append(DP[c][0])
            Y.append(DP[c][1])

        SUM=sum(Y)

        for i in range(len(X)):
            ANS+=X[i]*(SUM-Y[i])

    print(ANS%mod)
        

        
        
                    
                

    
    

    
0