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:
N, M = map(int, input().split())

W = [1] * (N + 1)
W[1] = 0
R = [0] * (N + 1)
R[1] = 1
cnt = [1] * (N + 1)

for _ in range(M):
    fr, to = map(int, input().split())
    cnt[fr] -= 1
    cnt[to] += 1

    if R[fr] == 1 and W[fr] >= 1:
        R[to] = 1
        W[to] += 1

    if R[fr] == 1 and W[fr] == 0:
        R[fr] = 0
        R[to] = 1

    if R[fr] == 0 and W[fr] >= 1:
        W[fr] -= 1
        W[to] += 1

    if cnt[fr] == 0:
        W[fr] = 0
        R[fr] = 0

ans = len([0 for r in R if r >= 1])
print(ans)

Submission status: Accepted

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

num_l=[1 for i in range(n)]
bit_l=[1]+[0 for i in range(n-1)]

for x,y in l:
  x-=1
  y-=1
  if bit_l[x]==1 and num_l[x]==1:
    bit_l[x]=0
    bit_l[y]=1
    num_l[x]-=1
    num_l[y]+=1
  elif bit_l[x]==1 and num_l[x]>=2:
    bit_l[y]=1
    num_l[x]-=1
    num_l[y]+=1
  else:
    num_l[x]-=1
    num_l[y]+=1

