Lost in a jungle

2.5

4 votes
Dynamic Programming, Graph, Math Basic, Tree, Trees, Algorithms
Problem

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 n1 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 u1,u2,...,uk where for each ui 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 n1 lines contains two integers xi,yi that displays an edge in the tree between nodes xi and yi

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 109+7.

Constraints
2n100000
1xi,yin
 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In this example, nodes number 2 and 4 are leaves. The only valid choosing leaves, would be to choose leaf number 4 so that nodes number 1 and 3 has 1 chosen leaf in their subtrees.

Editor Image

?