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 bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n, m = LI()
    cnt = [None] + [1 for _ in range(n)]
    exist = [False] + [False for _ in range(n)]
    exist[1] = True

    for _ in range(m):
        x, y = LI()

        cnt[y] += 1
        exist[y] |= exist[x]

        cnt[x] -= 1
        exist[x] &= cnt[x] != 0
    
    ans = sum(exist)
    print(ans)
main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = [True if i == 0 else False for i in range(n)]
k = [1 for i in range(n)]
for i in range(m):
    xy = list(map(int, input().split()))
    if l[xy[0]-1]:
        if k[xy[0]-1] > 1:
            l[xy[1]-1] = True
        else:
            l[xy[1]-1] = True
            l[xy[0]-1] = False
    k[xy[0]-1] -= 1
    k[xy[1]-1] += 1
print(l.count(True))

Submission status: Accepted

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

redball = [0]*n
redball[0] = 1
ball_cnt = [1]*n

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

    if ball_cnt[x] == 1 and redball[x]:
        redball[x] = 0
        redball[y] = 1

    if ball_cnt[x] > 1 and redball[x]:
        redball[y] = 1

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

print(redball.count(1))

Submission status: Accepted

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

redball = [0]*n
redball[0] = 1
ball_cnt = [1]*n

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

    if ball_cnt[x] == 1 and redball[x] == 1:
        redball[x] = 0
        redball[y] = 1

    if ball_cnt[x] > 1 and redball[x] == 1:
        redball[y] = 1

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

print(redball.count(1))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
red = {1, }
l = [1 for _ in range(n)]
for _ in range(m):
    x, y = map(int, input().split())
    l[x - 1] -= 1
    l[y - 1] += 1
    if x in red:
        red.add(y)
        if l[x - 1] == 0:
            red.discard(x)
print(len(red))

Submission status: Accepted

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

Submission status: Accepted

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

dp = [False]*N
dp[0] = True
cnt = [1]*N

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

print(sum(dp))

Submission status: Accepted

Solution code:
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 balls[x-1]:
        balls[x-1] -= 1
        balls[y-1] += 1
        if red[x-1]:
            red[y-1] = 1
            if balls[x-1] == 0:
                red[x-1] = 0
cnt = 0
for i in range(n):
    if red[i] and balls[i]:
        cnt += 1
print(cnt)

Submission status: Accepted

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

D = [0] * N
C = [1] * N
D[0] = 1

for x, y in xy:
  x -= 1
  y -= 1
  if D[x] == 1:
    D[y] = 1
  C[x] -= 1
  C[y] += 1
  if C[x] == 0:
    D[x] = 0

print(sum(D))

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,M=MI()
    L=[0]*N
    L[0]=1
    cnt=[1]*N
    for i in range(M):
        x,y=MI()
        x-=1
        y-=1
        if L[x]==1:
            L[y]=1
            if cnt[x]==1:
                L[x]=0
                flag=0
        cnt[x]-=1
        cnt[y]+=1
                
    print(sum(L))
        

main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
dp=[False]*n
dp2=[1]*n

dp[0]=True

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

for i in range(n):
  if dp[i]==True:
    ans+=1
    
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from math import gcd,pi
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n,m = i_map()
    box = [1 for _ in range(n+1)]
    red = [0 for _ in range(n+1)]
    red[1] = 1

    for i in range(m):
        x,y = i_map()
        box[x] -= 1
        box[y] += 1
        if red[x] == 1:
            red[y] = 1
        if box[x] == 0:
            red[x] = 0

    print(sum(red))

if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
jud = [True] + [False]*(n - 1)
l = [1]*n

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

ans = 0
for i in jud:
    if i:
        ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = [list(map(int,input().split())) for i in range(m)]
x,y = [list(i) for i in zip(*l)]
num = [1] * n
red = ['out'] * n
red[0] = 'in'
for i in range(m):
    if red[x[i]-1] == 'in':
        red[y[i]-1] = 'in'
    num[x[i]-1] -= 1
    num[y[i]-1] += 1
    if num[x[i]-1] == 0:
        red[x[i]-1] = 'out'
