Number of connected components

You are given a tree, that is, an undirected, connected graph with no
cycles. Can you count how many (non-empty) connected subgraphs it
contains?

Input

Input consists of several trees, each one with the number of vertices n,
followed by its n − 1 edges. You can assume 1 ≤ n ≤ 10⁵, that vertices
are numbered between 0 and n − 1, and that the given edges indeed form a
tree.

Output

For every given tree, print its number of connected subgraphs. As this
number may be large, make the computations modulo 10⁸ + 7.

Problem information

Author: Edgar Moreno

Generation: 2026-01-25T11:52:55.342Z

© Jutge.org, 2006–2026.
https://jutge.org
