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):


INF = float('inf')


def inpl(): return list(map(int, input().split()))


N, M = inpl()
move = [inpl() for _ in range(M)]

ans = [0 for _ in range(N)]
ans[0] = 1
box = [1 for _ in range(N)]

for i in range(M):
    x, y = move[i]
    box[x - 1] -= 1
    box[y - 1] += 1
    if ans[x - 1]:
        ans[y - 1] = 1
        if box[x - 1] == 0:
            ans[x - 1] = 0
    


print(sum(ans))

Solution (Python):
f = lambda: list(map(int,input().split()))
f_ = lambda: [int(x)-1 for x in input().split()]

n,m = f()
sus = set()
conf = [0]
num = [1 for _ in range(n)]
for _ in range(m):
    x,y = f_()
    num[x] -= 1; num[y] += 1
    if x in conf:
        if num[y] == 1:
            conf = [y]
        else:
            conf = []
            sus.add(y)
    elif y in conf:
        conf = []
        sus.add(y)
    elif x in sus:
        sus.add(y)
        if num[x] == 0:
            sus.remove(x)
print(max(len(sus),1))

Solution (Python):
n,m=map(int,input().split())
r=[False]*n
c=[1]*n
r[0]=True

for _ in range(m):
  x,y=map(lambda n:int(n)-1,input().split())
  if r[x]:
    r[y]=True
  c[x]-=1
  c[y]+=1
  if c[x]==0:
    r[x]=False

print(sum(r))

Solution (Python):
def main21():
    strbuf = input("");
    strbufs = strbuf.split();
    buf = [];
    for i in range(2):
        buf.append(int(strbufs[i]));
    kensa = [];
    for j in range(buf[1]):
        strbuf = input("");
        strbufs = strbuf.split();
        kari = [];
        for i in range(2):
            kari.append((int(strbufs[i]))-1);
        kensa.append(kari);
    flag = [];
    kazu = [];
    for i in range(buf[0]):
        flag.append(0);
        kazu.append(1);
    flag[0] = 1;
    for i in range(buf[1]):
        kazu[kensa[i][0]] -= 1;
        kazu[kensa[i][1]] += 1;
        if flag[kensa[i][0]] == 1:
            flag[kensa[i][1]] = 1;
        if kazu[kensa[i][0]] == 0:
            flag[kensa[i][0]] = 0;
    result = 0;
    for i in range(buf[0]):
        if flag[i] == 1:
            result+=1;
    print(result);

if __name__ == '__main__':
    main21()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, XY):
    rc = [False] * (N + 1)
    bn = [1] * (N + 1)

    rc[1] = True
    for x, y in XY:
        bn[x] -= 1
        bn[y] += 1
        if rc[x]:
            rc[y] = True
        if bn[x] == 0 and rc[x]:
            rc[x] = False

    return rc.count(True)


def main():
    N, M = read_int_n()
    XY = [read_int_n() for _ in range(M)]
    print(slv(N, M, XY))


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, XY):
    rc = [False] * (N + 1)
    bn = [1] * (N + 1)

    rc[1] = True
    for x, y in XY:
        bn[x] -= 1
        bn[y] += 1
        if rc[x]:
            rc[y] = True
        if bn[x] == 0 and rc[x]:
            rc[x] = False

    return rc.count(True)


def main():
    N, M = read_int_n()
    XY = [read_int_n() for _ in range(M)]
    print(slv(N, M, XY))


if __name__ == '__main__':
    main()

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

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

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

        if red[x-1] == 1:
            red[y-1] = 1
        if balls[x-1] == 0:
            red[x-1] = 0

    cnt = 0
    for i in range(n):
        if red[i] == 1:
            cnt += 1

    print(cnt)


if __name__ == '__main__':
    main()

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

ps = [False] * (N + 1)
cs = [1] * (N + 1)

ps[1] = True

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

result = sum(ps)

print(result)

