Problem:
   Problem Statement We have N boxes, numbered 1 through N . At first, box 1 contains one red ball, and each of the other boxes contains one white ball.  Snuke will perform the following M operations, one by one. In the i -th operation, he randomly picks one ball from box x_i , then he puts it into box y_i .  Find the number of boxes that may contain the red ball after all operations are performed.      Constraints  2≤N≤10^5  1≤M≤10^5  1≤x_i,y_i≤N  x_i≠y_i  Just before the i -th operation is performed, box x_i contains at least 1 ball.         Input The input is given from Standard Input in the following format:  N  M  x_1  y_1  :  x_M  y_M       Output Print the number of boxes that may contain the red ball after all operations are performed.        Sample Input 1 3 2
1 2
2 3      Sample Output 1 2  Just after the first operation, box 1 is empty, box 2 contains one red ball and one white ball, and box 3 contains one white ball.  Now, consider the second operation. If Snuke picks the red ball from box 2 , the red ball will go into box 3 . If he picks the white ball instead, the red ball will stay in box 2 .
Thus, the number of boxes that may contain the red ball after all operations, is 2 .       Sample Input 2 3 3
1 2
2 3
2 3      Sample Output 2 1  All balls will go into box 3 .       Sample Input 3 4 4
1 2
2 3
4 1
3 4      Sample Output 3 3

Solution (Python):
n,m=map(int,input().split())
XY=[list(map(int,input().split())) for _ in range(m)]

cnt=[1 for _ in range(n)]
red=[True]+[False for _ in range(n-1)]
for x,y in XY:
  if cnt[x-1]>=1:
    cnt[x-1]-=1
    cnt[y-1]+=1
    if red[x-1]:
      red[y-1]=True
      if cnt[x-1]==0:
        red[x-1]=False
  else:
    red[x-1]=False
print(sum(red))

Solution (Python):
_,*B=open(0);W=[1]*7**6;R=[0,1]+[0]*7**6
for b in B:x,y=map(int,b.split());W[x]-=1;W[y]+=1;R[y]|=R[x];R[x]&=W[x]>0
print(sum(R))

Solution (Python):

"""
Spyder Editor

This is a temporary script file.
"""

N, M = map(int, input().split())
box = 1
b = []
a = []
for i in range(N):
    a.append(1)
    b.append(0)
a.append(1)
b.append(0)

b[1] = 1

for i in range(M):
    x, y = map(int, input().split())
    if b[x] == 1:
        if a[x] == 1:
            b[x] = 0
        b[y] = 1
    a[x] -= 1
    a[y] += 1
cnt = 0
for i in range(N + 1):
    if b[i] == 1:
        cnt += 1
print(cnt)

Solution (Python):

N, M = map(int, input().split())
x = [0] * M
y = [0] * M
for i in range(M):
    x[i], y[i] = map(int, input().split())

check = [[1, 0] for _ in range(N+1)]
check[1] = [1, 1]

for i in range(M):
    if check[x[i]][0] >= 1:
        check[x[i]][0] -= 1
        check[y[i]][0] += 1
    if check[x[i]][1] == 1:
        check[y[i]][1] = 1
    if check[x[i]][0] == 0:
        check[x[i]][1] = 0

ans = 0
for i in range(N+1):
    if check[i][1] == 1:
        ans += 1
print(ans)

Solution (Python):
N,M=map(int,input().split())
A=[1]*(N+1)
B=[0]*(N+1)
B[1]=1

for i in range(M):
  x,y=map(int,input().split())
  A[x]-=1
  A[y]+=1
  if B[x]:
    B[y]=1
    if not A[x]:
      B[x]=0
  
  
  
print(sum(B))

Solution (Python):

def main():
    N, M, *XY = map(int, open(0).read().split())
    R = [0] * (N + 1)  
    R[1] = 1
    C = [1] * (N + 1)  
    for x, y in zip(*[iter(XY)] * 2):
        R[y] = 1 if R[x] or R[y] else 0  
        C[x] -= 1
        C[y] += 1
        R[x] = 0 if C[x] == 0 else R[x]  
    ans = sum(R[1:])
    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
import sys
import heapq
import bisect

