Problem:
Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black.
Here, it is not allowed to paint two adjacent vertices both in black.

Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.

Sample Input 1

3
1 2
2 3

Sample Output 1

5

There are five ways to paint the vertices, as follows:

Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

9

There are nine ways to paint the vertices, as follows:

Sample Input 3

1

Sample Output 3

2

Sample Input 4

10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

157

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;

namespace Contest
{
    class Program
    {
        private Scanner sc;
        private int N;
        private List<int>[] E, Child;
        private const long Mod = (long)1e9 + 7;
        private long[] W, B;
        private bool[] C;
        public void Solve()
        {
            sc = new Scanner();
            N = sc.NextInt();
            E = new List<int>[N];
            Child = new List<int>[N];
            for (int i = 0; i < N; i++)
            {
                E[i] = new List<int>();
                Child[i] = new List<int>();
            }

            for (int i = 0; i < N - 1; i++)
            {
                int x = sc.NextInt() - 1;
                int y = sc.NextInt() - 1;
                E[x].Add(y);
                E[y].Add(x);
            }
            W = new long[N];
            B = new long[N];
            C = new bool[N];
            for (int i = 0; i < N; i++)
            {
                W[i] = -1;
                B[i] = -1;
            }
            Calc(0);
            Console.WriteLine((White(0) + Black(0)) % Mod);
        }

        private void Calc(int i)
        {
            C[i] = true;
            foreach (var i1 in E[i])
            {
                if (C[i1]) continue;
                Child[i].Add(i1);
                Calc(i1);
            }

            C[i] = false;
        }

        // 0をroot iを白色
        private long White(int i)
        {
            if (W[i] != -1)
            {
                return W[i];
            }
            long res = 1;
            foreach (var i1 in Child[i])
            {
                res *= (White(i1) + Black(i1)) % Mod;
                res %= Mod;
            }
            W[i] = res;
            return res;
        }

        // 〃　黒色
        private long Black(int i)
        {
            if (B[i] != -1)
            {
                return B[i];
            }
            long res = 1;
            foreach (var i1 in Child[i])
            {
                res *= White(i1);
                res %= Mod;
            }
            B[i] = res;
            return res;
        }


        static void Main() => new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _stream = new StreamReader(Console.OpenStandardInput());
        _pos = 0;
        _line = new string[0];
        _separator = ' ';
    }
    private readonly char _separator;
    private readonly StreamReader _stream;
    private int _pos;
    private string[] _line;
    #region get a element
    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = _stream.ReadLine().Split(_separator);
            _pos = 0;
        }
        return _line[_pos++];
    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    #endregion
    #region convert array
    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }
    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }
    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }
    #endregion
    #region get array
    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = _stream.ReadLine().Split(_separator);
        _pos = _line.Length;
        return _line;
    }
    public int[] IntArray()
    {
        return ToIntArray(Array());
    }
    public long[] LongArray()
    {
        return ToLongArray(Array());
    }
    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }
    #endregion
}