Solution (Python):
def main():
  t = input().split(" ")
  N, M = int(t[0]), int(t[1])
  x = set([0])
  n = [1]*N
  for i in range(M):
    t = input().split(" ")
    fr, to = int(t[0])-1, int(t[1])-1
    if fr in x:
      x.add(to)
    n[fr] -= 1
    n[to] += 1
    if (fr in x) and n[fr] == 0:
        x.remove(fr)
  print(len(x))

main()

Solution (Python):
N, M = map(int, input().split())
ops = [list(map(lambda x: int(x) - 1, input().split())) for i in range(M)]



num = [1] * N
flg = [True] + [False] * (N - 1)

for x, y in ops:
    num[y] += 1
    if flg[x]:
        flg[y] = True

    num[x] -= 1
    if num[x] == 0:
        flg[x] = False

print(flg.count(True))

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

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


a = [1 for i in range(n)]
b = [0 for i in range(n)]
b[0] = 1
for i in x:
    a[i[0]] -= 1
    a[i[1]] += 1
    if b[i[0]] == 1:
        b[i[1]] = 1
    if a[i[0]] == 0:
        b[i[0]] = 0
print(sum(b))

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

red = [False] * (n+1)
num = [1] * (n+1)

red[1] = True

for x, y in X:
    num[x] -= 1
    num[y] += 1

    if red[x]:
        red[y] = True    
        if num[x] == 0:
            red[x] = False

print(red.count(True))

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

red = [0] * (n+1)
white = [1] * (n+1)

red[1] = 1
white[1] = 0

for x, y in X:
    if red[x] == 1:
        if white[x] >= 1:
            white[x] -= 1
        else:
            red[x] -= 1

        if red[y] == 0:
            red[y] += 1
        else:
            white[y] += 1

    else:
        white[x] -= 1
        white[y] += 1

print(sum(red))

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

Solution (Python):
import numpy as np
N, M = map(int, input().split())
a = []
for i in range(M):
    a.append([int(j) for j in input().split()])

num = np.ones((N+1,))
red = np.zeros((N+1,))
red[1] = 1

for i in range(M):
    if red[a[i][0]] == 1:
        red[a[i][1]] = 1
    num[a[i][0]] -= 1
    num[a[i][1]] += 1
    if num[a[i][0]] == 0:
        red[a[i][0]] = 0

print(np.sum(red == 1))

Solution (Python):
initial = list(map(int, input().split(" ")))
move_list=[]
for i in range(initial[1]):
    move_list.append(list(map(int,input().split(" "))))


contents_list = [[1, False] for i in range(initial[0]-1)]
contents_list.insert(0,[1,True])

for move in move_list:
    x_ball_num,x_red_ball_exist = contents_list[move[0]-1]
    if x_red_ball_exist:
        contents_list[move[1]-1][1] = True
        if x_ball_num == 1:
            contents_list[move[0]-1][1] = False


    contents_list[move[0]-1][0] -= 1
    contents_list[move[1]-1][0] += 1

cnt = 0
for content in contents_list:
    if content[1] == True:
        cnt += 1

print(cnt)

Solution (Python):
n, m = map(int, input().split())
actions = [list(map(int, input().split())) for _ in range(m)]
boxes = [[1, True]] + [[1, False] for _ in range(n - 1)]

for i in range(m):
	boxes[actions[i][0] - 1][0] -= 1
	boxes[actions[i][1] - 1][0] += 1
	if boxes[actions[i][0] - 1][1] == True:
		boxes[actions[i][1] - 1][1] = True
	if boxes[actions[i][0] - 1][0] == 0:
		boxes[actions[i][0] - 1][1] = False

ans = 0
for i in range(n):
	if boxes[i][0] > 0 and boxes[i][1] == True:
		ans += 1

print(ans)

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

B = [1 for i in range(N)]
P = set([1])
for i in range(M):
    x,y = map(int,input().split())
    B[x-1] -= 1
    B[y-1] += 1
    if x in P:
        P.add(y)
    if B[x-1] == 0 and x in P:
        P.remove(x)
        
print(len(P))

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

