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



Sample input:
3
1 2
2 3


Sample output:
5

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100005)
mod=1000000007
# https://ikatakos.com/pot/programming_algorithm/contest_history/atcoder/2019/0106_educational_dp_2

def dfs(v,a):
    w,b=,1,1
    for u in links[v]:
        if u==a:
            continue
        cw,cb=dfs(u,v)
        w*=cw*cb
        b*=cw
        w%=mod
        b%=mod
    return w,b


n=int(input())
links=[set() for i in range(n)]

for line in sys.stdin.readlines():
    x,y=map(int, input().split())
    x-=1
    y-=1
    links[x].add(y)
    links[y].add(x)

print(sum(dfs(0,None)%mod))

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop
import collections
import math
import heapq
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
inf = float("inf")


def main():
    N = int(input())

    E = {}
    for i in range(0, N+1):
    E[i] = []
    for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    E[a].append(b)
    E[b].append(a)

    dp = [[0 for i in range(2)] for j in range(100100)]

    def dfs(c, p):
    dp[c][0], dp[c][1] = 1, 1
    for to in E[c]:
        if to != p:
            dfs(to, c)
            dp[c][0] *= dp[to][0] + dp[to][1]
            dp[c][1] *= dp[to][0]

    dfs(0, -1)

    ans = (dp[0][0] + dp[0][1]) % MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop
import collections
import math
import heapq
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
inf = float("inf")


def main():
   N = int(input())

    E = {}
    for i in range(0, N+1):
        E[i] = []
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        E[a].append(b)
        E[b].append(a)

    dp = [[0 for i in range(2)] for j in range(100100)]


    def dfs(c, p):
        dp[c][0], dp[c][1] = 1, 1
        for to in E[c]:
            if to != p:
                dfs(to, c)
                dp[c][0] *= dp[to][0] + dp[to][1]
                dp[c][1] *= dp[to][0]


    dfs(0, -1)

    ans = (dp[0][0] + dp[0][1]) % MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)

MOD = 1e9+7

n = int(input())

dp = [[-1]*2 for i in range(n+1)]

g = [list() for i in range(n)]

for i in range(n-1):
    x, y = list(map(int, input().split()))
    x-=1
    y-=1
    g[x].append(y)
    g[y].append(x)

def dfs(u, p, c):
    
    if dp[u][c]!=-1:
        return dp[u][c]

    ans=1
    for v in g[u]:
        if v==p:
            continue
        ans = (ans*(dfs(v, u, 0)+(dfs(v, u, 1) if c==0 else 0)))
    
    dp[u][c]=ans
    return ans

print(int((dfs(0, -1, 0)+dfs(0, -1, 1))%MOD))

Submission status: Runtime Error

Solution code:
# @author 

import sys


class PIndependentSet:
    def solve(self):
        from collections import defaultdict

        MOD = 10 ** 9 + 7

        def dfs(s):
            done[s] = 1
            dp[s][0] = 1
            dp[s][1] = 1
            for x in adj[s]:
                if done[x]:
                    continue
                _in = True
                dfs(x)
                dp[s][0] = (dp[s][0] * dp[x][1]) % MOD
                dp[s][1] = (dp[s][1] * (dp[x][0] + dp[x][1])) % MOD

        n = int(input())
        adj = defaultdict(list)

        for i in range(n - 1):
            x, y = [int(_) for _ in input().split()]
            x -= 1
            y -= 1
            adj[x].append(y)
            adj[y].append(x)

        dp = [[0, 0] for _ in range(n)]
        done = [0] * n
        dfs(0)

        print(dp[0][0] + dp[0][1])

solver = PIndependentSet()
input = sys.stdin.readline

solver.solve()

Submission status: Runtime Error

Solution code:
import fileinput
from collections import defaultdict

