qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import math

N = int(input())
a = [int(i) for i in input().split()]
b = math.round(sum(a)/N)

c = sum([(i - b)**2 for i in a])
print(c)

Fixed code:
import math

N = int(input())
a = [int(i) for i in input().split()]
b = round(sum(a)/N)

c = sum([(i - b)**2 for i in a])
print(c)
Buggy code:
N=int(input())
a=list(map(int,input().split()))
ans=10**9
bns=0
for i in range(-100,101):
  for j in range(N):
    bns+=(abs(a[j]-i)**2)
    ans=min(ans,bns)
print(ans)

Fixed code:
N=int(input())
a=list(map(int,input().split()))
ans=10**9
for i in range(-100,101):
  bns=0
  for j in range(N):
    bns+=(abs(a[j]-i)**2)
  ans=min(ans,bns)
print(ans)
Buggy code:
a = list(map(int,input().split()))

ans = []
for i in range(-100, 100):
    total = 0
    for ai in a:
        total += (ai - i)**2
    ans.append(total)

print(min(ans))


Fixed code:
N = int(input())
a = list(map(int,input().split()))
 
ans = []
for i in range(-100, 101):
    total = 0
    for ai in a:
        total += (ai - i)**2
    ans.append(total)
 
print(min(ans))
Buggy code:
N = int(input())
A = list(map(int, input().split()))

ave = sum(A) / N
if sum(A) % N >= N / 2:
  ave += 1
  
ans = 0
for i in range(N):
  ans += (A[i] - ave) * (A[i] - ave) 

print(int(ans))

Fixed code:
N = int(input())
A = list(map(int, input().split()))

ave = sum(A) // N 
if sum(A) % N >= N / 2:
  ave += 1

ans = 0
for i in range(N):
  ans += (A[i] - ave) * (A[i] - ave) 

print(int(ans))
Buggy code:
N=int(input())
*a,=map(int,input().split())
print(min([sum(map(lambda x:(x-n)**2,a)),n] for n in range(1,100))[0])


Fixed code:
N=int(input())
*a,=map(int,input().split())
print(min([sum(map(lambda x:(x-n)**2,a)),n] for n in range(-100,101))[0])
Buggy code:
N = int(input())
a = list(map(int,input().split()))

def minimize(list):
    min = 0
    for i in range(-100,101):
        num = 0
        for j in range(len(list)):
            num = num + (int(list[j]) - i)**2
        if min == 0:
            min = num
        elif min > num:
            min = num
    return min

print(minimize(a))

Fixed code:
N = int(input())
a = list(map(int,input().split()))

def minimize(list):
    min = 1000000
    for i in range(-100,101):
        num = 0
        for j in range(len(list)):
            num = num + (int(list[j]) - i)**2
        if min >= num:
            min = num
    return min

print(minimize(a))
Buggy code:
def main():
  n = int(input())
  nums = list(map(int, input()))
  cmin = 0
  for x in nums:
	cmin += (x - min(nums)) ** 2
  for i in range(min(nums), max(nums)+1):
	c = getcost(i, nums)
    if c < cmin:
      cmin = c
  print(cmin)
  
def getcost(x, nums):
  c = 0
  for n in nums:
    c += (n - x) ** 2
  return c
  
if __name__ == '__main__':
  main()

Fixed code:
def main():
  n = int(input())
  nums = list(map(int, input().split()))
  cmin = 0
  for x in nums:
    cmin += (x - min(nums)) ** 2
  for i in range(min(nums), max(nums)+1):
    c = getcost(i, nums)
    if c < cmin:
      cmin = c
  print(cmin)
  
def getcost(x, nums):
  c = 0
  for n in nums:
    c += (n - x) ** 2
  return c
  
if __name__ == '__main__':
  main()
Buggy code:
n=int(input())
c=list(map(int,input().split()))
minans=1000000000
for j in range(101):
  ans=0
  for i in range(n):
    ans+=(c[i]-j)**2
  minans=min(ans,minans)
print(minans)  
  