amt = [1 for i in range(n)]
red = [i == 0 for i in range(n)]

for i in range(m):
	a, b = input().split(' ')
	a, b = int(a), int(b)
	a -= 1
	b -= 1

	amt[a] -= 1
	amt[b] += 1

	red[b] |= red[a]

	if amt[a] == 0:
		red[a] = False

print(sum(red))

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

arr = [1 for i in range(n)]
isexist = [False for i in range(n)]

isexist[0] = True

for i in range(m):
	frm,to = map(int,raw_input().split())
	frm -= 1
	to -= 1

	arr[frm] -= 1
	arr[to] += 1

	if isexist[frm] :
		isexist[to] = True

	if arr[frm] == 0:
		isexist[frm] = False




count = 0
for i in range(n):
	if isexist[i] : 
		count += 1

print count

Solution (Python):

def inpl(): return map(int, input().split())

N, M = inpl()

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

balls = [1]*(N+1)

for _ in range(M):
    x, y = inpl()
    if prob[x]:
        prob[y] = True

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

    if balls[x] == 0:
        prob[x] = False

print(sum(prob))

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

for _ in range(m):
    a, b = map(int, input().split())
    xtoy.append([a, b])
    
exist_red = [0]*n
exist_red[0] = 1
exist_ball = [1]*n

for x, y in xtoy:
    if exist_red[x-1] == 1:
        exist_red[y-1] = 1
        
        if exist_ball[x-1] == 1:
            exist_red[x-1] = 0
    
    exist_ball[x-1] -= 1
    exist_ball[y-1] +=1
    

print(exist_red.count(1))

Solution (Python):
n, m = map(int,input().split())
move = [list(map(int,input().split())) for _ in range(m)]
balls = [1] * n
flag = [1] + [0] * n
for i in range(m):
    balls[move[i][0] - 1] -= 1
    balls[move[i][1] - 1] += 1
    if flag[move[i][0] - 1]:
        flag[move[i][1] - 1] = 1
        if balls[move[i][0] - 1] == 0:
            flag[move[i][0] - 1] = 0

print(sum(flag))

Solution (Python):

def init_box(n):
	b = []
	for i in range(n):
		b.append([False, 1])

	b[0][0] = True

	return b

def move(a, b, lst):
	if lst[a][1] == 1:
		if lst[a][0]:
			lst[a][0] = False
			lst[b][0] = True
		else:
			lst[a][0] = False
	else:
		if lst[a][0]:
			lst[b][0] = True

	lst[a][1] -= 1
	lst[b][1] += 1

def count_red(lst):
	c = 0
	for i in lst:
		if i[0]:
			c += 1

	return c


N, M = input().split()
N, M = int(N), int(M)
box = init_box(N)
act = []
for i in range(M):
	x, y = input().split()
	x, y = int(x), int(y)
	act.append([x - 1, y - 1])

for i in act:
	move(i[0], i[1], box)
print(count_red(box))

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

x=[0 for i in range(m)]
y=[0 for i in range(m)]
ans=[0 for i in range(n)]
count=[1 for i in range(n)]
ans[0]=1

for i in range(m):
  x[i],y[i]=[int(i) for i in input().split()]

for i in range(m):
  count[x[i]-1]-=1
  count[y[i]-1]+=1
  
  if ans[x[i]-1]==1:
    ans[y[i]-1]=1
  if count[x[i]-1]==0:
    ans[x[i]-1]=0
    


print(sum(ans))

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

Solution (Python):

import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]), int(input_raw[1])]
    adj_list = {}
    is_red = [False for i in range(N)]
    is_red[0] = True
    num = [1 for i in range(N)]
    for i in range(M):
        input_raw = read_func().strip().split()
        [x, y] = [int(input_raw[0]), int(input_raw[1])]
        if is_red[x - 1] == True:
            is_red[y - 1] = True
            if num[x - 1] <= 1:
                is_red[x - 1] = False
        num[x - 1] -= 1
        num[y - 1] += 1
    print sum(is_red)