def longest_path(edges):
    modulo = 10**9 + 7
    graph = defaultdict(list)
    for v_a, v_b in edges:
        graph[v_a].append(v_b)
        graph[v_b].append(v_a)
    
    set_count_black = [0] * (len(graph) + 1)
    set_count_white = [0] * (len(graph) + 1)

    node_stack = []
    nodes_on_stack = [True] + [False] * node_count

    node_stack.append([1, 0])
    nodes_on_stack[1] = True

    while node_stack:
        top_node, last_child = node_stack[-1]
        if top_node in graph and last_child < len(graph[top_node]):
            node_stack[-1][1] += 1
            current_child = graph[top_node][last_child]
            if nodes_on_stack[current_child] == False:
                node_stack.append([current_child, 0])
                nodes_on_stack[current_child] = True
        else:
            node, children = node_stack.pop()
            nodes_on_stack[node] = False

            if children == 1 and node != 1:
                set_count_black[node] = 1
                set_count_white[node] = 1
            else:
                bc = 1
                wc = 1
                for child in graph[node]:
                    if nodes_on_stack[child]:
                        continue
                    bc *= set_count_white[child]
                    wc *= (set_count_white[child] + set_count_black[child])
                set_count_white[node] = wc % modulo
                set_count_black[node] = bc % modulo
    return (set_count_white[1] + set_count_black[1]) % modulo

inp = fileinput.FileInput()

node_count = int(inp.readline())
edges = []

for _ in range(node_count - 1):
    split = inp.readline().split(" ")
    edges.append(
        (int(split[0]), int(split[1]))
    )

print(longest_path(edges))

Submission status: Runtime Error

Solution code:
import fileinput
from collections import defaultdict

def longest_path(edges):
    modulo = 10**9 + 7
    graph = defaultdict(list)
    for v_a, v_b in edges:
        graph[v_a].append(v_b)
        graph[v_b].append(v_a)
    
    set_count_black = [0] * (len(graph) + 1)
    set_count_white = [0] * (len(graph) + 1)

    node_stack = []
    nodes_on_stack = [True] + [False] * node_count

    node_stack.append([1, 0])
    nodes_on_stack[1] = True

    while node_stack:
        top_node, last_child = node_stack[-1]
        if top_node in graph and last_child < len(graph[top_node]):
            node_stack[-1][1] += 1
            current_child = graph[top_node][last_child]
            if nodes_on_stack[current_child] == False:
                node_stack.append([current_child, 0])
                nodes_on_stack[current_child] = True
        else:
            node, children = node_stack.pop()
            nodes_on_stack[node] = False

            if children == 1 and node != 1:
                set_count_black[node] = 1
                set_count_white[node] = 1
            else:
                bc = 1
                wc = 1
                for child in graph[node]:
                    if nodes_on_stack[child]:
                        continue
                    bc *= set_count_white[child]
                    wc *= (set_count_white[child] + set_count_black[child])
                set_count_white[node] = wc % modulo
                set_count_black[node] = bc % modulo
    return set_count_white[1] + set_count_black[1]

inp = fileinput.FileInput()

node_count = int(inp.readline())
edges = []

for _ in range(node_count - 1):
    split = inp.readline().split(" ")
    edges.append(
        (int(split[0]), int(split[1]))
    )

print(longest_path(edges))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template

# dfsの様々な実装例 in Python
# https://qiita.com/drken/items/a803d4fc4a727e02f7ba
# BEGIN CUT HERE


# v : 現在探索中の頂点，p : vの親（v が親のときは -1）
def dfs_tree(G,v,p):
    '''木上のdfsの実装例'''
    for nv in G[v]:
        if nv == p:
            continue
        dfs_tree(G, nv, v)
def main1():
    N = ii()
    G = [[] for i in range(N)]
    for i in range(N - 1):
        a, b = mi()
        G[a].append(b)
        G[b].append(a)
    
    root = 0
    dfs_tree(G, root, -1)