Fixed code:
n=int(input())
c=list(map(int,input().split()))
minans=1000000000
for j in range(-101,101):
  ans=0
  for i in range(n):
    ans+=(c[i]-j)**2
  minans=min(ans,minans)
print(minans)
Buggy code:
n=int(input())
a=list(map(int,input().split()))

diff,ans=100000,100000


for x in range(min(a),max(a)+1):
    crnt=0
    for aa in a:
        crnt+=(aa-x)**2
    if crnt<diff:
        ans=crnt

print(x)

Fixed code:
n=int(input())
a=list(map(int,input().split()))

ans=10**10
for x in range(min(a),max(a)+1):
    crnt=0
    for aa in a:
        crnt+=(aa-x)**2
    if crnt<ans:
        ans=crnt

print(ans)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
b=sum(a)/n
c=(b*2+1)//2
s=0
for i in a:
    s+=(i-s)**2
print(s)




Fixed code:
n=int(input())
a=list(map(int,input().split()))
b=sum(a)/n
c=(b*2+1)//2
s=0
for i in a:
    s+=(i-c)**2
s=int(s)
print(s)
Buggy code:
import copy
n = int(input())
a = list(map(int, input().split()))
a_avg = int(sum(a)/len(a))
b = copy.copy(a)
while(len(set(b)) > 1):
    for i in range(len(a)):
        if b[i] > a_avg:
            b[i] -= 1
        if b[i] < a_avg:
            b[i] += 1
s = 0
for i in range(len(a)):
    s += (a[i]-b[i])**2
print(s)

Fixed code:

import copy
n = int(input())
a = list(map(int, input().split()))
a_avg = round(sum(a)/len(a))
b = copy.copy(a)
while(len(set(b)) > 1):
    for i in range(len(a)):
        if b[i] > a_avg:
            b[i] -= 1
        if b[i] < a_avg:
            b[i] += 1
s = 0
for i in range(len(a)):
    s += (a[i]-b[i])**2
print(s)
Buggy code:
import math
from statistics import mean

n = int(input())
a = list(map(int, input().split()))

m = mean(a)
if m - math.floor(m) < 0.5:
    m = math.floor(m)
else:
    m = math.floor(m) + 1

diff = 0
for v in a:
    diff = (m - v) ** 2

print(diff)


Fixed code:
import math
from statistics import mean

n = int(input())
a = list(map(int, input().split()))

m = mean(a)
if m - math.floor(m) < 0.5:
    m = math.floor(m)
else:
    m = math.floor(m) + 1

diff = 0
for v in a:
    diff += (m - v) ** 2