mod = 10**9+7
dd = ((-1,0),(1,0),(0,-1),(0,1))

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(sys.stdin.readline()[:-1])
def IR(n): return([I() for _ in range(n)])

def GCD(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def LCM(a,b):
    return a * b // GCD(a,b)

def main():
    N,M = LI()
    XY = []
    for _ in range(M):
        XY.append(LI())

    possibility = [[False,1] for _ in range(N)]
    possibility[0] = [True,1]

    for x,y in XY:
        if possibility[x-1][0]:
            possibility[y-1][0] = True
        possibility[x-1][1] -= 1
        possibility[y-1][1] += 1

        if possibility[x-1][1] == 0:
            possibility[x-1][0] = False

    ans = 0
    for i in range(N):
        if possibility[i][0]:
            ans += 1
    return(ans)

if __name__ == "__main__":
    print(main())

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, M = MAP()

has_red = [False] * N
has_red[0] = True
box = [1] * N
for _ in range(M):
    x, y = MAP()
    x -= 1
    y -= 1
    box[x] -= 1
    box[y] += 1
    if has_red[x]:
        has_red[y] = True
    if box[x] == 0:
        has_red[x] = False

ans = 0
for i in range(N):
    if box[i] != 0 and has_red[i]:
        ans += 1
print(ans)

Solution (Python):
from collections import defaultdict


def resolve():
    n, m = map(int, input().split())
    xys = list(list(map(int, input().split())) for _ in range(m))
    num = [1] * n
    red = [False] * n
    red[0] = True
    for x, y in xys:
        if red[x - 1]:
            red[y - 1] = True
        num[x - 1] -= 1
        num[y - 1] += 1
        if num[x - 1] == 0:
            red[x - 1] = False
    print(red.count(True))


if __name__ == "__main__":
    resolve()

Solution (Python):
N, M = map(int, input().split())
ns = [1]*N
fs = [False]*N
fs[0] = True
for i in range(M):
  x, y = map(int, input().split())
  x, y = x-1, y-1
  if ns[x] > 1:
    ns[x] -= 1
    ns[y] += 1
    if fs[x]:
      fs[y] = True
  elif ns[x] == 1:
    ns[x] -= 1
    ns[y] += 1
    if fs[x]:
      fs[x] = False
      fs[y] = True
r = fs.count(True)
print(r)

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
cnt = [1]*n
check = [True if i == 0 else False for i in range(n)]
for _ in range(m):
    x,y = LI()
    if check[x-1]:
        check[y-1] = True
        cnt[x-1] -= 1
        cnt[y-1] += 1
        if cnt[x-1] == 0:
            check[x-1] = False
    else:
        if cnt[x-1] > 0:
            cnt[x-1] -= 1
            cnt[y-1] += 1
ans = check.count(True)
print(ans)

Solution (Python):
n, m = map(int, input().split())
red = [False] * n
num = [1] * n
red[0] = True
first = 0
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    if red[x]:
        red[y] = True
        num[x] -= 1
        num[y] += 1
        if num[x] == 0:
            red[x] = False
    if not red[x]:
        if num[x] > 0:
            num[y] += 1
            num[x] -= 1
print(red.count(True))

Solution (Python):
def main():
    N, M = (int(i) for i in input().split())
    xys = [[int(i)-1 for i in input().split()] for j in range(M)]
    boxes = [[False,1] for _ in range(N)]
    boxes[0][0] = True 
    for x,y in xys:
        if boxes[x][0] == True:
            if boxes[x][1] == 1:
                boxes[x][0] = False
            boxes[y][0] = True
        boxes[x][1] -= 1
        boxes[y][1] += 1
    ans = 0
    for tf,c in boxes:
        if tf:
            ans += 1
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
N, M = (int(i) for i in input().split())
xys = [[int(i)-1 for i in input().split()] for j in range(M)]
boxes = [[False,1] for _ in range(N)]
boxes[0][0] = True 
for x,y in xys:
    if boxes[x][0] == True:
        if boxes[x][1] == 1:
            boxes[x] = [False,0]
        else:
            boxes[x][1] -= 1
        boxes[y] = [True,boxes[y][1] + 1]
    elif boxes[x][0] == False:
        boxes[x][1] -= 1
        boxes[y][1] += 1
ans = 0
for tf,c in boxes:
    if tf:
        ans += 1
print(ans)

Solution (Python):
n,m=map(int,input().split())
bnum=[1]*n
red=[True]+[False]*(n-1)
for i in range(m):
    x,y=map(int,input().split())
    x-=1;y-=1
    if bnum[x]==1 and red[x]:
        red[x]=False
        bnum[x]-=1
        bnum[y]+=1
        red[y]=True
    elif red[x]:
        bnum[x]-=1
        red[y]=True
        bnum[y]+=1
    else:
        bnum[x]-=1;bnum[y]+=1
print(sum(1 for i in red if i))

Solution (Python):
def main():

    N, M = map(int, input().split())

    cand = {1}
    count = dict()
    for i in range(1, N+1):
        count[i] = 1
    for _ in range(M):
        a, b = map(int, input().split())
        if a in cand:
            cand.add(b)
        if count[a] == 1 and a in cand:
            cand.remove(a)
        count[a] -= 1
        count[b] += 1
    return len(cand)



if __name__ == '__main__':
    print(main())

Solution (Python):
n, m = [ int(v) for v in input().split() ]
ball_list = [1 for i in range(n)]
ball_list_2 = [1 for i in range(n)]
ball_list[0] = 2

for i in range(m):
    x, y = [ int(v)-1 for v in input().split() ]
    
    if ball_list[x] == 1:
        if ball_list[y] in [0,1]:
            ball_list[y] = 1
        else:
            ball_list[y] = 3
    elif ball_list[x] == 2:
        if ball_list[y] in [0,2]:
            ball_list[y] = 2
        else:
            ball_list[y] = 3
    elif ball_list[x] == 3:
        ball_list[y] = 3
    
    ball_list_2[y] += 1
    ball_list_2[x] -= 1
    if ball_list_2[x] == 0:
        ball_list[x] = 0
    
print(sum([ 1 for i in ball_list if i > 1]))

Solution (Python):
N, M = map(int, input().split())
X, Y = [], []
for i in range(M):
    tmp = list(map(int, input().split()))
    X.append(tmp[0])
    Y.append(tmp[1])
box_ball = [1] * N
red = [False] * N
red[0] = True
for x, y in zip(X, Y):
    if red[x-1]:
        red[y-1] = True
    box_ball[x-1] -= 1
    box_ball[y-1] += 1
    if box_ball[x-1] == 0:
        red[x-1] = False
print(red.count(True))

Solution (Python):
N, M = map(int, input().split())
ans = set([1])
balls = [1]*N

for _ in range(M):
    x, y = map(int, input().split())
    if x in ans:
        ans.add(y)
    balls[x-1] -= 1
    if balls[x-1] == 0 and x in ans:
        ans.remove(x)
    balls[y-1] += 1

print(len(ans))

Solution (Python):
N,M=map(int,input().split())
A=[1 for _ in range(N)]

B=[0 for _ in range(N)]

ans=0
B[0]=1
for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  
  if B[x]==1 and A[x]>=2:
    A[x]-=1
    A[y]+=1
    B[y]=1
  
  
  elif B[x]==1 and A[x]==1:
    A[x]-=1
    A[y]+=1
    B[x]=0
    B[y]=1
  
  
  else:
    A[x]-=1
    A[y]+=1
  
print(B.count(1))

Solution (Python):
n,m=map(int,input().split())
ans=set()
ans.add(0)
l=[1 for i in range(n)]
for i in range(m):
    x,y=[int(j)-1 for j in input().split()]
    l[x]-=1
    l[y]+=1
    if x in ans:
        ans.add(y)
        if l[x]==0:
            ans.remove(x)
print(len(ans))

Solution (Python):


n,m=map(int,input().split())
box=[[1,1,0]]+[[1,0,1] for _ in range(n-1)]

for i in range(m):
    x,y=map(int,input().split())
    x-=1;y-=1
    box[y][1]|=box[x][1]
    box[y][2]|=box[x][2]
    box[y][0]+=1
    box[x][0]-=1
    if box[x][0]==0:
        box[x]=[0,0,0]        

cnt=0
for c,r,w in box:
    if  not c:continue
    if r:
        cnt+=1
print(cnt)

Solution (Python):
n, m = map(int, input().split())
x = [None] * m
y = [None] * m
for i in range(m):
    x[i], y[i] = map(lambda s: int(s) - 1, input().split())

red = [False] * n
balls = [1] * n
red[0] = True
for i in range(m):
    if red[x[i]]:
        red[y[i]] = True
    balls[x[i]] -= 1
    balls[y[i]] += 1
    if balls[x[i]] == 0:
        red[x[i]] = False

ans = sum(1 for v in red if v)
print(ans)

Solution (Python):
n,m = map(int,input().split())
list = [list(map(int,input().split())) for i in range(m)]

num = [1]*n
red = [0]*n
red[0] = 1

for i in range(m):
    x = list[i][0]-1
    y = list[i][1]-1
    if red[x] == 1:
        if num[x] == 1:
            red[x] = 0
        red[y] = 1
    num[x] -= 1
    num[y] += 1

print(sum(red))

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():

    return


def B():
    n,m = LI()
    f = [1]*n
    l = [0]*n
    l[0] = 1
    for i in range(m):
        x,y = LI()
        x -= 1
        y -= 1
        if l[x]:
            l[y] = 1
        f[x] -= 1
        f[y] += 1
        if f[x] == 0:
            l[x] = 0
    ans = 0
    for i in range(n):
        if l[i]:
            ans += 1
    print(ans)
    return


def C():

    return


def D():

    return


def E():

    return


def F():

    return


if __name__ == "__main__":
    B()

Solution (Python):
N, M = [int(i) for i in input().split()]
red = [False for i in range(N+1)]
red[1] = True
balls = [1 for i in range(N+1)]
for i in range(M):
    x, y = [int(_) for _ in input().split()]
    if red[x]:
        red[y] = True
    balls[x] -= 1
    if balls[x] == 0:
        red[x] = False
    balls[y] += 1
print(len([r for r in red if r]))

Solution (Python):
from sys import exit, stdin
from itertools import combinations
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
dp = [False]*(10**6)
dp2 = [0]*(10**6)
dp[1] = True
for i in range(N):
    dp2[i+1] = 1
for i in range(M):
    x, y = map(int, stdin.readline().rstrip().split())
    if dp[x]:
        dp[y] = True
    dp2[x] -= 1
    dp2[y] += 1
    if not dp2[x]:
        dp[x] = False
ans = 0
for i in range(N):
    if dp[i+1]:
        ans += 1
print(ans)

Solution (Python):
n, m = map(int, input().split())

B = [1] * n
R = [0] * n
R[0] = 1
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1; y -= 1

    if B[x] == 1 and R[x] == 1:
        R[x] =  0; R[y] =  1
    elif B[x] > 1 and R[x] == 1:
        R[y] =  1
    B[x] -= 1; B[y] += 1

print(len([i for i in R if i==1]))

Solution (Python):





from sys import stdin,setrecursionlimit


setrecursionlimit(10**6)
input=stdin.readline

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

Solution (Python):
N, M = map(int, input().split())

B = [1]*N
R=[False]*N

R[0]=True

for i in range(M):
    x, y = map(int, input().split())
    x-=1
    y-=1
    if R[x] == True:
        R[y] = True
    B[x] -= 1
    B[y] += 1
    if B[x] == 0:
        R[x] = False

print(sum(R))

Solution (Python):
N, M = map(int, input().split())

B = [1]*N
R=[0]*N

R[0]=1

for i in range(M):
    x, y = map(int, input().split())
    x-=1
    y-=1
    if R[x] == 1:
        R[y] = 1
    B[x] -= 1
    B[y] += 1
    if B[x] == 0:
        R[x] = 0

print(sum(R))

Solution (Python):
N, M = map(int, input().split())

B = [[1, 0] for i in range(N)]

B[0][1] = 1

for i in range(M):
    x, y = map(int, input().split())
    if B[x - 1][1] == 1:
        B[y - 1][1] = 1
    B[x - 1][0] -= 1
    B[y - 1][0] += 1
    if B[x - 1][0] == 0:
        B[x - 1][1] = 0

cnt = 0
for i in range(N):
    if B[i][1] == 1:
        cnt += 1

print(cnt)

Solution (Python):
n, m = map(int, input().split())

ball = [1] * n
flag = [True] + [False] * (n - 1)

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

	if ball[x] > 0:
		ball[x] -= 1
		ball[y] += 1

		if flag[x] == True:
			flag[y] = True
		if ball[x] == 0:
			flag[x] = False
	else:
		continue

print(sum(flag))

Solution (Python):
N, M, *XY = map(int, open(0).read().split())

red = [False] * (N + 1)
red[1] = True

box = [1] * (N + 1)

for x, y in zip(XY[::2], XY[1::2]):
    if red[x]:
        red[y] = True
    box[y] += 1
    box[x] -= 1
    if box[x] == 0:
        red[x] = False

print(red.count(True))

Solution (Python):



def main():
    n, m = map(int, input().split())
    num = [1 for _ in range(n)]
    red = [False for _ in range(n)]
    red[0] = True

    
    
    
    
    for i in range(m):
        xi, yi = map(int, input().split())
        xi -= 1
        yi -= 1

        if red[xi]:
            red[yi] = True

        num[xi] -= 1
        num[yi] += 1

        
        if num[xi] == 0:
            red[xi] = False

    print(sum(red))


if __name__ == '__main__':
    main()

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    N, M = tuple(map(int, input().split()))
    X = tuple(tuple(map(int, input().split())) for _ in range(M))
    R = [0] * (N + 1)  
    R[1] = 1
    C = [1] * (N + 1)  
    for x, y in X:
        if R[x]:
            R[y] = 1
        C[x] -= 1
        C[y] += 1
        if C[x] == 0:
            R[x] = 0
    ans = sum(R[1:])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N, M = tuple(map(int, input().split()))
    X = tuple(tuple(map(int, input().split())) for _ in range(M))
    R = [0] * (N + 1)  
    R[1] = 1
    C = [1] * (N + 1)  
    for x, y in X:
        if R[x]:
            R[y] = 1
        C[x] -= 1
        C[y] += 1
        if C[x] == 0:
            R[x] = 0
    ans = sum(R[1:])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    N, M = tuple(map(int, input().split()))
    X = tuple(tuple(map(int, input().split())) for _ in range(M))
    R = [0] * (N + 1)  
    R[1] = 1
    C = [1] * (N + 1)  
    for x, y in X:
        R[y] = 1 if R[x] or R[y] else 0
        C[x] -= 1
        C[y] += 1
        R[x] = 0 if C[x] == 0 else R[x]
    ans = sum(R[1:])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def sol():
    N, M = map(int, input().split())

    ball = [1] * N
    hasRed = [False] * N
    hasRed[0] = True

    for _ in range(M):
        fr ,to = map(int, input().split())
        fr -= 1
        to -= 1
        if hasRed[fr]:
            hasRed[to] = True
        ball[fr] -= 1
        if ball[fr] == 0:
            hasRed[fr] = False
        ball[to] += 1

    ans = 0
    for has, count in zip(hasRed, ball):
        if has and count > 0:
            ans += 1

    print(ans)


sol()

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from itertools import *
from copy import *

N, M = map(int, input().split())
num, red = [1] * (N+1), [0]+[1]+[0]*(N-1)
for i in range(M):
    x, y = map(int, input().split())
    red[y] |= red[x]
    num[x] -= 1
    num[y] += 1
    if num[x] == 0:
        red[x] = 0
print(sum(red))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N,M = map(int,input().split())
XY = [[int(x) for x in input().split()] for _ in range(M)]



ball = [None] + [1] * N
red = [None] + [1] + [0] * (N-1)

for x,y in XY:
    if red[x] == 0:
        
        ball[x] -= 1
        ball[y] += 1
        continue
    if red[x] == 1:
        if ball[x] == 1:
            
            red[x] = 0
            ball[x] -= 1
            red[y] = 1
            ball[y] += 1
        else:
            
            ball[x] -= 1
            ball[y] += 1
            red[y] = 1

answer = sum(red[1:])
print(answer)

ball

red

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

Solution (Python):
n,*l=map(int,open(0).read().split());a=[0,1]+[0]*n;b=[1]*-~n
for i,j in zip(l[1::2],l[2::2]):a[j]|=a[i];b[i]-=1;b[j]+=1;a[i]&=(b[i]>0)
print(sum(a))

Solution (Python):
n,m=map(int,input().split())
balls=[1 for i in range(n)]
pro=[False for i in range(n)]
pro[0]=True

for inp in range(m):
    a,b=map(int,input().split())
    a-=1
    b-=1
    balls[a]-=1
    balls[b]+=1
    if pro[a]:
        pro[b]=True
    if balls[a]==0:
        pro[a]=False



print(pro.count(True))

Solution (Python):
N,M = map(int,input().split())
box = [1]*N
red = [0]*N
red[0] = 1
for i in range(M):
    a,b = map(int,input().split())
    box[a-1] -= 1
    box[b-1] += 1
    if red[a-1] == 1:
        red[b-1] = 1
    if box[a-1] == 0:
        red[a-1] = 0
print(sum(red))

Solution (Python):
n,m=map(int,input().split())
xy=[list(map(lambda x:int(x)-1,input().split())) for _ in [0]*m]

cnt=[1 for _ in [0]*n]
red=[False for _ in [0]*n]
red[0]=1

for x,y in xy:
    cnt[x]-=1
    cnt[y]+=1
    if red[x]==True:
        red[y]=True
    if cnt[x]==0:
        red[x]=False
print(red.count(True))

Solution (Python):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
XY = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

counts = np.ones(N, dtype=int)
possible = np.zeros(N, dtype=bool)
possible[0] = 1

for x, y in XY:
    x -= 1
    y -= 1
    possible[y] |= possible[x]
    counts[x] -= 1
    counts[y] += 1
    if counts[x] == 0:
        possible[x] = False

ans = possible.sum()
print(ans)

Solution (Python):
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
sum=[1 for i in range(n)]
red=[0 for i in range(n)]
red[0]=1
for i in range(m):
  sum[l[i][0]-1]-=1
  sum[l[i][1]-1]+=1
  if red[l[i][0]-1]==1:
    red[l[i][1]-1]=1
  if sum[l[i][0]-1]==0:
    red[l[i][0]-1]=0
ct=0
for i in range(n):
  if sum[i]!=0 and red[i]==1:
    ct+=1
print(ct)

Solution (Python):
N,M=map(int,input().split())
xy=[list(map(int,input().split())) for i in range(M)]
b=[1]*N
r=[1]+[0]*(N-1)
for x,y in xy:
    b[x-1]-=1
    b[y-1]+=1
    if r[x-1]==1:
        r[y-1]=1
        if b[x-1]==0:
            r[x-1]=0
print(sum(r))

Solution (Python):
import math


N,M= list(map(int,input().split(" ")))

red = [False] * (N + 1)
nums = [1] * (N+1)
red[1] = True


for i in range(M):
    x, y =  list(map(int,input().split(" ")))

    nums[x]-=1
    nums[y]+=1
    if red[x]:
        red[y] = True
    if nums[x] == 0:
        red[x] = False
print(red.count(True))

Solution (Python):
n,m=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(m)]
l=[1]*n
red=set([0])
for i in a:
    l[i[0]-1]-=1
    l[i[1]-1]+=1
    if i[0]-1 in red:
        red.add(i[1]-1)
        if l[i[0]-1]==0:red.remove(i[0]-1)