# 0を頂点とした木の根からの深さ，各頂点を根とした部分木のサイズを求める
# さっきのやつからすこし改造する
depth = [0] * (10 ** 5 + 10)
subtree_size = [0] * (10 ** 5 + 10)
# d : 頂点 v の深さ
def dfs_tree_kai(G, v, p, d):
    
    depth[v] = d
    for nv in G[v]:
        if nv == p:
            continue
        else:
            dfs_tree_kai(G, nv, v, d + 1)
    
    # 帰りがけ時に，部分木のサイズを求める
    subtree_size[v] = 1  # 自分自身
    for c in G[v]:
        if c == p:
            continue
        subtree_size[v] += subtree_size[c]
    return depth, subtree_size

def main2():
    '''木DPの簡単な例（部分木のサイズを求める）'''
    N = ii()
    G = [[] for i in range(N)]
    for i in range(N - 1):
        a, b = mi()
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    
    root = 0  # 仮に頂点0を根とする
    dfs_tree_kai(G, root, -1, 0)
    
    for v in range(N):
        print(v, ": depth = ", depth[v], ", subtree_size = ", subtree_size[v])
    
def EDPC_P():
    N = ii()
    dp = [[1, 1] for _ in range(N)]
    G = [[] for i in range(N)]
    for i in range(N - 1):
        a, b = mi()
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    depth = [0]*N
    def dfs_EDPC_P(G, v, p, d):
        depth[v] = d
        for nv in G[v]:
            if nv == p:
                continue
            dfs_EDPC_P(G, nv, v, d + 1)
        for c in G[v]:
            if c == p:
                continue
            dp[v][0] *= (dp[c][0] + dp[c][1]) % (10 ** 9 + 7)
            dp[v][1] *= dp[c][0]
    dfs_EDPC_P(G,0,-1,0)
    print((dp[0][0]+dp[0][1])%(10**9+7))
    
# END CUT HERE

if __name__ == '__main__':
    EDPC_P()

Submission status: Runtime Error

Solution code:
BLK = 1
WHT = 0
MOD = 10 ** 9 + 7


def calcdp(r, p, bw):
    if dp[r][bw] != -1:
        return dp[r][bw]

    if p == 0:
        cs = H[r]
    else:
        cs = H[r] - {p}

    if len(cs) == 0:
        dp[r][bw] = 1
        return 1
    else:
        if bw == BLK:
            res = 1
            for c in cs:
                res = (res * calcdp(c, r, WHT)) % MOD
            dp[r][BLK] = res
            return res
        else:
            res = 1
            for c in cs:
                res = (res * (calcdp(c, r, WHT) + calcdp(c, r, BLK))) % MOD
            dp[r][WHT] = res

            return res


N = int(input())
H = [set() for _ in range(N + 1)]
for _ in range(1, N):
    x, y = map(int, input().split())
    H[x].add(y)
    H[y].add(x)

dp = [[-1] * 2 for _ in range(N + 1)]

print((calcdp(1, 0, BLK) + calcdp(1, 0, WHT)) % MOD)

Submission status: Runtime Error

Solution code:


function Main(input) {
  input = input.split("\n");
  var N = parseInt(input[0]);
  var edge = new Array(N);
  var dp = new Array(N);
  var mod = Math.pow(10,9)+7;
  for(var i=0;i<N-1;i++){
      tmp = input[i+1].split(" ").map(i => parseInt(i));
      if(edge[tmp[0]-1]==undefined){
        edge[tmp[0]-1]=[];
      }
      if(edge[tmp[1]-1]==undefined){
        edge[tmp[1]-1]=[];
      }
      edge[tmp[0]-1].push(tmp[1]-1);
      edge[tmp[1]-1].push(tmp[0]-1);
  }
  var flag=new Array(N).fill(0);
  if(N!=1){
    flag[0]=1;
    for(var i=0;i<N;i++){
      if(edge[i].length==1){
        dp[i]=[1,1];
        flag[i]=1;
      }else{
        dp[i]=[0,0];
      }
    }
    dp[0]=[0,0];
    var stack = [0];

    while(stack.length!=0){
      var term=0;
      var white=1;
      var black=1;
      var length=stack.length-1;
      for(var i=0;i<edge[stack[length]].length;i++){
        if(flag[edge[stack[length]][i]]==0){
          flag[edge[stack[length]][i]]=1;
          stack.push(edge[stack[length]][i]);
          term=1;
        }else{
          var sub1=Math.max(1,(dp[edge[stack[length]][i]][0]+dp[edge[stack[length]][i]][1])%mod);
          var sub2=Math.max(1,dp[edge[stack[length]][i]][0])
          white=(white*sub1)%mod;
          black=(black*sub2)%mod;
        }
      }
      if(term==0){
        dp[stack[length]]=[white,black];
        stack.pop();
      }
    }
    var ans=(dp[0][0]+dp[0][1])%mod;
  }else{
    var ans=2;
  }
  console.log('%s', ans);
  return ans;
}
// "実行する"ボタンを押した時に実行される関数 (デバッグ用)
function debug(n){
  var input = document.getElementById("input"+n).value;
  var ans=Main(input);
  var result="WA";
  if(ans==document.getElementById("output"+n).value.split("\n")[0]){
    result="AC";
  }
  document.getElementById("result"+n).innerHTML=result;
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

MOD=10**9+7
 
n=int(input())
edge=[[] for _ in range(n)]
for _ in range(n-1):
  p,q = [int(i)-1 for i in input().split()]
  edge[p].append(q)
  edge[q].append(p)

flag=[0]*n

if n==1:
  ans=2
else:
  dp=[[0] * 2 for i in range(n)]
  for i in range(n):
    if len(edge[i])==1:
      dp[i]=[1,1]
      flag[i]=1
    else:
      dp[i]=[0,0]
  dp[0]=[0,0]
  stack = [0]
  while len(stack)!=0:
    term=0
    white=1
    black=1
    for i in range(len(edge[stack[len(stack)-1]])):
      if flag[edge[stack[len(stack)-1]]]==0:
        flag[edge[stack[len(stack)-1]][i]]=1
        stack.append(edge[stack[len(stack)-1]][i])
        term=1
      else:
        sub1=max(1,(dp[edge[stack[len(stack)-1]][i]][0]+dp[edge[stack[len(stack)-1]][i]][0])%MOD)
        sub2=max(1,dp[edge[stack[len(stack)-1]][i]][0])
        white=(white*sub1)%MOD
        black=(black*sub2)%MOD
    if term == 0:
      dp[stack[len(stack)-1]]=[white,black]
      stack.pop()
  
  ans = (dp[0][0]+dp[0][1])%MOD


print(ans)

Submission status: Runtime Error

Solution code:
MOD=10**9+7
 
n=int(input())
edge=[[] for _ in range(n)]
for _ in range(n-1):
  p,q = [int(i)-1 for i in input().split()]
  edge[p].append(q)
  edge[q].append(p)

flag=[0]*n

if n==1:
  ans=2
else:
  dp=[[0] * 2 for i in range(n)]
  for i in range(n):
    if len(edge[i])==1:
      dp[i]=[1,1]
      flag[i]=1
    else:
      dp[i]=[0,0]
  dp[0]=[0,0]
  stack = [0]
  while len(stack)!=0:
    term=0
    white=1
    black=1
    for i in range(len(edge[stack[len(stack)-1]])):
      if flag[edge[stack[len(stack)-1]]]==0:
        flag[edge[stack[len(stack)-1]][i]]=1
        stack.append(edge[stack[len(stack)-1]][i])
        term=1
      else:
        sub1=max(1,(dp[edge[stack[len(stack)-1]][i]][0]+dp[edge[stack[len(stack)-1]][i]][0])%MOD)
        sub2=max(1,dp[edge[stack[len(stack)-1]][i]][0])
        white=(white*sub1)%MOD
        black=(black*sub2)%MOD
    if term == 0:
      dp[stack[len(stack)-1]]=[white,black]
      stack.pop()
  
  ans = (dp[0][0]+dp[0][1])%MOD


print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
MOD = 10**9+7
E = {}
for i in range(0,N+1):
    E[i] = []
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    E[a].append(b)
    E[b].append(a)

dp = [ [0 for i in range(2)] for j in range(1000100)]

def dfs(c,p):
    dp[c][0],dp[c][1] = 1,1
    for to in E[c]:
        if to != p:
            dfs(to,c)
            dp[c][0] *= dp[to][0] + dp[to][1]
            dp[c][1] *= dp[to][0]
            dp[c][0] %= MOD
            dp[c][1] %= MOD

dfs(0,-1)

ans = (dp[0][0] + dp[0][1]) % MOD
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
MOD = 10**9+7
E = {}
for i in range(0,N+1):
    E[i] = []
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    E[a].append(b)
    E[b].append(a)

dp = [ [0 for i in range(2)] for j in range(100100)]

def dfs(c,p):
    dp[c][0],dp[c][1] = 1,1
    for to in E[c]:
        if to != p:
            dfs(to,c)
            dp[c][0] *= dp[to][0] + dp[to][1]
            dp[c][1] *= dp[to][0]
            dp[c][0] %= MOD
            dp[c][1] %= MOD

dfs(0,-1)

ans = (dp[0][0] + dp[0][1]) % MOD
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
MOD = 10**9+7
E = {}
for i in range(0,N):
    E[i] = []
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    E[a].append(b)
    E[b].append(a)

dp = [ [0 for i in range(2)] for j in range(100100)]

def dfs(c,p):
    dp[c][0],dp[c][1] = 1,1
    for to in E[c]:
        if to != p:
            dfs(to,c)
            dp[c][0] *= dp[to][0] + dp[to][1]
            dp[c][1] *= dp[to][0]

dfs(0,-1)

ans = (dp[0][0] + dp[0][1]) % MOD
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
edge=[[] for _ in range(n)]
for _ in range(n-1):
    p,q = [int(i)-1 for i in input().split()]
    edge[p].append(q)
    edge[q].append(p)

    
used = [0]*n
ansb = [0]*n
answ = [0]*n

def dfs(x):
    if used[x]: return (ansb[x], answ[x])
    used[x] = 1
    cb=1; cw=1;
    for e in edge[x]:
        if used[e]: continue
        eb,ew = dfs(e)
        cb*=ew
        cw*=(eb+ew)
    ansb[x]=cb
    answ[x]=cw
    return (cb,cw)

print(sum(dfs(0)))

#print(used)
#print(ansb)
#print(answ)

Submission status: Runtime Error

Solution code:
n = int(input())
mod = 10 ** 9 + 7
node = [[] for _ in range(n)]
for i in range(n - 1):
    x, y = map(int, input().split())
    node[x - 1].append(y - 1)
    node[y - 1].append(x - 1)

def f(x, prev):
    cnt = 1
    for i in node[x]:
        if i != prev:
            cnt *= g(i, x)
            cnt %= mod
    return g(x, prev) + cnt

def g(x, prev):
    cnt = 1
    for i in node[x]:
        if i != prev:
            cnt *= f(i, x)
            cnt %= mod
    return cnt

print(f(0, -1))

Submission status: Runtime Error

Solution code:
MOD = 10**9 + 7
n = int(input())
tree = [[] for i in range(n)]
for i in range(n-1):
    tmp1, tmp2 = map(int, input().split())
    tree[tmp1 - 1].append(tmp2 - 1) 
    tree[tmp2 - 1].append(tmp1 - 1)
visited = [False]*n

def dfs(i):
    visited[i] = True
    if all([visited[j] for j in tree[i]]):
        return 1, 1  #黒の通り数, 白の通り数
    else:
        ans1 = 1
        ans2 = 1
        for j in tree[i]:
            if not visited[j]:
                tmp1, tmp2 = dfs(j)
                ans1 *= tmp2
                ans2 *= (tmp1 + tmp2)
                ans1 %= MOD
                ans2 %= MOD
        return ans1, ans2  #黒の通り数, 白の通り数

print(sum(dfs(0)) % MOD)

Submission status: Runtime Error

Solution code:
def bw(i, p):
    if dp[i] != [0, 0]:
        return dp[i]
    b = w = 1
    for ii in h[i]:
        if ii in p: continue
        bwi = bw(ii, p + [ii])
        w *= (bwi[0] + bwi[1])
        b *= bwi[1]
        w %= md
        b %= md
    dp[i] = [b, w]
    return [b, w]


md = 10 ** 9 + 7
n = int(input())
if n == 1:
    print(2)
    exit()
h = {}
for _ in range(n - 1):
    a, b = map(int, input().split())
    h.setdefault(a, [])
    h.setdefault(b, [])
    h[a] += [b]
    h[b] += [a]
dp = [[0, 0] for _ in range(n + 1)]
b, w = bw(1, [1])
print((b + w) % md)

Submission status: Runtime Error

Solution code:
def bw(i,p):
    if dp[i] != [0, 0]:
        return dp[i]
    b = w = 1
    for ii in h[i]:
        if ii in p:continue
        bwi = bw(ii,p+[ii])
        w *= (bwi[0] + bwi[1])
        b *= bwi[1]
        w %= md
        b %= md
    dp[i] = [b, w]
    return [b, w]


md = 10 ** 9 + 7
n = int(input())
if n==1:
    print(2)
    exit()
h = {}
for _ in range(n - 1):
    a, b = map(int, input().split())
    h.setdefault(a, [])
    h.setdefault(b, [])
    h[a] += [b]
    h[b] += [a]
dp = [[0, 0] for _ in range(n + 1)]
b,w=bw(1,[1])
print(b+w)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**7)
mod=10**9+7
n=int(input())
edges=[[] for _ in range(n)]
for _ in range(n-1):
    x,y=map(int,input().split())
    edges[x-1].append(y-1)
    edges[y-1].append(x-1)
