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, raw_input().split())
x = []
for i in xrange(M):
    x.append(map(int, raw_input().split()))
hako=[]
for i in xrange(N+1):
    if i==1:
        hako.append([1,1])
    else:
        hako.append([0,1])
for move in x:
    hako[move[0]][1]=hako[move[0]][1]-1
    hako[move[1]][1]=hako[move[1]][1]+1
    if hako[move[0]][0]==1:
        hako[move[1]][0]=1
    if hako[move[0]][1]==0:
        hako[move[0]][0]=0
s=0
for i in xrange(N+1):
    s=s+hako[i][0]
print s

Solution (Python):
n,m = map(int,raw_input().split())
arr = [1]*n
x = [False]*n
x[0] = True
counter = 0
for i in xrange(m) :
    a,b = map(int,raw_input().split())
    if x[a-1] :
        x[b-1] = True
    arr[a-1] -= 1
    arr[b-1] += 1
    if arr[a-1] == 0 :
        x[a-1] = False
for j in x :
    if j  :
        counter += 1
print counter

Solution (Python):


N, M = map(int, input().split())
B = {x+1:1 for x in range(N)}
f = {x+1:0 for x in range(N)}
f[1] = 1
for i in range(M):
    x, y = map(int, input().split())
    B[x] -= 1
    B[y] += 1
    if f[x] : f[y] = 1
    if B[x] < 1 : f[x] = 0
ans = 0
for k, v in f.items() : ans += v
print(ans)

Solution (Python):
n,m = map(int, input().split())
flag=[0]*n
flag[0]=1
ball=[1]*n
count=0

for i in range(m):
    xi, yi = map(int, input().split())
    xi-=1
    yi-=1
    ball[xi] = ball[xi] - 1
    ball[yi] = ball[yi] + 1
    if(flag[xi]==1):
        flag[yi] = 1
    if(ball[xi]==0):
        flag[xi]=0
for i in range(len(flag)):
    if(flag[i]==1):
        count+=1
print(count)

Solution (Python):
N,M=[int(x) for x in input().split()]
b=[1]*(N+1)
lst={1}
for i in range(M):
    x,y=[int(j) for j in input().split()]
    b[x]-=1
    b[y]+=1
    if x in lst:
        lst.add(y)
        if b[x]==0 :
            lst.remove(x)


    
        

print(len(lst))

Solution (Python):



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

red = set([1])
pos = [0] + [1] * N
for m in range(M):
    x,y = map(int,input().split())
    if x in red:
        red.add(y)
        if pos[x] == 1:
            red.remove(x)
    pos[x] -= 1
    pos[y] += 1
        
print(len(red))

Solution (Python):


import array


def main():
    n, m = map(int, input().split())
    deg = array.array("l", (0 for _ in range(n)))
    can_have_red = array.array("B", (False for _ in range(n)))
    can_have_red[0] = True
    for _ in range(m):
        x, y = map(lambda x: int(x) - 1, input().split())
        deg[x] -= 1
        deg[y] += 1
        if can_have_red[x]:
            can_have_red[y] = True
        if deg[x] < 0:
            can_have_red[x] = False
    print(sum(can_have_red))


if __name__ == '__main__':
    main()

Solution (Python):
n,m=map(int,raw_input().split())
s=[0]*n
s[0]=1
w=[1]*n
for i in range(m):
    x,y=map(int,raw_input().split())
    if s[x-1]==1:
        s[y-1]=1
        w[x-1]-=1
        w[y-1]+=1
    else:
        w[x-1]-=1
        w[y-1]+=1

    if w[x-1]==0:
        s[x-1]=0
print s.count(1)

Solution (Python):
import re
import functools

class IO_for_Contest(object):
    @staticmethod
    def my_input():
        
        return input()

    @staticmethod
    def read_from_input():
        n, m = IO_for_Contest.read_n_int(2)
        x, y = [], []
        for i in range(m):
            xx, yy = IO_for_Contest.read_n_int(2)
            x.append(xx)
            y.append(yy)
        return n, x, y

    @staticmethod
    def read_line():
        return IO_for_Contest.my_input().strip()

    @staticmethod
    def read_int():
        return int(IO_for_Contest.my_input().strip())

    @staticmethod
    def read_n_int(n):
        return list(map(int, re.split('\W+', IO_for_Contest.my_input().strip())))[ : n]

def solve():
    n, x, y = IO_for_Contest.read_from_input()
    m = len(x)
    print(count_boxes_can_have_red_ball(n, m, x, y))

def count_boxes_can_have_red_ball(n, m, x, y):
    """
    >>> n, m, x, y = 3, 2, [1, 2], [2, 3]
    >>> count_boxes_can_have_red_ball(n, m, x, y)
    2
    """
    balls = [0]
    red_ball_can_exist = [None]
    for i in range(n):
        if i == 0:
            red_ball_can_exist.append(True)
        else:
            red_ball_can_exist.append(False)
        balls.append(1)
    for i in range(m):
        f, t = x[i], y[i]
        balls[f] -= 1
        balls[t] += 1
        if red_ball_can_exist[f]:
            if balls[f] <= 0:
                red_ball_can_exist[f] = False
            red_ball_can_exist[t] = True
        else:
            pass
        
    return red_ball_can_exist.count(True)

if __name__ == '__main__':
    
    
    solve()

Solution (Python):
from sys import stdin
from itertools import repeat
def main():
    n, m = map(int, stdin.readline().split())
    dat = map(int, stdin.read().split(), repeat(10, 2 * m))
    a = [0] + [2] * n
    a[1] = 1
    for i in xrange(m):
        x, y = dat[i*2], dat[i*2+1]
	if a[x] == 0:
            continue
        a[y] += 2
        if a[x] & 1 and a[y] & 1 ^ 1:
            a[y] -= 1
        a[x] -= 2
	if a[x] < 0:
            a[x] = 0
    print sum(x & 1 for x in a)