print(len(red))

Solution (Python):
N,M = map(int,input().split())
XY = [list(map(int,input().split())) for _ in [0]*M]
R = [False]*N
R[0] = True
B = [1]*N
for x,y in XY:
    x-=1;y-=1
    B[y] += 1
    B[x] -= 1
    R[y] = R[y] or R[x]
    R[x] = R[x] and B[x]>0
print(sum(R))

Solution (Python):
N, M = map(int, input().split())
lst = [[0, 0], [1, 0]] + [[0, 1] for _ in range(N)]

for _ in range(M):
    x, y = map(int, input().split())
    if lst[x][0] == 0: 
        lst[x][1] -= 1 
        lst[y][1] += 1
    if lst[x][0] == 1: 
        if lst[x][1] == 0: 
            
            lst[x][0] -= 1
            if lst[y][0] == 0:
                lst[y][0] = 1
            else:
                lst[y][1] += 1
        else:
            
            lst[x][1] -= 1
            if lst[y][0] == 0:
                lst[y][0] = 1
            else:
                lst[y][1] += 1
count = 0
for i in lst:
    if i[0] == 1:
        count += 1
print (count)

Solution (Python):
N, M = map(int, input().split()) 
XY = [list(map(int, input().split())) for _ in range(M)]

balls = [1 for _ in range(N)]
red = [False for _ in range(N)]
red[0] = True
for i in range(M):
    x, y = XY[i]
    x -= 1
    y -= 1
    if red[x]:
        red[y] = True
        if balls[x] == 1:
            red[x] = False
    balls[x] -= 1
    balls[y] += 1