C=[[] for _ in range(n)]

def fill_C(v,par):
    for c in edges[v]:
        if c==par:
            continue
        C[v].append(c)
        fill_C(c,v)
fill_C(root,-1)

DP=[[-1,-1] for _ in range(n)]

def white(v):
    if DP[v][0]!=-1:
        return DP[v][0]
    res=1
    for c in C[v]:
        res*=white(c)+black(c)
        res%=mod
    DP[v][0]=res
    return res
def black(v):
    if DP[v][1]!=-1:
        return DP[v][1]
    res=1
    for c in C[v]:
        res*=white(c)
        res%=mod
    DP[v][1]=res
    return res

print((black(0)+white(0))%mod)

Submission status: Runtime Error

Solution code:
n = int(input())
graph = [[] for _ in range(n)]

for _ in range(n-1):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    graph[x].append(y)
    graph[y].append(x)

mod = 10**9 + 7

memo = [0] * n
# dp[v][0]:= 頂点vを白に塗ったときの、vを親とする部分木の塗り方の総数
# dp[v][1]:= 頂点vを黒に塗ったときの、vを親とする部分木の塗り方の総数
dp = [[0, 0] for _ in range(n+1)]


def dfs(v):
    if memo[v] != 0:
        return
    memo[v] = 1
    dp[v][0] = 1
    dp[v][1] = 1
    for nv in graph[v]:
        if memo[nv] != 0:
            continue
        dfs(nv)
        dp[v][0] *= (dp[nv][0] + dp[nv][1])
        dp[v][0] %= mod

        dp[v][1] *= dp[nv][0]
        dp[v][1] %= mod