if __name__ == '__main__':
    main()

Solution (Python):
N, M = [int(x) for x in input().split()]
m = {}
m[1] = {
    'count': 1,
    'passed': True
}
for i in range(2,N+1):
    m[i] ={ 
        'count': 1,
        'passed': False
    }
for _ in range(M):
    a, b = [int(x) for x in input().split()]
    m[b]['count'] += 1
    if m[a]['passed']:
        m[b]['passed'] = True
    m[a]['count'] -= 1
    if m[a]['count'] == 0:
        m[a]['passed'] = False
count = 0
for key in m:
    if m[key]['passed']:
        count += 1
print(count)

Solution (Python):
N, M = map(int, input().split())
flag = [0]*N
ball = [1]*N
flag[0] = 1
for _ in range(M):
    x, y = map(int, input().split())
    ball[x-1] -= 1
    ball[y-1] += 1
    if flag[x-1] == 1:
        flag[y-1] = 1
    if ball[x-1] == 0:
        flag[x-1] = 0
print(sum(flag))

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

redded = [False] * (N+1)
redded[1] = True
boll_num = [1] * (N+1)

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

ans = 0
for r, b in zip(redded, boll_num):
    if r and b:
        ans += 1
print(ans)

Solution (Python):
N,M = map(int,input().split())
a = []
for i in range(M):
    a.append(input())
 
b = [0 for i in range(N)]
b[0] = 1
c = [1 for i in range(N)]
for d in a:
    fr,to = map(int,d.split())
    fr -=1
    to -=1
    c[fr ] -= 1
    c[to ] += 1
    if b[fr] > 0:
        b[to] = 1
        b[fr] = 1 if c[fr] > 0 else 0
 
ans = b.count(1)
print(ans)

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

Solution (Python):
N, M = map(int, input().split())
XY = [list(map(int, input().split())) for i in range(M)]
boxes = [1] * (N+1)
reded = [False] * (N+1)
boxes[0] = 0
reded[1] = True

for x, y in XY:
    boxes[x] -= 1
    boxes[y] += 1

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

    if boxes[x] == 0:
        reded[x] = False

ans = 0
for n, r in zip(boxes, reded):
    if n and r:
        ans += 1
print(ans)

Solution (Python):


n, m = map(int, raw_input().split())
balls = [1] * n
reds = [False] * n
reds[0] = True

for _ in range(m):
    a, b = map(int, raw_input().split())
    a -= 1
    b -= 1
    balls[a] -= 1
    balls[b] += 1

    if reds[a]:
        reds[b] = True

    if balls[a] == 0:
        reds[a] = False

print(reds.count(True))

Solution (Python):
s = raw_input().split()
n = int(s[0])
m = int(s[1])

red = [False for x in range(n)]
red[0] = True

cnt = [1 for x in range(n)]

for _ in xrange(0, m):
    s = raw_input().split()
    x = int(s[0]) - 1
    y = int(s[1]) - 1

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

    if red[x]:
        red[y] = True
        if cnt[x] == 0:
            red[x] = False

print(sum(red))

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

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

Solution (Python):
n, m = [int(x) for x in raw_input().split()]
red = [True] + [False] * (n - 1)
cnt = [1] * n
for _ in range(m):
  x, y = [int(x) - 1 for x in raw_input().split()]
  cnt[x] -= 1
  cnt[y] += 1
  if red[x]:
    red[y] = True
    if cnt[x] == 0:
      red[x] = False

print sum(red)

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

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

Solution (Python):
n,m=map(int,input().split(' '))
box=[1]*n
red=[0]*n
red[0]=1
for i in range(m):
  x,y=map(lambda x:x-1,map(int,input().split(' ')))
  if box[x] == 1:
    red[y]=max(red[y],red[x])
    red[x]=0
  else:
    red[y]=max(red[y],red[x])
  box[x]-=1
  box[y]+=1
print(red.count(1))

Solution (Python):
n, m = (int(i) for i in input().split())

lst = [[int(i) for i in input().split()] for i in range(m)]