print(sum(red))

Solution (Python):
n,m=map(int,input().split())
l=[1]*(n+1)
r=set([1])
for i in range(m):
    x,y=map(int,input().split())
    if x in r:
        r.add(y)
    l[x]-=1
    l[y]+=1
    if l[x]==0 and x in r:
        r.remove(x)
print(len(r))

Solution (Python):
N, M = [int(_) for _ in raw_input().split(" ")]

ret = [1] * N
color = [False]*N
color[0] = True
for i in range(M):
  _from, _to = [int(_) for _ in raw_input().split(" ")]
  ret[_from-1] -= 1
  ret[_to-1] += 1
  if color[_from-1] == True:
    color[_to-1] = True
  if ret[_from-1] == 0:
    color[_from-1] = False
  
  
print color.count(True)

Solution (Python):
N,M=map(int,input().split())
flg=[0]*(N+1)
flg[1]=1
flg2=[1]*(N+1)
for _ in range(M):
    xy=list(map(int,input().split()))
    if flg[xy[0]]==1:
        flg[xy[1]]=1
    flg2[xy[1]]+=1
    flg2[xy[0]]-=1
    if flg2[xy[0]]==0:
        flg[xy[0]]=0
print(sum(map(lambda x:x>0,flg)))

Solution (Python):
N,M = map(int,input().split())