print(diff)
Buggy code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
a = list(map(int,input().split()))
h = Decimal(str(sum(a)/n).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
a = list(map(lambda x:(x-h)**2,a))
print(sum(a))

Fixed code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
a = list(map(int,input().split()))
h = Decimal(str(sum(a)/n)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
a = list(map(lambda x:(x-h)**2,a))
print(sum(a))
Buggy code:
input()
a = list(map(int, input().split()))
for i in range(min(a),max(a)+1):
    cost_min = 0 if i == min(a) else cost_min
    cost = 0
    for e in a:
        add_cost = abs((e - i)) ** 2
        cost_min += add_cost if i == min(a) else 0
        cost += add_cost if i != min(a) else 0
    print(cost)
    if not i == min(a) and cost_min > cost:
        cost_min = cost
print(cost_min)

Fixed code:
input()
a = list(map(int, input().split()))
for i in range(min(a),max(a)+1):
    cost_min = 0 if i == min(a) else cost_min
    cost = 0
    for e in a:
        add_cost = abs((e - i)) ** 2
        cost_min += add_cost if i == min(a) else 0
        cost += add_cost if i != min(a) else 0
    if not i == min(a) and cost_min > cost:
        cost_min = cost
print(cost_min)
Buggy code:
if __name__ == '__main__':
    n = int(input())
    lst = list(map(int, input().split()))

    ans = 50*101
    for i in range(-100,101):
        print(i)
        mn = 0
        for c in lst:
            mn += (i-c)*(i-c)
        ans = min(ans,mn)

    print(ans)


Fixed code:
if __name__ == '__main__':
    n = int(input())
    lst = list(map(int, input().split()))

    ans = (50*101)*(50*101)
    for i in range(-100,101):
        mn = 0
        for c in lst:
            mn += (i-c)*(i-c)
        ans = min(ans,mn)

    print(ans)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
l=[]
for x in range(-100,101):
    c=0
    for i in range(n):
        c+=(a[i]-x)**2
    l.append(c)

print(min(c))


Fixed code:
n=int(input())
a=list(map(int,input().split()))
l=[]
for x in range(-100,101):
    c=0
    for i in range(n):
        c+=(a[i]-x)**2
    l.append(c)

print(min(l))
Buggy code:
from collections import defaultdict
n, m = map(int, input().split())
r = set()
r.add(1)
d = defaultdict(int)
for i in range(m):
  x, y = map(int, input().split())
  d[x] -= 1
  d[y] += 1
  if x in r:
    r.add(y)
ans = len(list(r))
for e in d:
  if d[e] == -1 and e in r:
    ans -= 1
print(ans)

Fixed code:
from collections import defaultdict
n, m = map(int, input().split())
r = set()
r.add(1)
d = defaultdict(int)
for i in range(m):
  x, y = map(int, input().split())
  d[x] -= 1
  d[y] += 1
  if x in r:
    r.add(y)
  if d[x] == -1 and x in r:
    r.remove(x)
ans = len(list(r))
for e in d:
  if d[e] == -1 and e in r:
    ans -= 1
print(ans)
Buggy code:
n,m = map(int,input().split())
possible = [0]*n
kosuu = [1]*n
possible[0] = 1
L = []

for i in range(m):
    x,y = map(int,input().split())
    L.append((x,y))

for i in range(idx,m):
    x,y = L[i][0],L[i][1]
    x,y = x-1,y-1
    if kosuu[x] >=2 and possible[x] == 1:
        possible[y] = 1
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 1:
        possible[x] = 0
        possible[y] = 1
        kosuu[x] = 0
        kosuu[y] +=1
    elif kosuu[x] >=2 and possible[x] == 0:
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 0:
        kosuu[x] = 0
        kosuu[y] +=1
        
print(sum(possible))

Fixed code:
n,m = map(int,input().split())
possible = [0]*n
kosuu = [1]*n
possible[0] = 1
L = []

for i in range(m):
    x,y = map(int,input().split())
    L.append((x,y))

for i in range(m):
    x,y = L[i][0],L[i][1]
    x,y = x-1,y-1
    if kosuu[x] >=2 and possible[x] == 1:
        possible[y] = 1
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 1:
        possible[x] = 0
        possible[y] = 1
        kosuu[x] = 0
        kosuu[y] +=1
    elif kosuu[x] >=2 and possible[x] == 0:
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 0:
        kosuu[x] = 0
        kosuu[y] +=1
print(sum(possible))
Buggy code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
x = []
for i in range(m):
    x0, x1 = MAP()
    x.append([x0-1, x1-1])

r = [0]*n
w = [1]*n
r[0] = 1
w[0] = 0

for y in x:
    if r[y[0]] > 0:
        r[y[1]] = 1
    if w[y[0]] > 0:
        w[y[0]] += 1
    if r[y[0]] > 0 and w[y[0]] == 0:
        r[y[0]] = 0
    if r[y[0]] == 0 and w[y[0]] > 0:
        w[y[0]] -= 1

ans = sum(r)
print(ans)

Fixed code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
x = []
for i in range(m):
    x0, x1 = MAP()
    x.append([x0-1, x1-1])

r = [0]*n
w = [1]*n
t = [1]*n
r[0] = 1
w[0] = 0

for y in x:
    t[y[0]] -= 1
    t[y[1]] += 1

    if r[y[0]] > 0 and w[y[0]] == 0:
        r[y[0]] = 0
        r[y[1]] = 1
    elif r[y[0]] == 0 and w[y[0]] > 0:
        w[y[0]] -= 1
        w[y[1]] += 1
    elif r[y[0]] > 0 and w[y[0]] > 0:
        r[y[1]] = 1
        w[y[1]] += 1
    if t[y[0]] == 0:
        r[y[0]] = w[y[0]] = 0

ans = sum(r)
print(ans)
Buggy code:
n,m=map(int,input().split())
a=[1]*(n+1)
b=[0]*(n+1)
b[1]=1
for _ in range(m):
  x,y=map(int,input().split())
  a[x]-=1
  a[y]+=1
  if b[x]:b[y]=1
ans=0
for i,j in zip(a,b):
  ans+=(i!=0)*j
print(ans)

Fixed code:
n,m=map(int,input().split())
a=[1]*(n+1)
b=[0]*(n+1)
b[1]=1
for _ in range(m):
  x,y=map(int,input().split())
  a[x]-=1
  a[y]+=1
  if b[x]:b[y]=1
  if a[x]==0:b[x]=0
ans=0
for i,j in zip(a,b):
  ans+=(i!=0)*j
print(ans)
Buggy code:
import math
import collections
import fractions
import itertools
import functools
import  operator

def solve():
    n, m = map(int, input().split())
    ball, can = [0]*n, [True]*n
    ball[0], can[0] = 1, True
    for _ in range(m):
        x, y = map(int, input().split())
        if can[x-1] == True:
            can[y-1] = True
        ball[x-1] -= 1
        ball[y-1] += 1
        if ball[x-1] == 0:
            can[x-1] = False
    print(can.count(True))
    return 0

if __name__ == "__main__":
    solve()

Fixed code:
import math
import collections
import fractions
import itertools
import functools
import  operator

def solve():
    n, m = map(int, input().split())
    ball, can = [1]*n, [False]*n
    can[0] = True
    for _ in range(m):
        x, y = map(int, input().split())
        if can[x-1] == True:
            can[y-1] = True
        ball[x-1] -= 1
        ball[y-1] += 1
        if ball[x-1] == 0:
            can[x-1] = False
    print(can.count(True))
    return 0

if __name__ == "__main__":
    solve()
Buggy code:
n, m = map(int,input().split())
b_list = []

for i in range(m):
    x = []
    x = list(map(int,input().split()))
    b_list.append(x)

ans_list = [0 for i in range(10**5)]
ans_list[0] = 1
ball_list = [1 for i in range(10**5)]
print(ball_list[0])

for tmp in b_list:
    x, y = tmp[0], tmp[1]
    print(x, y)
    if ans_list[x-1] == 1 and ans_list[y-1] == 0:
        ans_list[y-1] += 1
    ball_list[y-1] += 1
    ball_list[x-1] -= 1
    if ball_list[x-1] == 0:
        ans_list[x-1] = 0

ans = 0

for i in ans_list:
    if i == 1:
        ans += 1

print(ans)


Fixed code:
n, m = map(int,input().split())
b_list = []

for i in range(m):
    x = []
    x = list(map(int,input().split()))
    b_list.append(x)

ans_list = [0 for i in range(10**5)]
ans_list[0] = 1
ball_list = [1 for i in range(10**5)]

for tmp in b_list:
    x, y = tmp[0], tmp[1]
    if ans_list[x-1] == 1 and ans_list[y-1] == 0:
        ans_list[y-1] += 1
    ball_list[y-1] += 1
    ball_list[x-1] -= 1
    if ball_list[x-1] == 0:
        ans_list[x-1] = 0

ans = 0

for i in ans_list:
    if i == 1:
        ans += 1

print(ans)
Buggy code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun  7 14:28:00 2020

@author: NEC-PCuser
"""

N, M = map(int, input().split())
x, y = [], []

for i in range(M):
    x_tmp, y_tmp = map(int, input().split())
    x.append(x_tmp)
    y.append(y_tmp)
    
ball_set = set()
ball_set.add(1)
start_flg = True
for i in range(len(x)):
    if x[i] in ball_set:
        if start_flg == True:
            ball_set.discard(1)
        ball_set.add(y[i])
        

print(len(ball_set))



Fixed code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun  7 14:28:00 2020

@author: NEC-PCuser
"""

N, M = map(int, input().split())
x, y = [], []

for i in range(M):
    x_tmp, y_tmp = map(int, input().split())
    x.append(x_tmp)
    y.append(y_tmp)

box = [1] * N 
ball_set = set()
ball_set.add(1)
start_flg = True
for i in range(len(x)):
    if x[i] in ball_set:
        ball_set.add(y[i])
    box[x[i] - 1] -= 1
    box[y[i] - 1] += 1
    if box[x[i] - 1] == 0:
        ball_set.discard(x[i])

print(len(ball_set))
Buggy code:
import numpy as np   
n,m = [int(i)for i in input().split()]

l = [[int(i)-1 for i in input().split()] for _ in range(m)]

line = np.array([1 for _ in range(n)])
is_red = np.zeros(n,dtype = np.int32)
is_red[0] = 1

for x,y in l:
    line[x] -= 1 if line[x] > 0 else 0
    line[y] += 1
    is_red[y] = 1 if is_red[x] == 1 else is_red[y]
    

ans = np.count_nonzero(is_red[line > 0])

print(ans)
    



Fixed code:
import numpy as np   
n,m = [int(i)for i in input().split()]

l = [[int(i)-1 for i in input().split()] for _ in range(m)]

line = np.array([1 for _ in range(n)])
is_red = np.zeros(n,dtype = np.int32)
is_red[0] = 1

for x,y in l:
    line[x] -= 1 if line[x] > 0 else 0
    line[y] += 1
    is_red[y] = 1 if is_red[x] == 1 else is_red[y]
    is_red[x] = is_red[x] if line[x] > 0 else 0
    

ans = np.count_nonzero(is_red[line > 0])

print(ans)
Buggy code:
def solve():
    N, M = map(int, input().split())
    
    num = []
    red = []
    for i in range(N+1):
        num.append(1)
        red.append(0)
    red[1] = 1
    
    for _ range(M):
        x, y = map(int, input().split())
        if red[x] == 1:
            red[y] = 1
        num[x] -= 1
        num[y] += 1
        if num[x] == 0:
            red[x] = 0
    
    return sum(red)

print(solve())

Fixed code:
def solve():
    N, M = map(int, input().split())
    
    num = []
    red = []
    for i in range(N+1):
        num.append(1)
        red.append(0)
    red[1] = 1
    
    for _ in range(M):
        x, y = map(int, input().split())
        if red[x] == 1:
            red[y] = 1
        num[x] -= 1
        num[y] += 1
        if num[x] == 0:
            red[x] = 0
    
    return sum(red)

print(solve())
Buggy code:
n, m = map(int, input().split())
boxes = [[0, 1] for _ in range(n)]
boxes[0][0] = 1
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    boxes[x][1] -= 1
    boxes[y][1] += 1
    if boxes[x][0] == 1:
        boxes[y][0] = 1
    else:
        if boxes[y][0] == 1:
            continue
        else:
            boxes[y][0] = 0  
    if boxes[x][1] == 0:
        boxes[x][0] = -1
ans = 0
for a, b in boxes:
    # print(a, b)
    if a:
        ans += 1
# print(boxes)
print(ans)


Fixed code:
n, m = map(int, input().split())
boxes = [[0, 1] for _ in range(n)]
boxes[0][0] = 1
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    boxes[x][1] -= 1
    boxes[y][1] += 1
    if boxes[x][0] == 1:
        boxes[y][0] = 1
    else:
        if boxes[y][0] == 1:
            continue
        else:
            boxes[y][0] = 0  
    if boxes[x][1] == 0:
        boxes[x][0] = -1
ans = 0
for a, b in boxes:
    # print(a, b)
    if a > 0:
        ans += 1
# print(boxes)
print(ans)
Buggy code:
n, m = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(m)]

ans = [1] * (n)
ans[0] = 1
ball = [0] * (n)
ball[0] = 1
for i in range(m):
    x, y = xy[i]
    ans[y-1] += 1
    ans[x-1] -= 1
    if ball[x-1]:
      ball[y-1] = 1
    if ans[x-1] == 0:
      ball[x-1] = 0
      
print(sum(cnt))

Fixed code:
n, m = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(m)]

