Lost in a jungle
Practice
2.5 (4 votes)
Dynamic programming
Graph
Math basic
Tree
Trees
Algorithms
Problem
91% Success 994 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

At the weekend, Simon decided to go for a little picnic in the jungle. At the end of the night when he wanted to go home, he found out that he was actually lost! He started wandering around the jungle until he got tired and sat under a tree.
Since Simon is a computer scientist, he saw a tree, as $$n$$ nodes numbered from $$1$$ to $$n$$, that connected with $$n - 1$$ edges to each other so that each node $$v$$ has a unique path to each node $$u$$. 
Also of course every tree has a root, so Simon numbered the root as node $$1$$.
He was looking at the tree and suddenly came up with a question. In how many ways can he choose a set of leaves of the tree in such a way that in the subtree of each node of the tree (except leaves themselves), there would be an odd number of chosen leaves. Your task is to help him determine the answer.

Note

  • A leaf is a node of a tree that has only one edge connected to it (but the root is never considered a leaf).
  • A subtree of $$v$$ are nodes $$u_{1}, u_{2}, ..., u_{k}$$ where for each $$u_{i}$$ its path to root contains $$v$$.

Input format

  • The first line contains integers $$n$$ denoting the number of nodes in the tree.
  • Each of next $$n - 1$$ lines contains two integers $$x_{i}, y_{i}$$ that displays an edge in the tree between nodes $$x_{i}$$ and $$y_{i}$$. 

Output format

In the only line of output, you should output the number of ways. Since the number could be very large, you have to output the answer modulo $$10^{9} + 7$$.

Constraints
$$2 \leq n \leq 100000$$
$$1 \leq x_{i}, y_{i} \leq n$$
 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
4 votes
Tags:
Easy-Medium
Points:30
6 votes
Tags:
Dynamic ProgrammingAlgorithms1-DTreesDFS