結果

問題 No.3412 Christmas Tree Coloring
コンテスト
ユーザー LyricalMaestro
提出日時 2026-08-01 12:38:30
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 438 ms / 2,000 ms
+ 504µs
コード長 1,642 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 242 ms
コンパイル使用メモリ 95,732 KB
実行使用メモリ 135,956 KB
最終ジャッジ日時 2026-08-01 12:38:40
合計ジャッジ時間 8,354 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# https://yukicoder.me/problems/no/3412

from  collections import deque

MOD = 998244353


def main():
    N = int(input())
    next_nodes = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        next_nodes[a - 1].append(b - 1)
        next_nodes[b - 1].append(a - 1)

    next_childs = [[] for _ in range(N)]
    total_childs = [0] * N
    parents = [-2] * N
    parents[0] = -1
    stack = deque()
    stack.append((0, 0))
    while len(stack) > 0:
        v, index = stack.pop()

        while index < len(next_nodes[v]):
            w = next_nodes[v][index]
            if w == parents[v]:
                index += 1
                continue

            parents[w] = v
            stack.append((v, index + 1))
            stack.append((w, 0))
            break

        if index == len(next_nodes[v]):
            p = parents[v]
            if p != -1:
                total_childs[p] += 1 + total_childs[v]
                next_childs[p].append(1 + total_childs[v])

    answer = 0
    for base_i in range(N):
        rest_p = N - 1 - total_childs[base_i]
        if rest_p > 0:
            next_childs[base_i].append(rest_p)

        two_num = 0
        for x in next_childs[base_i]:
            if x > 1:
                two_num += 1

        if two_num > 0:
            ans = pow(2, len(next_nodes[base_i]), MOD)
            answer += ans
            answer %= MOD
        else:
            ans = pow(2, len(next_nodes[base_i]), MOD)
            ans -= 2
            ans %= MOD
            answer += ans
            answer %= MOD
    print(answer)




if __name__ == "__main__":
    main()
0