L = [1 for i in range(N)]

R = [0 for i in range(N)]
R[0] = 1

for i in range(M) :
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    
    L[x] -= 1
    L[y] += 1
    
    if R[x] == 1 and R[y] == 0 :
        R[y] = 1
    
    if L[x] == 0 and R[x] == 1 :
        R[x] = 0
    

print(sum(R))

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, m = li()
balls = [1]*n
canex = [0]*n

canex[0] = 1

for _ in range(m):
    x, y = li_()
    balls[x] -= 1
    balls[y] += 1

    if balls[x] > 0 and canex[x]:
        canex[y] = 1

    elif balls[x] == 0 and canex[x]:
        canex[y] = 1
        canex[x] = 0

print(sum(canex))

Solution (Python):
def main():
    N, M = map(int, input().split())
    cnt = [1] * (N+1)
    cand = set([1])
    for _ in range(M):
        x, y = map(int, input().split())
        cnt[x] -= 1
        cnt[y] += 1
        if x in cand:
            cand.add(y)
            if cnt[x] == 0:
                cand.remove(x)
    print(len(cand))


if __name__ == "__main__":
    main()

Solution (Python):
N, M = map(int, input().split())
xy = [[int(_) for _ in input().split()] for _ in range(M)]
red = [False]*(N+1)
red[1] = True
ballnum = [1]*(N+1)