print(red.count('in'))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
possible = [0]*n
kosuu = [1]*n
possible[0] = 1
L = []

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

for i in range(m):
    x,y = L[i][0],L[i][1]
    x,y = x-1,y-1
    if kosuu[x] >=2 and possible[x] == 1:
        possible[y] = 1
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 1:
        possible[x] = 0
        possible[y] = 1
        kosuu[x] = 0
        kosuu[y] +=1
    elif kosuu[x] >=2 and possible[x] == 0:
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 0:
        kosuu[x] = 0
        kosuu[y] +=1
print(sum(possible))

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()



def main():
	n,m=MI()
	A=[1]*(n+1)
	ans=[0]*(n+1)
	ans[1]=1

	for _ in range(m):
		x,y=MI()
		if ans[x]==1:
			ans[y]=1
		if A[x]==1 and ans[x]==1:
			ans[x]=0
		A[x]-=1
		A[y]+=1
		#print(A,ans)

	print(sum(ans))









if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
balls=[1 for _ in range(N)]
answers=[0 for _ in range(N)]
answers[0]=1
for _ in range(M):
    x,y=map(int,input().split())
    balls[x-1]-=1
    balls[y-1]+=1
    if balls[x-1]>=1 and answers[x-1]==1:
        answers[y-1]=1
    elif balls[x-1]==0 and answers[x-1]==1:
        answers[x-1]=0
        answers[y-1]=1
print(sum(answers))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
kouho = {}
kouho[1]=True
Nlist = [1]*(N+1)
ans = 0
for i in range(M):
    x, y = map(int, input().split())
    if x in kouho:
        kouho[y] = True
        Nlist[x] -= 1
        Nlist[y] += 1
    else:
        Nlist[x] -= 1
        Nlist[y] += 1
    if Nlist[x]==0:
        if x in kouho:
            del kouho[x]

for i in range(1, N+1):
    if Nlist[i] == 0:
        continue
    if i in kouho:
        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)]

ball_count = [1]*N

red_ball_cand = [0]*N
red_ball_cand[0] = 1

for i in range(M):
    x,y = XY[i]
    x,y = x-1,y-1
    if 0 < red_ball_cand[x]:
        red_ball_cand[y] = 1
        
    ball_count[x] -= 1
    ball_count[y] += 1
    
    if ball_count[x] == 0:
        red_ball_cand[x] = 0
print(sum(red_ball_cand))

Submission status: Accepted

Solution code:
N, M, *xy = map(int, open(0).read().split())
boxes = [1] * N
is_red = [0] * N
is_red[0] = 1

for x, y in zip(*[iter(xy)] * 2):
    x -= 1
    y -= 1
    boxes[x] -= 1
    boxes[y] += 1
    if is_red[x]:
        if boxes[x] == 0:
            is_red[x] = 0
        is_red[y] = 1
print(sum(is_red))

Submission status: Accepted

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

red = [False] * n
ball = [1] * n

red[0] = True
for _ in range(m):
    x, y = map(lambda x: int(x) - 1, input().split())
    if red[x]:
        red[y] = True
        if ball[x] == 1:
            red[x] = False
    ball[y] += 1
    ball[x] -= 1
print(len(list(filter(lambda x: x, red))))

Submission status: Accepted

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

B = [1 for i in range(N)]
R = [0 for i in range(N)]
R[0] = 1 #赤いボールが入っている可能性があるなら１、無いなら０
for i in range(M):
  src = Z[i][0]-1
  dst = Z[i][1]-1
  if B[src] > 1 and R[src] == 1:
    R[dst] = 1
  if B[src] == 1 and R[src] == 1:
    R[src] = 0 
    R[dst] = 1
  B[src] -= 1
  B[dst] += 1
print(sum(R))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

for x,y in xy:
    ball[x] -= 1
    ball[y] += 1
    if red[x] == 1:
        red[y] = 1
        if ball[x] == 0:
            red[x] = 0
