Problem p03175 (python)

Fixed submission:
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
flag[0]=1
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
    length=len(stack)-1
    for i in range(len(edge[stack[length]])):
      if flag[edge[stack[length]][i]]==0:
        flag[edge[stack[length]][i]]=1
        stack.append(edge[stack[length]][i])
        term=1
      else:
        sub1=max(1,(dp[edge[stack[length]][i]][0]+dp[edge[stack[length]][i]][1])%MOD)
        sub2=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()
  
  ans = (dp[0][0]+dp[0][1])%MOD

print(ans)

Buggy submission (verdict: Runtime Error):


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"));

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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)