for i in xy:
    x = i[0]
    y = i[1]
    ballnum[x] -= 1
    ballnum[y] += 1
    if red[x]:
        red[y] = True
        if ballnum[x] == 0:
            red[x] = False

print(red.count(True))

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

Solution (Python):
N, M = map(int, input().split())
ballcount = []
for i in range(0, N):
	ballcount.append(1)
redlist = [True]
for i in range(1, N):
	redlist.append(False)
for i in range(0, M):
	x, y = map(int, input().split())
	if redlist[x-1]: redlist[y-1] = True
	ballcount[x-1] -= 1
	ballcount[y-1] += 1
	if ballcount[x-1] == 0: redlist[x-1] = False
cnt = 0
for i in range(0, N):
	if redlist[i]: cnt += 1
print(cnt)

Solution (Python):
N,M=map(int,input().split())
red=[0]*(N+1)
cnt=[1]*(N+1)
red[1]=1
for i in range(M):
    x,y=map(int,input().split())
    if red[x]==1:
        red[y]=1
        if cnt[x]==1:
            red[x]=0
    cnt[x]-=1
    cnt[y]+=1
print(red.count(1))

Solution (Python):
n,m = map(int,input().split())
num = [1 for c in range(n)]
red = [False for d in range(n)]
red[0] = True
counter = 0
for i in range(m):
    x,y = map(int,input().split())
    if red[x-1]:
        red[y-1] = True
    num[x-1] = num[x-1] - 1
    num[y-1] = num[y-1] + 1
    if num[x-1] == 0:
        red[x-1] = False
