Sum of subtree sizes
Time limit2sMemory limit512 MB
Count all connected subgraphs of a tree and output the sum of their vertex counts modulo 1e9+7.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming, Combinatorics, DFS
- Solved
- No attempts yet
Problem
You are given an undirected tree . Its vertices are numbered from to .
A subtree of is a connected subgraph of that contains at least one vertex. The size of a subtree is the number of vertices in it.
Write a program that computes the sum of the sizes of all subtrees of .
Input
The first line contains the number of vertices ().
Each of the next lines contains one edge of the tree, given as the numbers and of the two vertices it joins (, ).
The given edges always form a tree.
Output
Print the sum of the sizes of all subtrees of modulo 1,000,000,007.