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:
# -*- coding: utf-8 -*-


def main():
    n, m = map(int, input().split())
    num = [1 for _ in range(n)]
    red = [False for _ in range(n)]
    red[0] = True

    # Key Insight
    # 問題を言い換える
    # num[i] := i番目のコップに入っている水の量
    # red[i] := i番目のコップに赤い水が入っているか
    for i in range(m):
        xi, yi = map(int, input().split())
        xi -= 1
        yi -= 1

        if red[xi]:
            red[yi] = True

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

        # 移動前のコップに入っていたのが赤い水だけだったとき
        if num[xi] == 0:
            red[xi] = False

    print(sum(red))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# AGC002B - Box and Ball
import sys
input = sys.stdin.readline

def main():
    N, M = tuple(map(int, input().split()))
    X = tuple(tuple(map(int, input().split())) for _ in range(M))
    R = [0] * (N + 1)  # red ball may be in box i
    R[1] = 1
    C = [1] * (N + 1)  # number of balls in box i (count)
    for x, y in X:
        if R[x]:
            R[y] = 1
        C[x] -= 1
        C[y] += 1
        if C[x] == 0:
            R[x] = 0
    ans = sum(R[1:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# AGC002B - Box and Ball
def main():
    N, M = tuple(map(int, input().split()))
    X = tuple(tuple(map(int, input().split())) for _ in range(M))
    R = [0] * (N + 1)  # red ball may be in box i
    R[1] = 1
    C = [1] * (N + 1)  # number of balls in box i (count)
    for x, y in X:
        if R[x]:
            R[y] = 1
        C[x] -= 1
        C[y] += 1
        if C[x] == 0:
            R[x] = 0
    ans = sum(R[1:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# AGC002B - Box and Ball
import sys
input = sys.stdin.readline

def main():
    N, M = tuple(map(int, input().split()))
    X = tuple(tuple(map(int, input().split())) for _ in range(M))
    R = [0] * (N + 1)  # red ball may be in box i
    R[1] = 1
    C = [1] * (N + 1)  # number of balls in box i (count)
    for x, y in X:
        R[y] = 1 if R[x] or R[y] else 0
        C[x] -= 1
        C[y] += 1
        R[x] = 0 if C[x] == 0 else R[x]
    ans = sum(R[1:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def sol():
    N, M = map(int, input().split())

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

    for _ in range(M):
        fr ,to = map(int, input().split())
        fr -= 1
        to -= 1
        if hasRed[fr]:
            hasRed[to] = True
        ball[fr] -= 1
        if ball[fr] == 0:
            hasRed[fr] = False
        ball[to] += 1

    ans = 0
    for has, count in zip(hasRed, ball):
        if has and count > 0:
            ans += 1

    print(ans)


sol()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from itertools import *
from copy import *

N, M = map(int, input().split())
num, red = [1] * (N+1), [0]+[1]+[0]*(N-1)
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] = 0
print(sum(red))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

# 各時点でのボールの合計個数、赤の最大個数（0 or 1）を持つ

ball = [None] + [1] * N
red = [None] + [1] + [0] * (N-1)

for x,y in XY:
    if red[x] == 0:
        # 必ず白をうつす
        ball[x] -= 1
        ball[y] += 1
        continue
    if red[x] == 1:
        if ball[x] == 1:
            # 必ず赤をうつす
            red[x] = 0
            ball[x] -= 1
            red[y] = 1
            ball[y] += 1
        else:
            # どちらかをうつす
            ball[x] -= 1
            ball[y] += 1
            red[y] = 1

answer = sum(red[1:])
print(answer)

ball

red

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split());a=[0,1]+[0]*n;b=[1]*-~n
for i,j in zip(l[1::2],l[2::2]):a[j]|=a[i];b[i]-=1;b[j]+=1;a[i]&=(b[i]>0)
print(sum(a))

Submission status: Accepted

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

for inp in range(m):
    a,b=map(int,input().split())
    a-=1
    b-=1
    balls[a]-=1
    balls[b]+=1
    if pro[a]:
        pro[b]=True
    if balls[a]==0:
        pro[a]=False


#print(pro)
print(pro.count(True))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
xy=[list(map(lambda x:int(x)-1,input().split())) for _ in [0]*m]

cnt=[1 for _ in [0]*n]
red=[False for _ in [0]*n]
red[0]=1

for x,y in xy:
    cnt[x]-=1
    cnt[y]+=1
    if red[x]==True:
        red[y]=True
    if cnt[x]==0:
        red[x]=False
print(red.count(True))

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
XY = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

counts = np.ones(N, dtype=int)
possible = np.zeros(N, dtype=bool)
possible[0] = 1

for x, y in XY:
    x -= 1
    y -= 1
    possible[y] |= possible[x]
    counts[x] -= 1
    counts[y] += 1
    if counts[x] == 0:
        possible[x] = False

ans = possible.sum()
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
sum=[1 for i in range(n)]
red=[0 for i in range(n)]
red[0]=1
for i in range(m):
  sum[l[i][0]-1]-=1
  sum[l[i][1]-1]+=1
  if red[l[i][0]-1]==1:
    red[l[i][1]-1]=1
  if sum[l[i][0]-1]==0:
    red[l[i][0]-1]=0
ct=0
for i in range(n):
  if sum[i]!=0 and red[i]==1:
    ct+=1
print(ct)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math


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

red = [False] * (N + 1)
nums = [1] * (N+1)
red[1] = True


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

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(m)]
l=[1]*n
red=set([0])
for i in a:
    l[i[0]-1]-=1
    l[i[1]-1]+=1
    if i[0]-1 in red:
        red.add(i[1]-1)
        if l[i[0]-1]==0:red.remove(i[0]-1)
print(len(red))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
XY = [list(map(int,input().split())) for _ in [0]*M]
R = [False]*N
R[0] = True
B = [1]*N
for x,y in XY:
    x-=1;y-=1
    B[y] += 1
    B[x] -= 1
    R[y] = R[y] or R[x]
    R[x] = R[x] and B[x]>0
print(sum(R))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
lst = [[0, 0], [1, 0]] + [[0, 1] for _ in range(N)]

for _ in range(M):
    x, y = map(int, input().split())
    if lst[x][0] == 0: #赤いボールがないとき
        lst[x][1] -= 1 #白いボールを移動させる
        lst[y][1] += 1
    if lst[x][0] == 1: #赤いボールがあるとき
        if lst[x][1] == 0: #赤いボールしかないとき
            #赤いボールを移動させる
            lst[x][0] -= 1
            if lst[y][0] == 0:
                lst[y][0] = 1
            else:
                lst[y][1] += 1
        else:
            #全体のボールを減らして、x, yともに赤が入っていることとする
            lst[x][1] -= 1
            if lst[y][0] == 0:
                lst[y][0] = 1
            else:
                lst[y][1] += 1
count = 0
for i in lst:
    if i[0] == 1:
        count += 1
print (count)
# print (lst)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[1]*(n+1)
r=set([1])
for i in range(m):
    x,y=map(int,input().split())
    if x in r:
        r.add(y)
    l[x]-=1
    l[y]+=1
    if l[x]==0 and x in r:
        r.remove(x)
print(len(r))

Submission status: Accepted

Solution code:
N, M = [int(_) for _ in raw_input().split(" ")]

ret = [1] * N
color = [False]*N
color[0] = True
for i in range(M):
  _from, _to = [int(_) for _ in raw_input().split(" ")]
  ret[_from-1] -= 1
  ret[_to-1] += 1
  if color[_from-1] == True:
    color[_to-1] = True
  if ret[_from-1] == 0:
    color[_from-1] = False
  #print ret
  #print color
print color.count(True)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
flg=[0]*(N+1)
flg[1]=1
flg2=[1]*(N+1)
for _ in range(M):
    xy=list(map(int,input().split()))
    if flg[xy[0]]==1:
        flg[xy[1]]=1
    flg2[xy[1]]+=1
    flg2[xy[0]]-=1
    if flg2[xy[0]]==0:
        flg[xy[0]]=0
print(sum(map(lambda x:x>0,flg)))

Submission status: Accepted

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

L = [1 for i in range(N)]

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

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

print(sum(R))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, m = li()
balls = [1]*n
canex = [0]*n

canex[0] = 1

for _ in range(m):
    x, y = li_()
    balls[x] -= 1
    balls[y] += 1

    if balls[x] > 0 and canex[x]:
        canex[y] = 1

    elif balls[x] == 0 and canex[x]:
        canex[y] = 1
        canex[x] = 0

print(sum(canex))

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    cnt = [1] * (N+1)
    cand = set([1])
    for _ in range(M):
        x, y = map(int, input().split())
        cnt[x] -= 1
        cnt[y] += 1
        if x in cand:
            cand.add(y)
            if cnt[x] == 0:
                cand.remove(x)
    print(len(cand))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
xy = [[int(_) for _ in input().split()] for _ in range(M)]
red = [False]*(N+1)
red[1] = True
ballnum = [1]*(N+1)

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

print(red.count(True))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ballcount = []
for i in range(0, N):
	ballcount.append(1)
redlist = [True]
for i in range(1, N):
	redlist.append(False)
for i in range(0, M):
	x, y = map(int, input().split())
	if redlist[x-1]: redlist[y-1] = True
	ballcount[x-1] -= 1
	ballcount[y-1] += 1
	if ballcount[x-1] == 0: redlist[x-1] = False
cnt = 0
for i in range(0, N):
	if redlist[i]: cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = [int(it) for it in input().split()]
li = [ [int(it)-1 for it in input().split()] for i in range(M)]

#1:red candi
k0 = [0]*N
c0 = [1]*N

k0[0] = 1


for a,b in li:
  ka,kb,ca,cb = k0[a],k0[b],c0[a],c0[b]
  ga,gb=ka,kb
  if (ca==1 and ka==1):
    ga,gb=0,1
  if (ca>1  and ka==1):
    ga,gb=1,1
  k0[a],k0[b]=ga,gb

  c0[a]-=1
  c0[b]+=1
  
print ( sum(k0) )

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
op = []
for _ in range(M):
    x, y = map(int, input().split())
    op.append((x, y))
box = {i: 1 for i in range(1, N+1)}
possible = {i: False for i in range(1, N+1)}
possible[1] = True
for x, y in op:
    box[x] -= 1
    box[y] += 1
    if possible[x]:
        possible[y] = True
    if box[x] == 0:
        possible[x] = False
print(sum(possible.values()))

Submission status: Accepted

Solution code:
n, m = [int(v) for v in input().split()]
a_s = [[int(v) - 1 for v in input().split()] for _ in range(m)] #0起点に変換

ls = [[1, 0] for _ in range(n)]
ls[0][1] = 1

for a in a_s:
  x, y = a
  #玉移動
  #print(ls, x, y)
  ls[x][0] -= 1
  ls[y][0] += 1
  #移動元が赤箱なら先も赤箱にする
  if ls[x][1] == 1:
    ls[y][1] = 1
  #移動元が空になったら白箱にする
  if ls[x][0] == 0:
    ls[x][1] = 0
    
print(sum([e[1] for e in ls]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
xy=[list(map(int,input().split())) for _ in [0]*M]
n = [1]*N
r = [0]*N
r[0] = 1
for x,y in xy:
    x-=1;y-=1
    n[x] -= 1
    n[y] += 1
    r[y] = r[y] or r[x]
    if n[x] == 0 : r[x] = 0
print(sum(r))

Submission status: Accepted

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

Submission status: Accepted

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

red = set([1])
n_ball = [1]*(N+1)

for x,y in XY:
  n_ball[x] -= 1
  n_ball[y] += 1
  if x in red:
    red.add(y)
    if n_ball[x]==0:
      red.remove(x)
print(len(red))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N,M = (int(x) for x in input().split())
ball = [1 for x in range(N)]
red = [1] + [0 for x in range(N-1)]

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

print(N - sorted(red).index(1))

Submission status: Accepted

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

c = [1 for i in range(n)]
p = [0 for i in range(n)]
p[0]=1

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

print(p.count(1))

Submission status: Accepted

Solution code:
#AGC002B 12:45:00-12:47:30-
N, M = map(int, input().split())
moveList = [list(map(int, input().split())) for i in range(M)]
redFlag = [False for i in range(N)]
redFlag[0] = True
nums = [1 for i in range(N)]

for m in moveList:
    x, y = m[0]-1, m[1]-1
    
    nums[x] -= 1
    nums[y] += 1
    if redFlag[x]: redFlag[y] = True
    if (nums[x] == 0): redFlag[x] = False
    
print(sum(redFlag))

Submission status: Accepted

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


def solve(N: int, M: int, x: "List[int]", y: "List[int]"):
    balls = [1 for i in range(N)]
    red = [False for i in range(N)]
    red[0] = True

    for i in range(M):
        if red[x[i]]:
            if balls[x[i]] == 1:
                red[x[i]] = False
            red[y[i]] = True
        balls[x[i]] -= 1
        balls[y[i]] += 1
    
    print(sum(red))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    x = [int()] * (M)  # type: "List[int]" 
    y = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        x[i] = int(next(tokens))-1
        y[i] = int(next(tokens))-1
    solve(N, M, x, y)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 入力
N, M = map(int, input().split())
x, y = zip(*(map(int, input().split()) for _ in range(M)))

# シミュレーションにより、赤いボールが入っている可能性のある箱を列挙
c = [1 for _ in range(N + 1)]
r = [False for _ in range(N + 1)]
r[1] = True
for a, b in zip(x, y):
    c[b] += 1
    r[b] |= r[a]
    c[a] -= 1
    r[a] &= c[a] >= 1
ans = sum(r)

# 出力
print(ans)

Submission status: Accepted

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

for i in range(m):
    x,y = map(int, input().split())
    tama[x-1] -= 1
    tama[y-1] += 1
    red[y-1] = red[y-1] or red[x-1]
    if tama[x-1] == 0:
        red[x-1] = False
print(sum(red))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
red = [False for i in range(N+1)]
red[1] = True
box = [1 for i in range(N+1)]

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = []
for i in range(M):
  L.append(list(map(int,input().split())))
ball = [1]*N
aka = [False]*N
aka[0] = True
for i in range(M):
  if aka[L[i][0]-1] == True:
    aka[L[i][1]-1] = True
  ball[L[i][0]-1] -= 1
  if ball[L[i][0]-1] == 0:
    aka[L[i][0]-1] = False
  ball[L[i][1]-1] += 1
ans = 0
for i in range(N):
  if aka[i] == True:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
boxs=[1]*n
TF=[1]+[0]*(n-1)
for i in range(m):
    a,b=map(int,input().split())
    if boxs[a-1]==0:
        continue
    else:
        boxs[a-1]-=1
        boxs[b-1]+=1
    if TF[a-1]==1 and boxs[a-1]==0:
        TF[a-1]=0
        TF[b-1]=1
    elif TF[a-1]==1:
        TF[b-1]=1
#print(boxs)
print(sum(TF))

Submission status: Accepted

Solution code:
n, m   = map(int, input().split())
has    = [False for _ in range(n)]
has[0] = True  
count  = [1     for _ in range(n)]

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from collections import Counter

def main():
    N, M = LI()
    XY = []
    for _ in range(M):
        XY.append(LI_())
    balls = [1] * N
    exp = set([0])
    for x, y in XY:
        balls[x] -= 1
        balls[y] += 1
        if x in exp:
            exp.add(y)
            if balls[x] == 0:
                exp.remove(x)
    ans = len(exp)
    return ans

print(main())

Submission status: Accepted

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

Submission status: Accepted

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

print(sum(1 for r in red if r))

Submission status: Accepted

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

ans = 0
for t in boolball:
    if t:
        ans += 1

print(ans)

Submission status: Accepted

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

red = [False] * N
box = [1] * N
red[0] = True

for i in range(M):
    x, y = items[i]
    x, y = x - 1, y - 1

    if box[x] and red[x]:
        red[y] = True

    if box[x] == 1 and red[x]:
        red[x] = False

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

print(red.count(True))

Submission status: Accepted

Solution code:
a,b = map(int, input().split())
n = [1]*a
r = [0]*a
r[0] = 1

for i in range(b):
    ta,tb = map(int, input().split())
    ta-=1
    tb-=1
    if r[ta] == 1:
        r[tb] = 1
    n[ta] -=1
    n[tb] +=1
    if n[ta] ==0:
        r[ta] = 0
print(sum(r))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=list(map(int,input().split()))
W=[list(map(lambda x:int(x)-1,input().split())) for _ in range(M)]
BOX=[[1,1]]
BOX.extend([[1,0] for _ in range(N)])
for i in range(M):
    BOX[W[i][0]][0]=BOX[W[i][0]][0]-1
    BOX[W[i][1]][0]=BOX[W[i][1]][0]+1
    if(BOX[W[i][0]][1]==1):
        BOX[W[i][1]][1]=1
        if(BOX[W[i][0]][0]==0):
            BOX[W[i][0]][1]=0
print(sum(list(map(lambda x:x[:][1],BOX))))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = [0 for i in range(M)]
y = [0 for j in range(M)]
for k in range(M):
    x[k],y[k] = map(int,input().split())
    x[k] -= 1
    y[k] -= 1

ball = [1 for i in range(N)]
red = [0 for j in range(N)]
red[0] = 1

for i in range(M):
    ball[x[i]] -= 1
    ball[y[i]] += 1
    if red[x[i]] == 1:
        red[y[i]] = 1
    if ball[x[i]] == 0:
        red[x[i]] = 0

print(sum(red))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
XY=[[int(j)for j in input().split()]for _ in range(M)]
cnt=[1]*(N+1)
ans=[False]*(N+1)
ans[1]=True
for x,y in XY: 
  cnt[x]-=1
  cnt[y]+=1
  if ans[x] and not ans[y]: 
    ans[y]=True
  if ans[x] and cnt[x]==0:
    ans[x]=False
print(sum(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, m = LI()

    B = [1] * n
    R = [0] * n
    R[0] = 1

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

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

        # 赤いボールが移動したかも？
        if R[x]:
            R[y] = 1

        # 箱が空なので赤いボールは絶対入ってない！
        if not B[x]:
            R[x] = 0

    return sum(R)


print(main())

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, M = map(int, input().split())
box = [1]*N
red = [False]*N
red[0] = True


def update(fr, to):
    if box[fr] == 1:
        red[to] = red[fr] | red[to]
        box[fr] -= 1
        red[fr] = False
        box[to] += 1
    else:
        red[to] = red[fr] | red[to]
        box[fr] -= 1
        box[to] += 1


for _ in range(M):
    a, b = map(lambda x: int(x)-1, input().split())
    update(a, b)

ans = sum(red)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


D = defaultdict(int)
d = defaultdict(lambda: 1)
D[1] = 1

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

for i in range(M):
	x, y = list(map(int, input().split()))
	if D[x] == 1:
		if d[x] == 1:
			D[x] = 0
		D[y] = 1

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

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

print(s)