for j in range(n):
    if (red[j] == True)&(num[j]>0):
        counter += 1
print(counter)

Solution (Python):
N,M = [int(it) for it in input().split()]
li = [ [int(it)-1 for it in input().split()] for i in range(M)]


k0 = [0]*N
c0 = [1]*N

k0[0] = 1


for a,b in li:
  ka,kb,ca,cb = k0[a],k0[b],c0[a],c0[b]
  ga,gb=ka,kb
  if (ca==1 and ka==1):
    ga,gb=0,1
  if (ca>1  and ka==1):
    ga,gb=1,1
  k0[a],k0[b]=ga,gb

  c0[a]-=1
  c0[b]+=1
  
print ( sum(k0) )

Solution (Python):
N, M = map(int, input().split())
op = []
for _ in range(M):
    x, y = map(int, input().split())
    op.append((x, y))
box = {i: 1 for i in range(1, N+1)}
possible = {i: False for i in range(1, N+1)}
possible[1] = True
for x, y in op:
    box[x] -= 1
    box[y] += 1
    if possible[x]:
        possible[y] = True
    if box[x] == 0:
        possible[x] = False
print(sum(possible.values()))

Solution (Python):
n, m = [int(v) for v in input().split()]
a_s = [[int(v) - 1 for v in input().split()] for _ in range(m)] 