main()

Solution (Python):


NM = map(int, raw_input().split(" "))
N = NM[0]
M = NM[1]
containRed = [True]
for i in xrange(N-1):
    containRed.append(False)

balles = [1]
for i in xrange(N-1):
    balles.append(1)

for i in xrange(M):
    xiyi = map(int, raw_input().split(" "))
    xi = xiyi[0] - 1
    yi = xiyi[1] - 1
    if containRed[xi]:
        if balles[xi] == 1:
            balles[xi] = 0
            containRed[xi] = False
            balles[yi] += 1
            containRed[yi] = True
        else:
            balles[xi] -= 1
            balles[yi] += 1
            containRed[yi] = True
    else:
        balles[xi] -= 1
        balles[yi] += 1
    
print sum(containRed)

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

Solution (Python):

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys

def s():
    return raw_input().strip()
def n():
    return int(raw_input())
def d():
    return float(raw_input())

def ls():
    return raw_input().strip().split()
def ln():
    return map(int, raw_input().strip().split())
def ld():
    return map(float, raw_input().strip().split())

def fs():
    return [raw_input().strip() for i in xrange(input())]
def fn():
    return [int(raw_input().strip()) for i in xrange(input())]
def fd():
    return [float(raw_input().strip()) for i in xrange(input())]

N, M = ln()

XY = []
for i in xrange(M):
    XY.append(ln())

candidate = set([1])
num = [0] + [1] * N
for x, y in XY:
    num[x] -= 1
    num[y] += 1
    if x in candidate:
        candidate.add(y)
    if num[x] == 0:
        candidate.discard(x)

print len(candidate)

Solution (Python):
N, M = map(int, raw_input().split())
num_ball = [1] * N
red_ball = [1] + [0] * (N-1)
for i in range(M):
    x, y = map(int, raw_input().split())
    x -= 1
    y -= 1
    
    if red_ball[x]:
        red_ball[y] = 1
    
    num_ball[x] -=1
    num_ball[y] +=1
    
    if num_ball[x] == 0:
        red_ball[x] = 0
        
print sum(red_ball)

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

c = [1] * N
p = [0] * N

p[0] = 1

for counter in range(M):
    x, y = input().split()
    x, y = int(x) - 1, int(y) - 1

    if(c[x] > 0):
        c[x] -= 1
        c[y] += 1

        if(p[x]==1):
            p[y]=1
        if(c[x]==0):
            p[x]=0

sum = 0
for i in range(N):
    if(p[i]>0):
        sum+=1

print(sum)

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

ans = 0
B[0] = 1
for k 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):
import sys

N, M = map(int, sys.stdin.readline().split())
xy_s = []
for i in range(M):
    x, y = map(int, sys.stdin.readline().split())
    xy_s.append((x-1, y-1))

flags = [False] * N
ns = [1] * N
flags[0] = True

for x, y in xy_s:
    if flags[x]:
        if ns[x] == 1:
            flags[x] = False
        flags[y] = True
    ns[x] -= 1
    ns[y] += 1

res = 0
for f in flags:
    if f:
        res += 1

print res

Solution (Python):

import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

N, M = map(int, raw_input().split())
a = [-1]*N
a[0] = 1 
for i in xrange(M):
    x, y = map(int, raw_input().split())
    x, y = x - 1, y - 1
    if a[x] >= 1:
        a[y] = abs(a[y]) + 1
        a[x] -= 1
    elif a[x] <= -1:
        if a[y] <= 0:
            a[y] = a[y] - 1
        elif a[y] > 0:
            a[y] += 1
        a[x] += 1
ans = 0
for ai in a:
    if ai > 0:
        ans += 1
print ans

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

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


	


dp = [False]*(N+1)
dp[1] = True
ball = [1]*(N+1)

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

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

print(cnt)

Solution (Python):
N, M = map(int, raw_input().split())
B = [1]*N
R = [True]+[False]*(N-1)
for i in range(M):
    x, y = map(int, raw_input().split())
    x -= 1
    y -= 1
    B[y] += 1
    if R[x]:
        R[y] = True
    B[x] -= 1
    if B[x]==0:
        R[x] = False
print sum(map(int, R))

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

Solution (Python):
n,m= map(int,raw_input().split())
box = [1 for _ in range(n+1)]
red = [0 for _ in range(n+1)]
red[1] = 1
for i in xrange(m):
    x,y = map(int,raw_input().split())
    box[x] -= 1
    box[y] += 1
    if red[x] == 1:
        red[y] = 1
    if box[x] == 0:
        red[x] = 0



print sum(red)

Solution (Python):
N,M=map(int,input().split())
S={1}
C=[1] * (N+1)
for _ in range(M):
    a,b=map(int,input().split())
    if a in S:
        S.add(b)
        if C[a] == 1:
            S.remove(a)
    C[b] += 1
    C[a] -= 1
print(len(S))

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

XY= [list(map(lambda x:int(x)-1,input().split())) for _ in range(M)]


red= [False for i in range(N)]
red[0]=True
balls= [1 for i in range(N)]


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

Solution (Python):
n, m = [int(x) for x in input().split()]
red = [2] + [0 for _ in range(1, n)]
num = [1] * n

for _ in range(m):
    x, y = [int(x) for x in input().split()]
    x -= 1
    y -= 1
    if red[x] > 0:
        if num[x] == 1:
            red[y] = red[x]
            red[x] = 0
        else:
            red[x] = 1
            red[y] = 1

    num[x] -= 1
    num[y] += 1

print(len([True for r in red if r > 0]))