print(sum(red))

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
redl = [False]*(n+1)
redl[1] = True
cnts = [1]*(n+1)
cnts[0] = 1

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

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

Submission status: Accepted

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

check = [1]+[0]*n
numlis = [1]*(n+1)

for i in range(m):
    x, y = map(int, input().split())
    if numlis[x-1] > 1 and check[x-1] == 1:
        check[y-1] = 1
        numlis[x-1] -= 1
        numlis[y-1] += 1
    elif numlis[x-1] == 1 and check[x-1] == 1:
        check[y-1] = 1
        check[x-1] = 0
        numlis[x-1] -= 1
        numlis[y-1] += 1
    else:
        numlis[x-1] -= 1
        numlis[y-1] += 1
    

print(sum(check))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, m = map(int, input().split())
    balls = [1] * (n + 1)
    posib = [0] * (n + 1)
    posib[1] = 1
    m = map(int, read().split())
    ab = zip(m, m)
    for x, y in ab:
        if posib[x]:
            posib[y] = 1
        balls[y] += 1
        balls[x] -= 1
        if not balls[x]:
            posib[x] = 0
    r = sum(posib)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

    ball  = [0, 1] + [1 for i in range(N - 1)]
    rflag = [0, 1] + [0 for i in range(N - 1)] 

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

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

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

        if ball[x] == 0:
            rflag[x] = 0

    ans = sum(rflag[1:])
    print(ans)
    
