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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

#define ANS(f) if(f) cout << "YES" << endl; else cout << "NO" << endl;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template<typename T>
void readv(vector<T> &a){ REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a){ REP(i, a.size()){cin >> a[i]; a[i]--;} }
void debug(mat m){REP(i, m.size()){ REP(j, m[i].size()){ cout << m[i][j] << ","; } cout << endl; }}

struct edge{int to, cost;};

class Graph
{
public:
    int V;
    vector<vector<edge>> G;

    vec gen, par;
    mat chi, genlist;
    int depth;

    Graph(int V): V(V){
        G = vector<vector<edge>>(V, vector<edge>(0));
    }

    void add_edge(int from, int to, int cost){
        G[from].push_back(edge({to, cost}));
    }

    void add_edge2(int v1, int v2, int cost){
        add_edge(v1, v2, cost);
        add_edge(v2, v1, cost);
    }

    void dfs(int v, int d){
        gen[v] = d;
        depth = max(depth, d + 1);
        REP(k, G[v].size()){
            if(G[v][k].to == par[v]) continue;
            par[G[v][k].to] = v;
            chi[v].push_back(G[v][k].to);
            dfs(G[v][k].to, d + 1);
        }
    }

    mat makegenlist(){
        REP(v, V) genlist[gen[v]].push_back(v);
        return genlist;
    }

    void analyzeTree(int root){
        gen = vec(V); par = vec(V);
        chi = mat(V, vec(0));
        depth = 0;
        par[root] = -1;
        dfs(root, 0);

        genlist = mat(depth, vec(0));
        makegenlist();
    }

};

signed main(){

    int N; cin >> N;
    Graph T(N);
    int x, y;
    REP(i, N - 1){
        cin >> x >> y;
        T.add_edge2(x - 1, y - 1, 0);
    }
    T.analyzeTree(0);
    int mod = 1000000007;

    mat dp(N, vec(2, 0));
    IREP(d, T.depth){
        for(int v: T.genlist[d]){
            int v0 = 1;
            REP(i, T.chi[v].size()){
                v0 *= (dp[T.chi[v][i]][0] + dp[T.chi[v][i]][1]);
                v0 %= mod;
            }
            dp[v][0] = v0;
            int v1 = 1;
            REP(i, T.chi[v].size()){
                v1 *= dp[T.chi[v][i]][0];
                v1 %= mod;
            }
            dp[v][1] = v1;
        }
    }
    cout << (dp[0][0] + dp[0][1]) % mod;
    
    return 0;
}