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



Sample input:
3 2
1 2
2 3


Sample output:
2

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N, M = MAP()
xy = [LIST() for _ in range(M)]

check = [0]*N
check[0] = 1
cnt = [1]*N

for x, y in xy:
	if check[x-1]:
		check[y-1] = 1
	cnt[x-1] -= 1
	cnt[y-1] += 1
	if cnt[x-1] == 0:
		check[x-1] = 0

print(sum(check))

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline()[:-1]


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

red = [0]*N
red[0] = 1
box = [1]*N
for _ in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1

    if red[x] == 1 and box[x] == 1:
        # 箱Xには赤玉がある可能性があって、全ボール数が1個のみがある場合
        red[x] -= 1
        red[y] = 1
    elif red[x] == 1 and box[x] >= 2:
        # 箱Xには赤玉がある可能性があって、全ボール数が2個以上ある場合
        red[y] = 1
    box[x] -= 1
    box[y] += 1

print(red.count(1))

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline()[:-1]


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

red = [0]*N
red[0] = 1
box = [1]*N
for _ in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1

    if red[x] == 0:
        # 箱Xには赤玉がない場合
        box[x] -= 1
        box[y] += 1
    elif red[x] == 1 and box[x] == 1:
        # 箱Xには赤玉がある可能性があって、全ボール数が1個のみがある場合
        box[x] -= 1
        box[y] += 1
        red[x] -= 1
        red[y] = 1
    elif red[x] == 1 and box[x] >= 2:
        # 箱Xには赤玉がある可能性があって、全ボール数が2個以上ある場合
        box[x] -= 1
        box[y] += 1
        red[y] = 1