if __name__ == "__main__":
    main3()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N, M = [int(x) for x in input().split()]
    XY = [[int(x) for x in input().split()] for _ in range(M)]
    ans = [0] * (N + 1)
    ans[1] = 1
    cnt = [1] * (N + 1)
    for x, y in XY:
        if ans[x] == 1:
            ans[y] = 1
        if cnt[x] == 1:
            ans[x] = 0
        cnt[x] -= 1
        cnt[y] += 1

    print(sum(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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:
def resolve():
    n, m = map(int, input().split())

    num_balls = [1] * n
    red_prov = [False] * n
    red_prov[0] = True

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

        # リストnum_ballsを更新
        num_balls[x] -= 1
        num_balls[y] += 1

        # リストred_provを更新
        if red_prov[x]:
            red_prov[y] = True

        if num_balls[x] == 0:
            red_prov[x] = False

    print(sum(red_prov))
    
resolve()

Submission status: Accepted

Solution code:
def main():
    N, M = list(map(int, input().split()))
    proc_list = [list(map(lambda z: int(z) - 1, input().split())) for _ in range(M)]
    counter = [1] * N
    red_flag = [0] * N
    red_flag[0] = 1
    for from_box, to_box in proc_list:
        if red_flag[from_box] == 1:
            if counter[from_box] == 1:
                red_flag[from_box] = 0
            red_flag[to_box] = 1
        counter[from_box] -= 1
        counter[to_box] += 1
    print(sum(red_flag))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
b = [["W", 1] for _ in range(n)]
b[0] = ["R", 1]

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

    b[x-1][1] -= 1
    if b[x-1][0] == "R":
        b[y-1] = ["R", b[y-1][1]+1]

        if b[x-1][1] == 0:
            b[x-1][0] = "W"
    else:
        b[y-1][1] += 1

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

print(cnt)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
x = [0]
y = [0]
memo = [1] * (N + 1) #各箱の中のボールの数
flag = [0] * (N + 1) #赤ボールがある可能性
for _ in range(M):
    _x, _y = map(int, input().split())
    x.append(_x)
    y.append(_y)

flag[1] = 1
for i in range(1, M + 1):
    if memo[x[i]] == 0: #移動するボール無し
        continue
    memo[x[i]] -= 1
    memo[y[i]] += 1
    if flag[x[i]] == 1:
        flag[y[i]] = 1
    if memo[x[i]] == 0:
        flag[x[i]] = 0

ans = 0
for i in range(1, N + 1):
    if memo[i] <= 0:
        continue
    if flag[i] == 1:
        ans += 1
#print(memo)
#print(flag)
print(ans)

Submission status: Accepted

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

# from numba import njit

# input = stdin.readline

# @njit
def solve(n,m,queries):
  redInBox   = [False] * (n+1)
  amountOfBalls = [1] * (n+1)
  redInBox[1] = True
  
  for query in queries:
    x,y = query
    if redInBox[x]:
      redInBox[y] = True
      amountOfBalls[x] -= 1
      amountOfBalls[y] += 1
      if amountOfBalls[x] == 0:
        redInBox[x] = False
    else:
      amountOfBalls[x] -= 1
      amountOfBalls[y] += 1
 
  return sum(redInBox)



def main():
  N,M = map(int,input().split())
  queries = [list(map(int,input().split())) for _ in range(M)]
  print(solve(N,M,queries))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
red = [0 for i in range(N)]
cnt = [1 for i in range(N)]
red[0] = 1
for i in range(M):
    x,y = map(int, input().split())
    x -= 1
    y -= 1
    if red[x] > 0:
        if cnt[x] >= 2:
            cnt[x] -= 1
            cnt[y] += 1
            red[y] = 1
        else:
            cnt[x] -= 1
            cnt[y] += 1
            red[x] = 0
            red[y] = 1
    else:
        cnt[x] -= 1
        cnt[y] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n,m=map(int, input().split())
ball=[1]*n
red=[False]*n
red[0]=True
for i 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

print(red.count(True))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
prob = [False] * (n + 1)
exact = [False] * (n + 1)
prob[1] = exact[1] = True
num_in = [1] * (n + 1)
for _ in range(m):
    x, y = map(int, input().split())
    if num_in[x] == 1 and exact[x]:
        exact[y] = True
        exact[x] = False
        prob[x] = False
        prob[y] = True
    elif num_in[x] > 1 and exact[x]:
        exact[x] = False
        prob[y] = True
    elif prob[x]:
        if num_in[x] == 1:
            prob[x] = False
        prob[y] = True
    num_in[y] += 1
    num_in[x] -= 1
print(prob.count(True))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
num=[1]*n
aka=[0]*n
aka[0]=1
for _ in range(m):
  x,y=map(int,input().split())
  if num[x-1]>1 and aka[x-1]==1:
    num[x-1]+=-1
    aka[x-1]=1
    aka[y-1]=1
    num[y-1]+=1
  elif num[x-1]==1 and aka[x-1]==1:
    num[x-1]=0
    aka[x-1]=0
    aka[y-1]=1
    num[y-1]+=1
  elif aka[x-1]==0:
    num[x-1]+=-1
    num[y-1]+=1
print(sum(aka))

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
for i in range(n):
    if flag[i] == True and balls[i]>=1:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import sys

input=sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()


def II(): return int(input())


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


def MF(): return map(float,input().split())


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


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


def TI(): return tuple(map(int,input().split()))


# rstrip().decode('utf-8')


def main():
	n,m=MI()
	
	A=[1]*(n+1)
	B=[0]*(n+1)
	B[1]=1
	
	for _ in range(m):
		x,y=MI()
		A[x]-=1
		A[y]+=1
		
		if B[x]==1:
			B[y]=1
		if A[x]==0:
			B[x]=0

	print(sum(B))
	
	
	
	
	
	
	
	
	
	








	
	
	

if __name__=="__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

p=[1]*n
c=['w']*n
c[0]='r'


def ope(x,y):
    if c[x-1]=='r':
        c[y-1]='r'
    p[x-1] -= 1
    p[y-1] += 1
    if p[x-1]==0:
        c[x-1]='w'

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


print(c.count('r'))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc002/tasks/agc002_b
import sys
input = sys.stdin.buffer.readline

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

count = [1] * (n + 1)  # 1-indexed
have_red = [0] * (n + 1)  # 1-indexed
have_red[1] = 1
for _ in range(m):
    x, y = [int(i) for i in input().split()]
    if have_red[x]:
        have_red[y] = 1
    count[x] -= 1
    count[y] += 1
    if count[x] == 0:
        have_red[x] = 0

ans = sum(have_red)

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Z = [list(map(int,input().split())) for i in range(M)]
kosuu = [1 for _ in range(N)]
red = [False for _ in range(N)]
red[0] = True

for i in range(M):
  x = Z[i][0] - 1
  y = Z[i][1] - 1
  if (kosuu[x] != 0 and red[x] == True):
    red[y] = True
  if (kosuu[x] == 1 and red[x] == True):
    red[x] = False
  kosuu[x] -= 1
  kosuu[y] += 1

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

print(ans)

Submission status: Accepted

Solution code:
def solve():
  N, M = map(int, input().split())
  lis = [False]*N
  cnt = [1]*N
  lis[0] = True
  for i in range(M):
    x,y = map(int, input().split())
    x -= 1
    y -= 1
    if lis[x]==True:
      lis[y]=True
      if cnt[x]==1:
        lis[x]=False
    cnt[x] -= 1
    cnt[y] += 1
  ans = lis.count(True)
  return ans
print(solve())

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,m = readInts()
red = [False] * n
red[0] = True
box = [1] * n
for i in range(m):
    x,y = map(lambda x:int(x)-1, input().split())
    box[x] -= 1
    box[y] += 1
    if red[x]:
        red[y] = True
    if box[x] == 0:
        red[x] = False
cnt = 0
for i in range(n):
    if red[i]:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
# 2020/08/09
# AtCoder Grand Contest 002 - A

# Input
n, m = map(int,input().split())
rlist = [0] * (n+1)
wlist = [1] * (n+1) 
rlist[1] = 1
wlist[1] = 0

# Calc
for i in range(m):
    x, y = map(int,input().split())
    if rlist[x] >= 1:
        rlist[y] = rlist[y] + 1
        # if rlist[x] == 1 and wlist[x] == 0:
        if wlist[x] == 0:
            rlist[x] = rlist[x] - 1
        else:
            wlist[x] = wlist[x] - 1
    else:
        wlist[y] = wlist[y] + 1
        wlist[x] = wlist[x] - 1

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

# Output
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(sum(aka))

Submission status: Accepted

Solution code:
N, M, *xy = map(int, open(0).read().split())
boxes = [{"count": 1, "red": False} for _ in range(N)]
boxes[0]["red"] = True

for x, y in zip(*[iter(xy)] * 2):
    x -= 1
    y -= 1
    if boxes[x]["red"]:
        boxes[y]["red"] = True
        if boxes[x]["count"] == 1:
            boxes[x]["red"] = False
    boxes[x]["count"] -= 1
    boxes[y]["count"] += 1
print(sum(b["red"] for b in boxes))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
ball_cnts = [1]*(n+1)
ball_cnts[0] = 0
red_flag = [False]*(n+1)
red_flag[1] = True

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

ans = 0
for v in red_flag:
    if v: ans += 1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

in_group = set()
in_group.add(1)

boll_num = {}
for i in range(n):
    boll_num[i+1] = 1


for i in range(m):
    x,y = map(int,input().split())
    if(x in in_group):
        in_group.add(y)
        if(boll_num[x] == 1):
            in_group.remove(x)
    
    boll_num[x] = boll_num.setdefault(x,0) - 1
    boll_num[y] = boll_num.setdefault(y,0) + 1

print(len(in_group))

Submission status: Accepted

Solution code:
def solve():
    N, M = map(int, input().split())
    contains_redball = [False] * N
    contains_redball[0] = True
    balls_cnt = [1] * N
    for _ in range(M):
        x, y = map(int, input().split())
        x, y = x - 1, y - 1
        balls_cnt[x] -= 1
        balls_cnt[y] += 1
        if contains_redball[x]:
            contains_redball[y] = True
        if balls_cnt[x] == 0:
            contains_redball[x] = False
    print(sum(contains_redball))

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

q = [1]*N

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

print(sum([1 for i in range(N) if q[i]>0 and red[i]]))

Submission status: Accepted

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

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

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

    H[Y]|=H[X]

    Z[X]-=1
    Z[Y]+=1

    H[X]&=(Z[X]!=0)
print(sum(H))