cnt = [1 for _ in range(n)]
r = [0 for _ in range(n)]
r[0] = 1
for e in lst:
    x = e[0] - 1
    y = e[1] - 1
    if r[x] == 1:
        r[y] = 1
    cnt[x] -= 1
    cnt[y] += 1
    if cnt[x] == 0:
        r[x] = 0

print(sum(r))

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = 10 ** 10

def main():
    N, M = map(int, input().split())
    num = [1] * N
    mark = [1] + [0] * (N - 1)
    for _ in range(M):
        x, y = map(int, input().split())
        x -= 1
        y -= 1

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

        if mark[x] == 1:
            mark[y] = 1

        if num[x] == 0:
            mark[x] = 0

    print(sum(mark))

if __name__ == '__main__':
    main()

Solution (Python):
N,M = list(map(int,input().split()))
x = []
y = []
red = [0 for i in range(N)]
mas = [1 for i in range(N)]
red[0] = 1

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

print(sum(red))

Solution (Python):
from sys import stdin

def main():
	N,M = [int(x) for x in stdin.readline().strip().split()]
	cantidad = [ 1 for x in range(N)]
	tenencia = [ 0 for x in range(N)]
	tenencia[0] = 1
	for i in range(M):
		x,y = [int(x) for x in stdin.readline().strip().split()]
		cantidad[y-1]+=1
		cantidad[x-1]-=1
		if tenencia[x-1] == 1:
			tenencia[y-1] = 1
		if cantidad[x-1] == 0:
			tenencia[x-1] = 0
	ans,i = 0,0
	while i < N:
		if cantidad[i] >= 1 and tenencia[i] == 1:
			ans+=1
		i+=1
	print(ans)
main()

Solution (Python):
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().strip().split()]
    lista = [1]*(n+1)
    vis = [False]*(n+1)
    vis[1] = True
    for w in range(m):
        x,y = [int(z) for z in stdin.readline().strip().split()]
        if(vis[x]):
            lista[y]+=1;
            lista[x]-=1;
            vis[y] = True
            if(lista[x] == 0):
                vis[x] = False
        else:
            lista[y]+=1;
            lista[x]-=1;
    print(vis.count(True))

main()

Solution (Python):


mozir=input()
hyo=mozir.split(' ')
Boxes=int(hyo[0])
Opes=int(hyo[1])

Red={0}
Balls=[1]*Boxes

for idx in range(0,Opes,1):
    mozir=input()
    hyo=mozir.split(' ')
    Sousa=((int(hyo[0])-1,int(hyo[1])-1))

    if Balls[Sousa[0]]==1:
        if Sousa[0] in Red:
            Red.remove(Sousa[0])
            Red.update({Sousa[1]})
        else:
            pass

    else:
        if Sousa[0] in Red:
            Red.update({Sousa[1]})
        else:
            pass
        
    Balls[Sousa[0]]-=1
    Balls[Sousa[1]]+=1
    

print(len(Red))

Solution (Python):
n,m = [int(x) for x in input().split(' ')]
prob = [1] + [0 for x in range(n-1)]
ball = [1 for x in range(n)]
for i in range(m):
    x,y = [int(x) for x in input().split(' ')]
    ball[x-1] -= 1
    ball[y-1] += 1
    if prob[x-1] == 1:
        prob[y-1] = 1
    if ball[x-1] == 0:
        prob[x-1] = 0

print(sum(prob))

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


flag = []
ball = []

for i in range(n):
    flag.append(False)
    ball.append(1)

flag[0] = True

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

    ball[y] += 1
    ball[x] -= 1
    flag[y] |= flag[x]

    if ball[x] == 0:
        flag[x] = False
    
ans = 0

for i in range(n):
    ans += flag[i]

print(ans)

Solution (Python):
n,m=map(int,raw_input().split())
ans=[False]*n
ans[0]=True
cnt=[1]*n
for i in xrange(m):
    x,y=map(int,raw_input().split())
    x-=1;y-=1
    if cnt[x]==1 and ans[x]:
        ans[x]=False
        ans[y]=True
    elif ans[x]:
        ans[y]=True
    cnt[x]-=1
    cnt[y]+=1