ans = [1] * (n)
ans[0] = 1
ball = [False] * (n)
ball[0] = True
for i in range(m):
    x, y = xy[i]
    ans[y-1] += 1
    ans[x-1] -= 1
    if ball[x-1]:
      ball[y-1] = True
    if ans[x-1] == 0:
      ball[x-1] = False
      
cnt = 0
for i in ball:
  if i:
    cnt += 1

print(cnt)
Buggy code:
n,m  = map(int,input().split())

balls = [1 for _ in range(n)]
red = [1] + [0 for _ in range(1,n)]

for _ in range(m):
  x, y = map(int,input().split())
  x,y = x-1, y-1
  
  if balls[x] ==0:
    continue
    
  balls[x] = x-1
  balls[y] += 1
  if red[x]:
    red[y] = 1
  if balls[x]==0:
    red[x] = 0
print(sum(red))
  
  

Fixed code:
n,m  = map(int,input().split())

balls = [1 for _ in range(n)]
red = [1] + [0 for _ in range(1,n)]

for _ in range(m):
  x, y = map(int,input().split())
  x,y = x-1, y-1
  
  if balls[x] ==0:
    continue
    
  balls[x] -= 1
  balls[y] += 1
  if red[x]:
    red[y] = 1
  if balls[x]==0:
    red[x] = 0