print(red.count(1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
box = [1] * N
red = [1]
red.extend([0] * (N - 1))

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


print(sum(red))

Submission status: Accepted

Solution code:
import numpy as np
n, m = map(int, input().split())
'''
xx = []
yy = []
'''
box = np.zeros((n, 2))
box[:, 0] = 1
box[0, 1] = 1


count = 0
for _ in range(m):
    x, y = map(int, input().split())
    '''
    xx.append(x)
    yy.append(y)
    '''
    if (box[x - 1, 1] == 1):
        box[y - 1, 1] = 1

    box[x - 1, 0] -= 1

    if (box[x - 1, 0] == 0):
        box[x - 1, 1] = 0

    box[y - 1, 0] += 1

    # 赤玉が一度でもある場合
tar = box[:, 1]
ans = np.sum(tar != 0)
print(ans)

#xy = [list(map(int, input().split())) for _ in range(m)]

Submission status: Accepted

Solution code:
(N, M), *AB = [map(int, s.split()) for s in open(0)]
cnt = [1] * N
possibly_red = [False] * N
possibly_red[0] = True
for a, b in AB:
    a -= 1
    b -= 1
    cnt[a] -= 1
    cnt[b] += 1
    if possibly_red[a]:
        possibly_red[b] = True
        if cnt[a] == 0:
            possibly_red[a] = False
print(sum(possibly_red))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
rednow = [-1, 1] + [0 for i in range(n-1)] # maybe there's red ball
balnum = [-1] + [1 for i in range(n)] # the number of ball, each box
order = []
for i in range(m):
  order.append(list(map(int, input().split()))) 

for q in order:
  if rednow[q[0]] == 1:
    rednow[q[1]] = 1
    if balnum[q[0]] == 1:
      rednow[q[0]] = 0
  balnum[q[0]] -= 1
  balnum[q[1]] += 1

print(rednow.count(1))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

N,M = map(int,readline().split())
B = [1] * N
possible = {0}
for i in range(M):
  x,y = map(int,readline().split())
  x,y = x - 1,y - 1
  B[x] -= 1
  B[y] += 1
  if x in possible:
    possible.add(y)
  if x in possible and B[x] == 0:
    possible.remove(x)

print(len(possible))

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
red = [0] * N
red[0] = 1
boal = [1] * N

for i in range(M):
    x, y = map(int,input().split())
    x -= 1
    y -= 1
    if red[x] == 1 and boal[x] == 1:
        red[x] = 0
        boal[x] -= 1
        red[y] = 1
        boal[y] += 1
    elif red[x] == 1 and boal[x] > 1:
        boal[x] -= 1
        red[y] = 1
        boal[y] += 1
    else:
        boal[x] -= 1
        boal[y] += 1
print(sum(red))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
possibility = [False] * N  # True: 赤いボールが入っている可能性がある
possibility[0] = True
balls = [1] * N  # 箱に入っているボールの数
for i in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    if possibility[x]:
        possibility[y] = True
    balls[x] -= 1
    balls[y] += 1
    if balls[x] == 0:
        possibility[x] = False

ans = possibility.count(True)
print(ans)

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
XY = [list(map(int,input().split())) for _ in range(M)]
L1 = [1]*N
L2 = [0]*N
L2[0] = 1
for xy in XY:
    if L2[xy[0]-1] == 1:
        L2[xy[1]-1] = 1
    L1[xy[0]-1] -= 1
    L1[xy[1]-1] += 1
    if L1[xy[0]-1] == 0:
        L2[xy[0]-1] = 0
print(sum(L2))

Submission status: Accepted

Solution code:
# B - Box and Ball
N,M = map(int,input().split())
move = [list(map(int,input().split())) for _ in range(M)]
box = [[1,0] for _ in range(N+1)]
box[1][1] = 1
for x,y in move:
    box[x][0] -= 1
    box[y][0] += 1
    if box[x][1]==1:
        box[y][1] = 1
        if box[x][0]==0:
            box[x][1] = 0
print(sum(b[1] for b in box))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

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

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

for _ in range(M):
    x,y = map(int,readline().split())
    x,y = x-1,y-1
    if r[x]:
        if c[x] == 1:
            r[x] = 0
        r[y] = 1
    c[x] -= 1
    c[y] += 1

print(sum(r))

Submission status: Accepted

Solution code:
def main():
    n, m = list(map(int, input().split()))
    ans = set([1, ])
    balls = [1] * n
    for _ in range(m):
        x, y = list(map(int, input().split()))
        if x in ans:
            ans.add(y)
        if balls[x - 1] == 1 and x in ans:
            ans.remove(x)
        balls[x - 1] -= 1
        balls[y - 1] += 1
    print(len(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N, M = [int(x) for x in input().strip().split()]
    f = [False] * N
    c = [1] * N
    f[0] = True
    
    for m in range(M):
        x, y = [int(x) for x in input().strip().split()]
        f[y-1] = f[y-1] or f[x-1]
        c[y-1] += 1
        c[x-1] -= 1
        if c[x-1] == 0:
            f[x-1] = False
        
    print(len([1 for b in f if b]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X, Y = [], []
d = {n:1 for n in range(N)}
r = set([0])
for _ in range(M):
    x, y = map(int, input().split())
    d[x-1] -= 1
    d[y-1] += 1
    if x - 1 in r:
        r.add(y - 1)
        if d[x - 1] == 0:
            r.remove(x - 1)
    X.append(x)
    Y.append(y)

print(len(r))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

n,m = map(int,input().split())
l = []
for _ in range(m):
    a = list(map(int,input().split()))
    l.append(a)

memo = np.zeros((n,2))
memo[0][1] = 1

for i in range(n):
    memo[i][0] = 1

tmp1 = 0
tmp2 = 0

for j in range(m):
    tmp1 = l[j][0]-1
    tmp2 = l[j][1]-1
    memo[tmp1][0] -= 1
    memo[tmp2][0] += 1
    
    if memo[tmp2][1] == 0 and memo[tmp1][1] == 1:
        memo[tmp2][1] = 1
    
    if memo[tmp1][1] == 1 and memo[tmp1][0] == 0:
        memo[tmp1][1] = 0

ans = 0
for k in range(n):
    ans += memo[k][1]

print(int(ans))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = [0] * (M)
Y = [0] * (M)

started = False
contains_red = set([1])
ans = 1

nums = [1] * (N + 1)

for _ in range(M):
    x, y = map(int, input().split())
    X[_] = x
    Y[_] = y

for _ in range(M):
    nums[X[_]] -= 1
    nums[Y[_]] += 1

    if X[_] in contains_red:
        contains_red.add(Y[_])

    if nums[X[_]] == 0 and X[_] in contains_red:
        contains_red.remove(X[_])

# print(contains_red)
print(len(contains_red))

Submission status: Accepted

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

R = [0 for _ in range(N+1)] # possibility to include the red ball
C = [1 for _ in range(N+1)] # number of balls
R[1] = 1


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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ball = [1]*n
ans = [0]*n
ans[0] = 1

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

a = 0
for an in ans:
    if an == 1:
        a += 1

print(a)

Submission status: Accepted

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

flag = [0]*(N+1)
flag2 = [0]*(N+1)
flag[1] = 1
flag2[1] = 1
num = [1]*(N+1)

for xy in XYs:
  if flag2[xy[0]]==1 and flag[xy[0]]==1:
    flag[xy[0]]=0
    flag[xy[1]]=1
  elif flag[xy[0]]==1:
    flag[xy[1]]=1
  
  if flag2[xy[0]]==1 and num[xy[1]] == 0:
    flag2[xy[0]]=0
    flag2[xy[1]]=1
  else:
    flag2[xy[0]]=0
    flag2[xy[1]]=0
  num[xy[0]] -= 1
  num[xy[1]] += 1
  if num[xy[0]] == 0:
    flag[xy[0]] = 0
  
print(sum(flag))

Submission status: Accepted

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

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

ans = 0
for i in range(n):
    if s[i] and b[i]:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
xyl = [list(map(int,input().split())) for nesya in range(m)]
red = [0]*n
ball = [1]*n
red[0] = 1
for xy in xyl:
  x = xy[0]-1
  y = xy[1]-1
  if red[x] > 0:
    red[y] = 1
    if ball[x] == 1:
      red[x] = 0
  ball[x] -= 1
  ball[y] += 1
print(sum(red))

Submission status: Accepted

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

balls_in_box = [0] + [1 for _ in range(1,N+1)]
red_ball_containable_box = set()
red_ball_containable_box.add(1)

for i in range(M):
    x,y = map(int, input().split())
    if x in red_ball_containable_box:
        balls_in_box[x] -= 1
        balls_in_box[y] += 1
        red_ball_containable_box.add(y)
        if balls_in_box[x] == 0: #赤玉が入っている可能性が100％の箱を全部空にすれば確実に移る
            red_ball_containable_box.remove(x)
    else:
        balls_in_box[x] -= 1
        balls_in_box[y] += 1

print(len(red_ball_containable_box))

Submission status: Accepted

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

red = [False]*n
red[0] = True
balls = [1]*n

for x, y in mat:
    x -= 1
    y -= 1
    if red[x] is True:
        red[y] = True
    balls[x] -= 1
    balls[y] += 1
    if balls[x] == 0:
        red[x] = False

ans = 0
for r in red:
    if r is True:
        ans += 1
print(ans)

Submission status: Accepted

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

box = [1] * (n+1) 
redbox = [False] * (n+1) 
redbox[1] = True
for _ in range(m):
    x,y = list(map(int, input().split()))
    box[x]-=1
    box[y]+=1
    if redbox[x]:
        redbox[y] = True
    if box[x]==0:
        redbox[x] = False
#    print(box)
#    print(redbox)
print(sum(map(int, redbox[1:])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = [int(x) for x in input().split()]
xy = []
for _ in range(m):
    tmp = [int(x)-1 for x in input().split()]
    xy.append(tmp)

box = {x: 1 for x in range(n)}
red = {x: False for x in range(n)}
red[0] = True

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

print(len([1 for x in red.values() if x]))

Submission status: Accepted

Solution code:
def main():
    N,M  = map(int,(input().split()))
    BOX = [0]+[1]*N
    RED = [False]*(N+1)
    RED[1] = True

    for _ in range(M):
        x,y = map(int,(input().split()))
        BOX[x] -=1
        BOX[y] +=1
        if RED[x]:
            RED[y]=True
        if BOX[x]==0:
            RED[x]=False
    cnt = 0
    for r in RED:
        if r:
            cnt +=1
    print(cnt)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
C = [1]*n
X = [0]*n
X[0] = 1
for i in range(m):
    x, y = map(int, input().split())
    x, y = x-1, y-1
    if X[x] == 1:
        C[x] -= 1
        C[y] += 1
        X[y] = 1
        if C[x] == 0:
            X[x] = 0
    else:
        C[x] -= 1
        C[y] += 1
#print(C)
#print(X)
print(sum(X))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
d = {i: [1, 0] for i in range(1, n+1)}
d[1][1] = 1
for _ in range(m):
    x, y = map(int, input().split())
    d[x][0] -= 1
    d[y][0] += 1
    if d[x][1] == 1:
        d[y][1] = 1
    if d[x][0] == 0:
        d[x][1] = 0
ans = sum(d[i][1] for i in range(1, n+1))
print(ans)

Submission status: Accepted

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

a = [[1, False] for _ in range(n+1)]
a[1] = [1, True]

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

print(sum(x[1] for x in a))

Submission status: Accepted

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

# ボール個数、赤いボールがある可能性を管理
b = [1] * n
red = [1] + [0] * (n - 1)

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

    # 移す元に赤いボールがある可能性があれば、移す先に赤いボールが渡る可能性がある
    if red[x - 1] == 1:
        red[y - 1] = 1

    # ボール数の足し引き
    b[x - 1] -= 1
    b[y - 1] += 1

    # xのボールが0になったら赤いボールがある可能性が消える
    if b[x - 1] == 0:
        red[x - 1] = 0

ans = len([x for x in red if x > 0])

print(ans)

Submission status: Accepted

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

# ボール個数、赤いボールがある可能性を管理
b = [1] * n
red = [1] + [0] * (n - 1)

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

    # 移す元に赤いボールがある可能性があれば、移す先に赤いボールが渡る可能性がある
    if red[x - 1] == 1:
        red[y - 1] = 1

    # ボール数の足し引き
    b[x - 1] -= 1
    b[y - 1] += 1

    # xのボールが0になったら赤いボールがある可能性が消える
    if b[x - 1] == 0:
        red[x - 1] = 0

ans = len([x * y for x, y in zip(b, red) if x * y > 0])

print(ans)

Submission status: Accepted

Solution code:
N,M= list(map(int,input().split()))
dot=[1]*N
sw=[0] * N
sw[0]=1
for i in range(M):
   a,b=list(map(int,input().split()))
   a-=1;b-=1
   dot[a]-=1
   dot[b]+=1
   if sw[a]:
      sw[b]=1
   if dot[a] == 0:
      sw[a]=0
print(sw.count(1))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
ans=[0 for _ in range(n+1)]
cnt=[1 for _ in range(n+1)]
ans[1]=1
for i in range(m):
  a,b=map(int,input().split())
  if ans[a]==1:
    ans[b]=1
    if cnt[a]==1:
      ans[a]=0
  cnt[a]-=1
  cnt[b]+=1
print(sum(ans))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
checker = [1 for _i in range(n+1)]
r = [0 for i in range(n+1)]
r[1] = 1
for _i in range(m):
    x, y = map(int, input().split())
    checker[x] -= 1
    checker[y] += 1
    if r[x]>0:
        r[y] = 1
    if (r[x]>0) and (checker[x]==0):
        r[x] = 0
print(sum(r))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,M,*xy = map(int, read().split())

    check = [False] * N
    check[0] = True
    num = [1] * N

    for x, y in zip(*[iter(xy)]*2):
        num[x-1] -= 1
        num[y-1] += 1
        if check[x-1]:
            check[y-1] = True
        if num[x-1] == 0:
            check[x-1] = False

    ans = sum(check)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

exist = [0] * n
exist[0] = 1
cnt = [1] * n
for i in range(m):
    x, y = map(int, input().split())
    if exist[x - 1] == 1:
        exist[y - 1] = 1
        if cnt[x - 1] == 1:
            exist[x - 1] = 0

    
    cnt[x - 1] -= 1
    cnt[y - 1] += 1
print(sum(exist))

Submission status: Accepted

Solution code:
# AGC002 B Box and Ball

n, m = map(int, input().split())

b_list = [0] * (n+1)
b_list[1] = 1
c_list = [1] * (n+1)

for i in range(m):
    _x, _y = map(int, input().split())
    c_list[_x] -= 1
    c_list[_y] += 1 
    if b_list[_x] == 1:
        b_list[_y] = 1
        if c_list[_x] == 0:
            b_list[_x] = 0

print(sum(b_list))

Submission status: Accepted

Solution code:
import sys

N,M=map(int,input().split())
ope=[]
for i in range(M):
    ope.append(tuple(map(int,input().split())))

state=[1]*N
red=[0]*N
red[0]=1
for i in range(M):
    x,y=ope[i]
    if red[x-1]:
        if state[x-1]>1:
            red[x-1]=1
            red[y-1]=1
        else:
            red[x-1]=0
            red[y-1]=1
    state[x-1]-=1
    state[y-1]+=1

print(sum(red))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
num = [1] * N
possible = [False] * N
possible[0] = True

for i in range(M):
  x, y = map(int, input().split())
  x -= 1; y -= 1
  nx, ny = num[x], num[y]
  if possible[x]:
    possible[y] = True
    if nx == 1:
      possible[x] = False
  
  num[x] -= 1; num[y] += 1

print(sum(possible))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
kosuu=[0]+[1]*n
haitteru=[0]+[1]+[0]*(n-1)
for _ in range(m):
    x,y=map(int,input().split())
    if haitteru[x]==1:
        kosuu[x]-=1
        kosuu[y]+=1
        haitteru[y]=1
        if kosuu[x]==0:
            haitteru[x]=0
    else:
        kosuu[x]-=1
        kosuu[y]+=1
print(haitteru.count(1))

Submission status: Accepted

Solution 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())

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
cl = [1 for _ in range(n)]
rl = [1]+[0 for _ in range(n-1)]

for i in range(m):
    x_n, y_n = map(int, input().split())
    cl[x_n-1] -= 1
    cl[y_n-1] += 1
    if rl[x_n-1] == 1 and cl[x_n-1]>0:
        rl[y_n-1] = 1
    elif rl[x_n-1] == 1 and cl[x_n-1] == 0:
        rl[x_n-1] = 0
        rl[y_n-1] = 1
    elif rl[x_n-1] == 0:
        pass
print(sum(rl))

Submission status: Accepted

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

ball_count = [1]*N
visited = [0]*N
visited[0] = 1

for x,y in xy:
  ball_count[x-1] -= 1
  ball_count[y-1] += 1
  if visited[x-1] == 1:
    visited[y-1] = 1
  if ball_count[x-1] == 0:
    visited[x-1] = 0

print(sum(visited))

Submission status: Accepted

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

"""
ボールの数が入ったリストと，赤い玉が一回でもきたことがあるかのリスト
どちらもが０じゃないのが，終わったときに赤が入っている可能性のあるリスト
ただし，ボールリストが０になる時，seenは０になる
"""
ball_list = [1] * (N+1)
seen = [0] * (N+1)
ball_list[0] = 0
seen[1] = 1

for i in range(M):
    x = XY[i][0]
    y = XY[i][1]
    if seen[x] == 1:
        seen[y] = 1
        ball_list[x] -= 1
        ball_list[y] += 1
        if ball_list[x] == 0:
            seen[x] = 0
    else:
        ball_list[x] -= 1
        ball_list[y] += 1

print(seen.count(1))

Submission status: Accepted

Solution code:
import copy
N,M = [int(hoge) for hoge in input().split()]
Ball = [False]*N
BallNum = [1] * N
Ball[0] = True
for i in range(M):
    x,y = [int(hoge) - 1  for hoge in input().split()]
    BallNum[x] -= 1
    BallNum[y] += 1
    if Ball[x]:
        Ball[y] = True 
    if BallNum[x] == 0:
        Ball[x] = False
print(sum(Ball))

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
box = [[1,0] for i in range(n)]#個数、赤の可能性
box[0][1] = 1
ans = 1

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


print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n, m = MAP()
ans = [0 for _ in range(n)]
bal = [1 for _ in range(n)]
ans[0] = 1
for _ in range(m):
    x, y = MAP()
    bal[x - 1] -= 1
    bal[y - 1] += 1
    if ans[x - 1]:
        ans[y - 1] = 1
        if not bal[x - 1]:
            ans[x - 1] = 0
print(sum(ans))

Submission status: Accepted

Solution code:
def int_(num_str):
    return int(num_str) - 1

N, M = map(int, input().split())
balls = [1] * N
red = {0}
for i in range(M):
    x, y = map(int_, input().split())
    if x in red:
        if balls[x] == 1:
            red.remove(x)
            red.add(y)
        else:
            red.add(y)
    balls[x] -= 1
    balls[y] += 1
print(len(red))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
data = [[0,1] for i in range(n)]
data[0] = [1,1]
for i in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    data[b][1] += 1
    data[a][1] -= 1
    if data[a][0] == 1:
        data[b][0] = 1
    if data[a][1] == 0:
        data[a][0] = 0
else:
    ans = 0
    for i in data:
        if i[0] == 1:
            ans += 1
    else:
        print(ans)

Submission status: Accepted

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

r = [False] * n
r[0] = True
l = [1] * n

for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    l[x] -= 1
    l[y] += 1
    r[y] = r[x] | r[y]
    if not l[x]:
        r[x] = False

c = 0
for i in range(n):
    if r[i] == True and l[i] > 0:
        c += 1

print(c)

Submission status: Accepted

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

r = [False] * n
r[0] = True
l = [1] * n

for _ in range(m):
    x, y = map(int, input().split())
    l[x - 1] -= 1
    l[y - 1] += 1
    r[y - 1] = r[x - 1] | r[y - 1]
    if not l[x - 1]:
        r[x - 1] = False

c = 0
for i in range(n):
    if r[i] == True and l[i] > 0:
        c += 1

print(c)

Submission status: Accepted

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

r = [False] * n
r[0] = True
l = [1] * n

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

c = 0
for i in range(n):
    if r[i] == True and l[i] > 0:
        c += 1

print(c)

Submission status: Accepted

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

r = [False] * n
r[0] = True
l = [1] * n

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

c = 0
for i in range(n):
    if r[i] == True and l[i] > 0:
        c += 1

print(c)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x_li = [0]*m
y_li = [0]*m
for i in range(m):
    x_li[i],y_li[i]=map(int,input().split())

map_li = [1]*n
red = [False]*n
red[0]=True

for i in range(m):
    x,y = x_li[i],y_li[i]
    x,y = x-1,y-1
    if red[x]:
        red[y]=True
    map_li[x]-=1
    map_li[y]+=1
    if map_li[x]==0:
        red[x]=False
#print(map_li)
print(sum(red))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x_li = [0]*m
y_li = [0]*m
for i in range(m):
    x_li[i],y_li[i]=map(int,input().split())

map_li = [1]*n
red = [False]*n
red[0]=True

for i in range(m):
    x,y = x_li[i],y_li[i]
    x,y = x-1,y-1
    if red[x] and map_li[x]>1:
        red[y]=True
    elif red[x]:
        red[y]=True
        red[x]=False
    map_li[x]-=1
    map_li[y]+=1
#print(map_li)
print(sum(red))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
used = [False] * (n+1)
ball = [1] * (n+1)
used[1] = True
for _ in range(m):
    x,y = map(int, input().split())
    ball[x] -= 1
    ball[y] += 1
    if used[x]:
        used[y] = True
    if ball[x] == 0:
        used[x] = False
res = 0
for i in range(1, n + 1):
    if used[i] and ball[i] != 0:
        res += 1
print(res)
#print(ball)
#print(used)

Submission status: Accepted

Solution code:
n , m = map(int,input().split())
aka = [False for i in range(n+1)]
kazu = [1 for i in range(n+1)]
aka[1] = True
for i in range(m):
    x , y = map(int,input().split())
    if aka[x]:
        if kazu[x] >= 2:
            kazu[x] -= 1
            kazu[y] += 1
        elif kazu[x] == 1:
            kazu[x] -= 1
            kazu[y] += 1            
            aka[x] = False
        aka[y] = True
    elif not aka[x]:
        kazu[x] -= 1
        kazu[y] += 1
ans = 0
for i in range(1,n+1):
    if aka[i]:
        ans += 1
print(ans)

Submission status: Accepted

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

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

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

print(sum(status))

Submission status: Accepted

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

lisN = [0] + [1]*N
ansSet = set([1])

for _ in range(M):
    # print(ansSet,lisN)
    x,y = map(int,input().split())
    if x in ansSet:
        ansSet.add(y)
    lisN[x] -= 1
    lisN[y] += 1
    if lisN[x] == 0:
        ansSet.discard(x)

print(len(ansSet))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
hako = [1]*(n+1)
iro = ['white']*(n+1)
iro[1] = 'red'

for _ in range(m):
    
    x,y = map(int,input().split())
    if iro[x] == 'red':
        iro[y] ='red'
    elif iro[x] == 'white' and hako[y] == 0:
        iro[y] = 'white'
    hako[x] -= 1
    hako[y] += 1
ans = 0
for i in range(1,n+1):
    if hako[i] > 0 and iro[i] == 'red':
        ans += 1
print(ans)