print sum(ans)

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

Solution (Python):
temp=input().split()
N=int(temp[0])
M=int(temp[1])
temp=[input().split() for i in range(M)]

x=[int(temp[i][0])-1 for i in range(M)]
y=[int(temp[i][1])-1 for i in range(M)]
flag=[False for i in range(N)]
lest=[1 for i in range(N)]

flag[0]=True

for i in range(M):
	if flag[x[i]]==True:
		if lest[x[i]]==1:
			flag[x[i]]=False
		flag[y[i]]=True
	lest[x[i]]-=1
	lest[y[i]]+=1

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

Solution (Python):
ii=lambda:map(int,raw_input().split())
n,m=ii()
q=[ii() for _ in range(m)]
r=[1]+[0]*(n-1)
b=[1]*n
for x,y in q:
    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,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**10
mod = 10**9 + 7


def f():
    n,m = list(map(int, input().split()))
    fs = [0] * (n+1)
    fs[1] = 1
    a = [1] * (n+1)
    for _ in range(m):
        x,y = list(map(int, input().split()))
        a[x] -= 1
        a[y] += 1
        if fs[x]:
            fs[y] = 1
        if a[x] < 1:
            fs[x] = 0
    return sum(fs)

print(f())

Solution (Python):
n,m=map(int,raw_input().split())
rw=[1]*n
rball=[True]+[False]*(n-1)
for i in xrange(m):
    x,y=map(int,raw_input().split())
    x-=1;y-=1
    if rw[x]==1:
        if rball[x]:
            rball[x]=False
            rw[x]-=1
            rw[y]+=1
            rball[y]=True
        else:
            rw[x]-=1
            rw[y]+=1
    else:
        rw[x]-=1
        rw[y]+=1
        if rball[x]:
            rball[y]=True
ans=0
for i in xrange(n):
    if rball[i]:
        ans+=1
print(ans)

Solution (Python):
NM = input()
NM = "".join(NM).rstrip("\n").split(" ")
lis = [1 for s in range(int(NM[0]))]
lis2 =[0 for s in range(int(NM[0]))]
lis2[0]+=1

for s in range(int(NM[1])):
	temp=input()
	temp = "".join(temp).rstrip("\n").split(" ")
	
	t0 = int(temp[0])
	t1 = int(temp[1])
	lis[t1-1]+=1
	if lis2[t0-1]==1:
		lis2[t1-1]=1
	lis[t0-1]-=1
	if lis[t0-1] ==0:
		if lis2[t0-1]>0:
			lis2[t0-1]=0


ans=0
for s in range(int(NM[0])):
	if lis2[s]==1:
		ans+=1
print(ans)

Solution (Python):

n,m=map(int,raw_input().split())
a=[1 for _ in xrange(n)]
r=[0 for _ in xrange(n)]
r[0]=1
for i in xrange(m):
 x,y=map(int,raw_input().split())
 x-=1
 y-=1
 a[x]-=1
 a[y]+=1
 if r[x]==1: r[y]=1
 if a[x]==0: r[x]=0
 
print sum(r)

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

Solution (Python):
n, l = (int(s) for s in input().strip().split(' '))
an = []
for a in range(0,l):
    an.append(input().strip().split())
box=[[1,0] for i in range(n)]
box[0][1] = 1
for move in an:
    box[int(move[0])-1][0] -= 1
    box[int(move[1])-1][0]+=1
    if box[int(move[0])-1][1] == 1:
        box[int(move[1])-1][1]= 1
    if box[int(move[0])-1][0]==0:
        box[int(move[0])-1][1]=0
print(str(sum([s[1] for s in box])))

Solution (Python):
import sys
nmlist = list(map(int,input().split(" ")))
N = nmlist[0]
M = nmlist[1]