print(sum(red))
Buggy code:
N,M= map(int,input().split())

x = []
y = []
for i in range(M):
  xi, yi = input().split()
  x.append(int(xi))
  y.append(int(yi))

L=[]
for i in range(N):
  L.append(1)
R=[1]
for i in range(N-1):
  R.append(0)
#print(R) 

for i in range(M):
  if R[i]==1 and L[i]==1:
    R[i]=0
    L[i]=0
    R[y[i]-1]=1
    L[y[i]-1]=L[y[i]-1]+1
  elif R[i]==1 and L[i]>1:
    L[i]=L[i]-1
    R[y[i]-1]=1
    L[y[i]-1]=L[y[i]-1]+1
  elif R[i]==0 and L[i]>0:
    L[y[i]-1]=L[y[i]-1]+1
   
    
#print(R,L)
print(R.count(1))


Fixed code:
N,M= map(int,input().split())

x = []
y = []
for i in range(M):
  xi, yi = input().split()
  x.append(int(xi))
  y.append(int(yi))

L=[]
for i in range(N):
  L.append(1)
R=[1]
for i in range(N-1):
  R.append(0)
#print(R) 

#print('     ','R',R,'L',L) 
for i in range(M):
  if R[x[i]-1]==1 and L[x[i]-1]==1:
    R[x[i]-1]=0
    L[x[i]-1]=0
    R[y[i]-1]=1
    L[y[i]-1]=L[y[i]-1]+1
  elif R[x[i]-1]==1 and L[x[i]-1]>=1:
    L[x[i]-1]=L[x[i]-1]-1
    R[y[i]-1]=1
    L[y[i]-1]=L[y[i]-1]+1
  elif R[x[i]-1]==0 and L[x[i]-1]>=1:
    L[x[i]-1]=L[x[i]-1]-1
    L[y[i]-1]=L[y[i]-1]+1
  #print(i+1,x[i],y[i],'R',R,'L',L) 
    