dfs(0)
print((dp[0][0] + dp[0][1]) % mod)

Submission status: Runtime Error

Solution code:
MOD = 10**9+7
N = int(input())

X = [[] for _ in range(N)]

for _ in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)
    
P = [-1] * N
def dfs(i):
    for j in X[i]:
        if j != 0 and P[j] < 0:
            P[j] = i
            dfs(j)

dfs(0)

for i in range(N):
    if i != 0:
        X[i].remove(P[i])

ans = 0
B = [0] * N
W = [0] * N

def dfs2(i):
    _ansb = 1
    _answ = 1
    for j in X[i]:
        __ansb, __answ = dfs2(j)
        _ansb *= __answ
        _answ *= (__ansb+__answ)
        _ansb %= MOD
        _answ %= MOD
    
    return (_ansb, _answ)

print(sum(dfs2(0)) % MOD)

Submission status: Runtime Error

Solution code:
N = int(input())

X = [[] for _ in range(N)]

for _ in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)
    
P = [-1] * N
def dfs(i):
    for j in X[i]:
        if j != 0 and P[j] < 0:
            P[j] = i
            dfs(j)

dfs(0)

for i in range(N):
    if i != 0:
        X[i].remove(P[i])

ans = 0
B = [0] * N
W = [0] * N