ls = [[1, 0] for _ in range(n)]
ls[0][1] = 1

for a in a_s:
  x, y = a
  
  
  ls[x][0] -= 1
  ls[y][0] += 1
  
  if ls[x][1] == 1:
    ls[y][1] = 1
  
  if ls[x][0] == 0:
    ls[x][1] = 0
    
print(sum([e[1] for e in ls]))

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

Solution (Python):
n,m=map(int,input().split())
cnt=[1]*n
s=set([1])
for i in range(m):
  x,y=map(int,input().split())
  cnt[y-1]+=1
  cnt[x-1]-=1
  if x in s:
    s.add(y)
  if cnt[x-1]==0:
    if x in s:
      s.remove(x)
print(len(s))

Solution (Python):
N,M=map(int,input().split())
xy=[list(map(int,input().split())) for _ in [0]*M]
n = [1]*N
r = [0]*N
r[0] = 1
for x,y in xy:
    x-=1;y-=1
    n[x] -= 1
    n[y] += 1
    r[y] = r[y] or r[x]
    if n[x] == 0 : r[x] = 0
print(sum(r))

Solution (Python):
n,m=map(int,input().split())
A=[False]*n
B=[1]*n
A[0]=True
for i in range(m):
  x,y=map(int,input().split())
  if A[x-1]:
    A[y-1]=True
  B[x-1]-=1
  B[y-1]+=1
  if B[x-1]==0:
    A[x-1]=False
print(A.count(True))

Solution (Python):
N,M = list(map(int, input().split()))
XY = [list(map(int, input().split())) for _ in range(M)]

red = set([1])
n_ball = [1]*(N+1)

for x,y in XY:
  n_ball[x] -= 1
  n_ball[y] += 1
  if x in red:
    red.add(y)
    if n_ball[x]==0:
      red.remove(x)
print(len(red))