print(R.count(1))
Buggy code:
n, m = list(map(int, input().split()))

moves = [list(map(int, input().split())) for i in range(m)]

possibilities = [False] * n
possibilities[0] = True
nums = [1] * n

for i in range(m):
    x = moves[i][0] - 1
    y = moves[i][1] - 1

    if possibilities[x]:
        possibilities[y] = True

    nums[x] -= 1
    nums[y] += 1
    # if nums[x] == 0:
    #     possibilities[x] = False

ans = 0
for j in range(n):
    if possibilities[j] and (nums[j] > 0):
        ans += 1
print(ans)



Fixed code:
n, m = list(map(int, input().split()))

moves = [list(map(int, input().split())) for i in range(m)]

possibilities = [False] * n
possibilities[0] = True
nums = [1] * n

for i in range(m):
    x = moves[i][0] - 1
    y = moves[i][1] - 1

    if possibilities[x]:
        possibilities[y] = True

    nums[x] -= 1
    nums[y] += 1
    if nums[x] == 0:
        possibilities[x] = False

ans = 0
for j in range(n):
    if possibilities[j] and (nums[j] > 0):
        ans += 1
print(ans)
Buggy code:
n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(m)]
count=[1]*(n+1)
flag=[False]*(n+1)
flag[1]=True
for a,b in arr:
  count[b]+=1
  count[a]-=1
  if flag[a]==True:
    flag[b]==True
  if count[a]==0:
    flag[a]==False