def dfs2(i):
    _ansb = 1
    _answ = 1
    for j in X[i]:
        __ansb, __answ = dfs2(j)
        _ansb *= __answ
        _answ *= (__ansb+__answ)
    
    return (_ansb, _answ)

print(sum(dfs2(0)))

Submission status: Runtime Error

Solution code:
#include <cstdio>
#include <vector>
using namespace std;

const int MOD = 1e9 + 7;
vector<int> G[100005];
long long cnt[100005][2];

void dfs(int i, int par) {
    long long white = 1ll, black = 1ll;
    for (auto it : G[i]) {
        if (it != par) {
            dfs(it, i);
            (white *= (cnt[it][0] + cnt[it][1])) %= MOD;
            (black *= cnt[it][0]) %= MOD;
        }
    }
    cnt[i][0] = white;
    cnt[i][1] = black;
}

int main() {
    int n;
    scanf("%d", &n);
    int x, y;
    for (int i = 1; i < n; ++i) {
        scanf("%d%d", &x, &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1, 0);
    printf("%I64d\n", (cnt[1][0] + cnt[1][1]) % MOD);
    return 0;
}

Submission status: Runtime Error

Solution code:
n = int(input())
m = 1000000007
d = [[-1 for i in range(100000)] for j in range(2)]
v = [[] for j in range(100000)]

def f(x, c, p):
    if d[c][x] != -1: return d[c][x]
    d[c][x] = 1
    for i in v[x]:
        if i != p:
            t = f(i, 0, x)
            if c == 0: t += f(i, 1, x)
            t %= m
            d[c][x] = (d[c][x]*t)%m
    return d[c][x]

for i in range(n-1):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    v[x].append(y)
    v[y].append(x)
ans = (f(0, 0, -1) + f(0, 1, -1))%m
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

MOD = 10**9 + 7

def rec(vNow, vPar):
    W, B = 1, 1
    for v2 in adjL[vNow]:
        if v2 == vPar: continue
        W2, B2 = rec(v2, vNow)
        W = (W * (W2+B2)) % MOD
        B = (B * W2) % MOD
    return (W, B)

N = int(input())
adjL = [[] for v in range(N)]
for _ in range(N-1):
    x, y = map(int, input().split())
    adjL[x-1].append(y-1)
    adjL[y-1].append(x-1)

print(sum(rec(0, -1)) % MOD)

Submission status: Runtime Error

Solution code:
N=int(input())
table=[[] for i in range(N)]
for i in range(N-1):
    w,v=map(int,input().split())
    table[w-1].append(v-1)
    table[v-1].append(w-1)
mod=10**9+7
f=[-1]*N
g=[-1]*N
def ser(par,son):
    if f[son]!=-1 and g[son]!=-1:
        return f[son],g[son]
    s=1
    t=1
    for grason in table[son]:
        if grason==par:
            continue
        x,y=ser(son,grason)
        s*=x
        s%=mod
        t*=(x+y)
        t%=mod
    f[son],g[son]=t,s
    return t,s
z=1
w=1
for i in table[0]:
    ser(0,i)
    z*=f[i]
    z%=mod
    w*=(f[i]+g[i])
    w%=mod
print((z+w)%mod)