print(bit_l.count(1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, M = (int(i) for i in input().split())
    XY = [[int(i)-1 for i in input().split()] for j in range(M)]
    red = [False]*N
    red[0] = True
    cnt = [1]*N
    for x, y in XY:
        cnt[x] -= 1
        cnt[y] += 1
        if red[x]:
            red[y] = True
        if cnt[x] == 0:
            red[x] = False
    print(red.count(True))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:



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

prob = [False] * N
prob[0] = True
cnts = [1] * N

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

ans = 0
for i in range(N):
    if prob[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)]

exred = [i == 0 for i in range(N)]
n_ball = [1] * N

for i, (xi, yi) in enumerate(xy):
    exred[yi - 1] = exred[xi - 1] | exred[yi - 1]
    n_ball[xi - 1] -= 1
    n_ball[yi - 1] += 1
    if n_ball[xi - 1] == 0: exred[xi - 1] = False

ans = sum([int(item == True) for item in exred])
print(ans)

Submission status: Accepted

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

rs = [0] * N
rs[0] = 1
cs = [1] * N
for x,y in XY:
    x,y = x-1,y-1
    if rs[x]:
        if cs[x]==1:
            rs[x] = 0
        rs[y] = 1
    cs[x] -= 1
    cs[y] += 1
print(sum(rs))

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    x = [None] * m
    y = [None] * m
    for i in range(m):
        x[i], y[i] = map(int, input().split())

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

    print(sum(red))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
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 solve():
    n,m = LI()
    s = [1]*n
    f = [0]*n
    f[0] = 1
    for _ in range(m):
        a,b = LI()
        a -= 1
        b -= 1
        s[a] -= 1
        s[b] += 1
        if f[a]:
            f[b] = 1
            if s[a] == 0:
                f[a] = 0
    print(sum(f))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
"""
@author:  wangquaxiu
@time:  2020/4/20 22:04
"""


n,m = map(int,input().split())
box = [[1,0] for _ in range(n+1)]
box[1][1] = 1
# print(n,m)
count = 0
for i in range(m):
    x,y = map(int,input().split())
    if(box[x][0] == 1 and box[x][1] == 1):
        box[x][1] = 0
        box[y][1] = 1
    elif(box[x][1] == 1):
        box[y][1] = 1

    box[x][0] -= 1
    box[y][0] += 1

for b in box:
    if(b[1] == 1):
        count+=1

print(count)

Submission status: Accepted

Solution code:
def main():
    n,m=map(int,input().split())
    cnt=[1]*n
    res=[0]*n
    res[0]=1
    for i in range(m):
        x,y=map(int,input().split())
        if res[x-1]==1:
            res[y-1]=1
            if cnt[x-1]==1:
                res[x-1]=0
        cnt[x-1]-=1
        cnt[y-1]+=1
    print(sum(res))
if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n, m = map(int, input().split())

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

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

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

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

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

    print(sum(red))

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例1(self):
        input = """3 2
1 2
2 3"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例2(self):
        input = """3 3
1 2
2 3
2 3"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例3(self):
        input = """4 4
1 2
2 3
4 1
3 4"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

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

ans=1
n0=xy[0][1]
rb=[1]
rb[1:1]=[0]*(n-1)
cb=[1]*n
for i in range(m):
  if rb[xy[i][0]-1]==1:
    rb[xy[i][1]-1]=1
  cb[xy[i][1]-1]+=1
  cb[xy[i][0]-1]-=1
  if cb[xy[i][0]-1]==0:
    rb[xy[i][0]-1]=0

print(sum(rb))

Submission status: Accepted

Solution code:
import sys

n, m, *xy = map(int, sys.stdin.buffer.read().split())
xy = list(zip(xy[0::2], xy[1::2]))
counts = [1] * n
can = [False] * n
can[0] = True

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

print(sum(can))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    N, M = map(int, input().split())
    quantity = [1] * N
    red = [True] + [False] * (N - 1)

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

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


resolve()

Submission status: Accepted

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

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

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

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

print(R.count(1))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
a = [1]+[0]*(N-1)
b = [1]*N

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

Submission status: Accepted

Solution code:
n,m=list(map(int,input().split()))
B=[1]*n
R=[False]*n
R[0]=True

for i in range(m):
  x,y=list(map(int,input().split()))
  B[y-1]+=1
  B[x-1]-=1
#  print(B)
  if R[x-1]==True:
    if B[x-1]==0:
      R[x-1]=False
      R[y-1]=True
    else:
      R[y-1]=True      
#  print(R)
print(R.count(True))

Submission status: Accepted

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

dp = [1] * N
red = [False] * N
red[0] = True
for i in range(M):
    xi,yi = map(int, input().split())
    xi-=1
    yi-=1
    dp[yi] += 1
    red[yi] = red[yi] or red[xi]
    dp[xi] -= 1
    if dp[xi] == 0:
        red[xi] = False

ans = sum(red)
print(ans)

Submission status: Accepted

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

cnt = [1 for _ in range(N)]
red = [True] + [False for _ in range(N - 1)]

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

print(sum(red))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
move = [tuple(map(int, input().split())) for i in range(m)]

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

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

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

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)

num = [1 for _ in range(n)]
red = [False for _ in range(n)]
red[0] = True

for i in range(m):
    if red[xy[i][0]]:
        red[xy[i][1]] = True
    num[xy[i][0]] -= 1
    num[xy[i][1]] += 1
    if num[xy[i][0]] == 0:
        red[xy[i][0]] = False

print(sum([1 for x in red if x == True]))

Submission status: Accepted

Solution code:
# import fractions
# import sys
from sys import stdin

n, m = map(int, stdin.readline().rstrip().split())
xy = [list(map(int, stdin.readline().rstrip().split())) for i in range(m)]

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

print(sum(red))

Submission status: Accepted

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

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

print(boxes_status.count(True))

Submission status: Accepted

Solution code:
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())
    x, y = x - 1, y - 1
    if red[x]:
        red[y] = True

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

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

print(sum(red))

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    N, M = map(int, input().split())
    box = [0] + [1 for _ in range(N)]
    red = [0, 1] + [0 for _ in range(N - 1)]
    for _ in range(M):
        x, y = map(int, input().split())
        if red[x]:
            red[y] = 1
        box[x] -= 1
        box[y] += 1
        if not box[x]:
            red[x] = 0
    answer = sum(red)
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for _ in range(M):
    xi, yi = map(int, input().split())
    X.append(xi)
    Y.append(yi)

possible = [True] + [False] * (N - 1)
count = [1] * N

for i in range(M):
    xi, yi = X[i] - 1, Y[i] - 1

    # xiに赤があれば，yiも赤にできる
    if possible[xi]:
        possible[yi] = True
    # 数の変化
    count[xi] -= 1
    count[yi] += 1
    # 0になったら，赤は存在しない
    if count[xi] == 0:
        possible[xi] = False

count = 0
for f in possible:
    if f:
        count += 1
print(count)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    l = []
    for i in range(M):
        l.append(list(map(int, input().split())))
    s = [1 for i in range(N)]
    r = [0 for i in range(N)]
    r[0] = 1
    for i in range(M):
        x = l[i][0] - 1
        y = l[i][1] - 1
        if r[x] == 1:
            if s[x] == 1:
                r[x] = 0
            r[y] = 1
        s[x] -= 1
        s[y] += 1
    print(sum(r))

main()

Submission status: Accepted

Solution code:
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())
  
pos = [1]+[0]*(N-1)
num = [1]*N

for i in range(M):
  if pos[x[i]-1]==1:
    if num[x[i]-1]==1:
      pos[x[i]-1]=0
      pos[y[i]-1]=1
      num[x[i]-1] -= 1
      num[y[i]-1] += 1
    else:
      pos[y[i]-1]=1
      num[x[i]-1] -= 1
      num[y[i]-1] += 1
  else:
      num[x[i]-1] -= 1
      num[y[i]-1] += 1
  #print(i,pos)
  #print(i,num)
  
print(sum(pos))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
sift = [list(map(int, input().split())) for _ in range(M)]
ball = [0] * N
ball[0] = 1
num_ball = [1] * N
for i in range(M):
    #print(ball)
    a, b = sift[i]
    a -= 1
    b -= 1
    if ball[a] == 1:
        if num_ball[a] > 1:
            ball[b] = 1
            num_ball[a] -= 1
            num_ball[b] += 1
        else:
            ball[b] = 1
            ball[a] = 0
            num_ball[b] += 1
            num_ball[a] -= 1
    else:
        num_ball[a] -= 1
        num_ball[b] += 1
print(sum(ball))

Submission status: Accepted

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

n, m = [int(x) for x in input().split()]
xy = [[int(x) for x in input().split()] for _ in range(m)]

status = [[0, 1] for _ in range(n + 1)]
status[1] = [1, 1] # 赤？、個数

for i in range(m):
    x, y = xy[i]
    if status[x][0] == 1:
        if status[x][1] > 1:
            status[x][1] -= 1
        else:
            status[x] = [0, 0]
        status[y][0] = 1
        status[y][1] += 1
    else:
        status[x][1] -= 1
        status[y][1] += 1

ans = 0

for i in status[1:]:
    if i[0] == 1:
        ans += 1

print(ans)

Submission status: Accepted

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

for _ in range(M):
    x, y = map(int, input().split())
    X.append(x - 1)
    Y.append(y - 1)

red = [0] * N
red[0] = 1
ball = [1] * N

for x, y in zip(X, Y):
    if red[x] == 1 and ball[x] != 0:
        red[y] = 1
        if ball[x] == 1:
            red[x] = 0
        ball[x] -= 1
        ball[y] += 1
    elif ball[x] != 0:
        ball[x] -= 1
        ball[y] += 1

print(sum(red))

Submission status: Accepted

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

ans=0
jagh=[0]*n#あかがはいった事あるかどうか
jagh[0]=1
now=[1]*n#ボールの個数
for i in range(m):
    if jagh[xy[i][0]-1]==1:
        jagh[xy[i][1]-1]=1
        now[xy[i][0]-1]-=1
        if now[xy[i][0]-1]==0:
            jagh[xy[i][0]-1]=0
        now[xy[i][1]-1]+=1
    else:
        now[xy[i][0]-1]-=1
        if now[xy[i][0]-1]==0:
            jagh[xy[i][0]-1]=0
        now[xy[i][1]-1]+=1

#print(jagh,now)
for i in range(n):
    if jagh[i]==1 and 0<now[i]:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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

N, M = MAP()
balls = [[1, 0] for _ in range(N)]  # white, red
balls[0] = [0, 1]

for _ in range(M):
	x, y = MAP()

	w_x, r_x = balls[x-1]
	w_y, r_y = balls[y-1]
	
	if w_x >= 1 and r_x >= 1:
		balls[x-1][0] -= 1
		balls[y-1][1] += 1
	elif w_x == 0:
		balls[x-1][1] -= 1
		balls[y-1][1] += 1
	else:
		balls[x-1][0] -= 1
		balls[y-1][0] += 1

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X, Y = [0]*M, [0]*M
for i in range(M):
  X[i], Y[i] = map(int,input().split())
status = [False]*N; status[0] = True;
ball = [1]*N

for i in range(M):
  ball[X[i]-1] -= 1
  ball[Y[i]-1] += 1
  if status[X[i]-1] == True:
    status[Y[i]-1] = True
    if ball[X[i]-1] == 0:
      status[X[i]-1] = False
      
print(sum(status))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n, m = LI()
    ok = [1] * n
    ans = [0] * n
    ans[0] = 1
    for _ in range(m):
        x, y = LI_()
        ok[x] -= 1
        ok[y] += 1
        if ans[x]:
            ans[y] = 1
        if ok[x] == 0:
            ans[x] = 0
    print(sum(ans))
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
colors = ['R'] + ['W'] * (N-1)
counts = [1]*N

def receive(color, id):
    counts[id] += 1
    if colors[id] == 'E':
        colors[id] = color
    elif color != colors[id]:
        colors[id] = 'M'

def send(id):
    color = colors[id]
    counts[id] -= 1
    if counts[id] == 0:
        colors[id] = 'E'
    return color

for i in range(M):
    x, y = map(int, input().split())
    c = send(x-1)
    receive(c, y-1)

print(sum([1 for c in colors if c == 'R' or c == 'M']))

Submission status: Accepted

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""


def main():
    N, M = map(int, input().split())
    xy = []
    for _ in range(M):
        xy.append(list(map(int, input().split())))
    prob = [False] * N
    prob[0] = True
    ball = [1] * N
    for x, y in xy:
        ball[x-1] = ball[x-1] - 1
        ball[y-1] = ball[y-1] + 1
        if ball[x-1] < 1 and prob[x-1]:
            prob[x-1] = False
            prob[y-1] = True
        elif ball[x-1] >= 1 and prob[x-1]:
            prob[y-1] = True
    print(prob.count(True))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(sum(reds))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
op=[list(map(int,input().split())) for _ in range(M)]
susflag=[0]*N
susflag[0]=1
num=[1]*N
fr=0
to=0
#print(susflag,num)
for i in range(M):
    fr=op[i][0]-1
    to=op[i][1]-1
    num[fr]=num[fr]-1
    num[to]=num[to]+1
    if susflag[fr]==1:
        susflag[to]=1
    if num[fr]==0:
        susflag[fr]=0
    #print(susflag,num)
print(sum(susflag))

Submission status: Accepted

Solution code:
N,M = map(int,input().rstrip().split(" "))
ans = [False for _ in range(N + 1)]
balls = [1 for _ in range(N + 1)]
ans[1] = True
for i in range(M):
  q = list(map(int,input().rstrip().split(" ")))
  if ans[q[0]] == True:
    ans[q[1]] = True
  balls[q[0]] -= 1
  if balls[q[0]] == 0: ans[q[0]] = False
  balls[q[1]] += 1
ansi = 0
for i in range(N + 1):
  if ans[i]: ansi += 1
print(ansi)

Submission status: Accepted

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

x = 0
y = 1

# 0-indexに揃える
for _xy in xy:
    _xy[x] -= 1
    _xy[y] -= 1

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

for i in range(m):
    # ボール移動
    ballCnt[xy[i][x]] -= 1
    ballCnt[xy[i][y]] += 1

    if red[xy[i][x]]:
        red[xy[i][y]] = True

    if ballCnt[xy[i][x]] == 0:
        red[xy[i][x]] = False

# print(red)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
box_num = [1]*N
box_color = [0]*N
box_color[0] = 1   # 0:白 1:赤 2:白or赤

for _ in range(M):
    x, y = map(int, input().split())
    x, y = x-1, y-1
    box_num[x] -= 1
    box_num[y] += 1

    if box_num[y] != 1:
        if box_color[x]==box_color[y]:
            pass
        else:
            box_color[y] = 2
    else:
        box_color[y] = box_color[x]

ans = 0
for n, c in zip(box_num, box_color):
    if n>0:
        if c != 0:
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    n,m = map(int, input().split())
    poss  = [False]*n
    poss[0] = True
    cnts = [int() + 1]*n
    
    for i in range(m):
        #print(cnts)
        xi,yi = map(int, input().split())
        if poss[xi-1]:
            if cnts[xi-1] == 1:
                poss[xi-1] = False
            poss[yi-1] = True

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

        
    
    print(sum(poss))
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

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

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

print(sum(possibilities))

Submission status: Accepted

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

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

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

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

for x, y in xy:
    x -= 1
    y -= 1
    if ans[x] == 1 and balls[x] > 0:
        ans[y] = 1
    if balls[x] > 0:
        balls[x] -= 1
        balls[y] += 1
        if balls[x] == 0:
            ans[x] = 0

print(sum(ans))

Submission status: Accepted

Solution code:
def main():
    boxes, operation_num = map(int, input().split())
    operation = []
    for _ in range(operation_num):
        operation.append(list(map(int, input().split())))
    can_exist_red = [False for _ in range(boxes)]
    ball = [1 for _ in range(boxes)]
    can_exist_red[0] = True
    for op_from, op_to in operation:
        ball[op_to - 1] += 1
        ball[op_from - 1] -= 1
        if can_exist_red[op_from - 1]:
            can_exist_red[op_to - 1] = True
            if ball[op_from - 1] == 0:
                can_exist_red[op_from - 1] = False
    answer = 0
    for c in can_exist_red:
        if c:
            answer += 1
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    ball = [1] * N
    red = [False] * N
    red[0] = True
    for _ in range(M):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        ball[x] -= 1
        ball[y] += 1
        if red[x]:
            red[y] = True
        if ball[x] == 0:
            red[x] = False

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

    print(ans)




if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
d = {i+1:1 for i in range(N)}
isRed = {i+1:False for i in range(N)}

isRed[1] = True
for m in range(M):
  x,y = list(map(int,input().split()))
  d[x] -= 1
  d[y] += 1
  if isRed[x]:
    isRed[y] = True
  if d[x] == 0:
    isRed[x] = False
print(sum(isRed.values()))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
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()
ball = defaultdict(int)
count = defaultdict(int)
ball[1] = 1
for i in range(1,n+1):
    count[i] = 1

for _ in range(m):
    x, y = LI()
    if ball[x] and count[x] > 1:
        ball[y] = 1
        count[x] -= 1
        count[y] += 1
    elif ball[x] and count[x] == 1:
        ball[x] = 0
        ball[y] = 1
        count[x] = 0
        count[y] += 1
    elif not ball[x]:
        count[x] -= 1
        count[y] += 1

ans = 0
for i in range(1,n+1):
    if ball[i]:
        ans += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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