print(flag.count(True))

Fixed code:
n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(m)]
count=[1]*(n+1)
flag=[False]*(n+1)
flag[1]=True
for a,b in arr:
  count[b]+=1
  count[a]-=1
  if flag[a]==True:
    flag[b]=True
  if count[a]==0:
    flag[a]=False
print(flag.count(True))
Buggy code:
n,m = map(int,input().split())

red = [0 for i in range(n)]
amount = [1 for i in range(n)]
red[0] = 1
for _ in range(m):
    x,y = map(int,input().split())
    amount[x-1] -= 1
    amount[y-1] += 1
    if red[x-1]:
      red[y-1] = 1

ans = 0
for r,a in zip(red, amount):
  if r > 0 and a > 0:
    ans += 1

print(ans)

Fixed code:
n,m = map(int,input().split())

red = [0 for i in range(n)]
amount = [1 for i in range(n)]
red[0] = 1
for _ in range(m):
    x,y = map(int,input().split())
    amount[x-1] -= 1
    amount[y-1] += 1
    if red[x-1]:
      red[y-1] = 1
    if amount[x-1] == 0:
      red[x-1] = 0
ans = 0
for r,a in zip(red, amount):
  if r > 0 and a > 0:
    ans += 1



print(ans)
Buggy code:
import sys
input = sys.stdin.buffer.readline

n, m = map(int, input().split())
b = [False] * (n + 1)
c = [1] * (n + 1)
for i in range(m):
    x, y = map(int, input().split())
    c[x] -= 1
    c[y] += 1
    if b[x]:
        b[y] = True
    if c[x] == 0:
        b[x] = False
print(sum(b))

Fixed code:
import sys
input = sys.stdin.buffer.readline

n, m = map(int, input().split())
b = [False, True] + [False] * (n - 1)
c = [1] * (n + 1)
for i in range(m):
    x, y = map(int, input().split())
    c[x] -= 1
    c[y] += 1
    if b[x]:
        b[y] = True
    if c[x] == 0:
        b[x] = False
print(sum(b))
Buggy code:
n, m = map(int, input().split())
xy = [list(map(int,input().split())) for _ in range(m)]

cnt = [1] * n
r = [1] + [0] * (n-1)
for x, y in xy:
    cnt[x-1] -= 1
    cnt[y-1] += 1
    
    if cnt[x-1] == 0:
        r[x-1] = 0
        
    if r[x-1] == 1:
        r[y-1] = 1
        
print(sum(r))

Fixed code:
n, m = map(int, input().split())
xy = [list(map(int,input().split())) for _ in range(m)]

cnt = [1] * n
r = [1] + [0] * (n-1)
for x, y in xy:
    cnt[x-1] -= 1
    cnt[y-1] += 1
       
    if r[x-1] == 1:
        r[y-1] = 1
        
    if cnt[x-1] == 0:
        r[x-1] = 0
    
print(sum(r))