Wells
Time limit10sMemory limit1024 MB
Given a tree of N shelters, count the subsets of shelters with wells such that every simple path of exactly K shelters contains exactly one well, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
On the beautiful mountain of Velebit there are shelters. Exactly pairs of shelters are connected by a hiking path, and it is possible to travel between any pair of shelters using these paths.
Vila the Fairy likes hiking very much. It takes her exactly one day to traverse a hiking path connecting two shelters. She can use her magical abilities to appear at any shelter at the beginning of a day, and then spend the next days hiking without visiting the same shelter more than once. Thus, during each hike, Vila visits exactly shelters.
Vila gets thirsty while hiking, so she wants some shelters to have water wells. During every possible hike of hers, she wants to visit exactly one shelter with a well.
Determine whether a subset of shelters exists where wells can be placed to satisfy Vila's wishes. If so, count such subsets modulo .
Input
The first line contains two integers and ().
The next lines each contain two integers and (), describing a hiking path between shelters and .
The given paths form a tree.
Output
On the first line, print YES if a subset of shelters satisfying Vila's conditions exists, and NO otherwise.
On the second line, print the number of such subsets modulo .