moves = []
for i in range(M):
    moves.append(list(map(int,input().split(" "))))

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

for i in range(M):
    before = moves[i][0] - 1
    after = moves[i][1] - 1
    if box[before][0] != 0:
        box[before][0] -= 1
        if box[before][1] == 1:
            box[after][1] = 1
        if box[before][0] == 0:
            box[before][1] = 0
        box[after][0] += 1

output = 0
for i in range(N):
    output += box[i][1]

print(output)

Solution (Python):
class Box:
    def __init__(self, num=1, mayred=False):
        self.num = num
        self.mayred = mayred

n, m = (int(s) for s in input().strip().split(' '))
boxes = [Box() for _ in range(n)]
boxes[0].mayred = True
for _ in range(m):
    x, y = (int(s) for s in input().strip().split(' '))
    boxes[x-1].num -= 1
    boxes[y-1].num += 1
    if boxes[x-1].mayred == True:
        boxes[y-1].mayred = True
        if boxes[x-1].num == 0:
            boxes[x-1].mayred = False
print(len(list(filter(lambda b: b.mayred, boxes))))

Solution (Python):
N, M = map(int, input().split())
move = []
for i in range(M):
	move.append(list(map(int,input().split())))
box = [1 for i in range(N)]
box_flag = [False for i in range(N)]
box_flag[0] = True
for i in range(len(move)):
	box[move[i][0]-1] -= 1
	box[move[i][1]-1] += 1
	if box_flag[move[i][0]-1]:
		box_flag[move[i][1]-1] = True
	if box[move[i][0]-1] == 0:
		box_flag[move[i][0]-1] = False
print(box_flag.count(True))

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

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

for i in range(m):
    x,y=map(lambda x:int(x)-1,input().split())
    if arr[x][1]:
        if arr[x][0]>1:
            arr[x][0]-=1
            arr[y][0]+=1
            arr[y][1]=True
        else:
            arr[x][0]-=1
            arr[y][0]+=1
            arr[x][1]=False
            arr[y][1]=True
    else:
        arr[x][0]-=1
        arr[y][0]+=1

count=0
for i in range(n):
    if arr[i][1]:
        count+=1

print(count)

Solution (Python):
N, M = map(int, raw_input().split())
red = set([1])
num = [1 for i in xrange(N+1)]
for i in xrange(M):
    x, y = map(int, raw_input().split())
    num[x] -= 1
    num[y] += 1
    if x in red:
        red.add(y)
        if num[x] == 0:
            red.remove(x)
print len(red)

Solution (Python):

"""
Created on Mon Aug  1 00:19:18 2016

@author: dotha
"""


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

print(table2.count(True))

Solution (Python):
n, m = map(int,input().split())
f = [False] * n
f[0] = True
num = [1] * n

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

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

    if f[x]:
        f[y] = f[x]

    if num[x] == 0:
        f[x] = False

cnt = 0
for i in range(n):
    if f[i]:
        cnt += 1

print(cnt)

Solution (Python):
n, m = map(int, raw_input().split())
x, y = [], []
for i in range(m):
    x0, y0 = map(int, raw_input().split())
    x.append(x0-1)
    y.append(y0-1)
box = [1 for i in range(n)]
can = [False for i in range(n)]
can[0] = True
for i,j in zip(x,y):
    box[i] -= 1
    box[j] += 1
    if can[i]:
        if box[i] == 0:
            can[j] = True
            can[i] = False
        else:
            can[j] = True
print can.count(True)

Solution (Python):



from __future__ import division, print_function, absolute_import, unicode_literals

def main():
    N, M = map(int, input().split())
    p = [1] + [1] * (N-1)
    plob = [True] + [False] * (N-1)
    for _ in range(M):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        if p[x] == 0:
            continue
        p[x] -= 1
        p[y] += 1
        if plob[x] is True:
            if p[x] == 0:
                plob[x] = False
                plob[y] = True
            else:
                plob[y] = True
        
        
    print(sum(plob))
    
    
    

if __name__ == "__main__":
    main()