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())
a = [[int(i) for i in input().split()] for i in range(m)]
p=["f"]*n
p[0]="t"
q=[1]*n
for i in range(0,m):
  q[a[i][0]-1]-=1
  q[a[i][1]-1]+=1
  if p[a[i][0]-1]=="t":
    p[a[i][1]-1]="t"
    if q[a[i][0]-1]==0:
      p[a[i][0]-1]="f"
print(p.count("t"))

Submission status: Accepted

Solution code:
import sys
N, M = map(int, input().split())
P = [[int(v) - 1 for v in sys.stdin.readline().split()] for _ in range(M)]

L = [(1, True)] + ([(1, False)] * (N - 1))

for x, y in P:
    ac, ad = L[x]
    bc, bd = L[y]
    ac -= 1
    bc += 1
    if ad:
        bd = True
    if ac == 0:
        ad = False
    L[x] = (ac, ad)
    L[y] = (bc, bd)

print(sum([f for _, f in L]))

Submission status: Accepted

Solution code:
#import math
#import copy
#import sys
#import bisect
#input = sys.stdin.readline
mod=10**9+7
def factorial(x): #階乗計算
    n=1
    for i in range(x):
        n=(n*(i+1))%mod
    return n

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
def lcm(a, b):
    return((a*b)//gcd(a,b))
def div(x): #約数配列
    div=[1]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        if(x%check==0):
            div.append(check)
            while(x%check==0):
                x=x//check
        check+=1
    if x != 1:
      div.append(x)
    return div
def div2(x): #素因数分解配列
    div2=[]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div2.append(check)
            x/=check
        check+=1
    if x != 1:
      div2.append(x)
    return div2

def main():
    # x,y = map(int,input().split())           x=1,y=2
    # a = input().split()                      a=['1','2','3',...,'n']
    # a = list(map(int,input().split()))       a=[1,2,3,4,5,...,n]
    # li = input().split('T')	              FFFTFTTFF => li=['FFF', 'F', '', 'FF']
    # 複数行の「1 2 3 4」型の入力を一配列に
    # x = sorted([list(map(int, input().split())) for _ in range(n)])
    # ソート x.sort(key=lambda y:y[1])
    # [chr(i) for i in range(97, 97+26)] 英語小文字リスト
    # [chr(i) for i in range(65, 65+26)] 英語大文字リスト atcoder
    # ord(c) chr(2) alphabet <=> number
    n,m = map(int,input().split())
    p=[0 for _ in range(n)]
    box=[1 for _ in range(n)]
    p[0]=1
    for _ in range(m):
        a,b=map(int,input().split())
        if p[a-1]==1 and box[a-1]==1:
            p[b-1]=1
            p[a-1]=0
            box[a-1]-=1
            box[b-1]+=1
        elif p[a-1]==1 and box[a-1]!=1:
            p[b-1]=1
            p[a-1]=1
            box[a-1]-=1
            box[b-1]+=1
        else:
            box[a-1]-=1
            box[b-1]+=1
    print(sum(p))
main()

Submission status: Accepted

Solution code:
from sys import stdin
N,M = [int(x) for x in stdin.readline().rstrip().split()]
box = [1]*(N+1)
red = [False]*(N+1)
red[1] = True
for _ in range(M):
    x,y = [int(x) for x in stdin.readline().rstrip().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 red:
    if i:
        ans += 1
print(ans)

Submission status: Accepted

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

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

print(sum(bt))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))


n,m = inpl()
xy = [0] * m
ball = [1] * n
check = [False] * n
check[0] = True
fl = True
res = 0
for i in range(m):
    x,y = inpl()
    ball[x-1] -= 1
    ball[y-1] += 1
    if check[x-1] == True:
        check[y-1] = True
    if ball[x-1] == 0 and check[x-1]:
        check[x-1] = False
print(check.count(True))

Submission status: Accepted

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

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

print(sum(red))

Submission status: Accepted

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

li=[1]*(n+1)

se=set()
se.add(1)
for  i in range(m):
	x,y=map(int,input().split())
	li[x]-=1
	li[y]+=1
	if x in se:
		se.add(y)
		if li[x]==0:
			se.remove(x)
print(len(se))

Submission status: Accepted

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


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

    A = [0]*N
    A[0] = 1
    B = [1]*N
    for q in range(M):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        if A[x] == 1:
            A[y] = 1
        B[x] -= 1
        B[y] += 1
        if B[x] == 0:
            A[x] = 0

    ans = 0
    for i in range(N):
        if A[i] == 1 and B[i] != 0:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

lis = [1] * 10 ** 5
ind_red = [False] * 10 ** 5
ind_red[0] = True
for m in range(M):
    x, y = map(int, input().split())
    if ind_red[x - 1] and lis[x - 1] >= 2:
        ind_red[y - 1] = True
    elif ind_red[x - 1] and lis[x - 1] < 2:
        ind_red[y - 1] = True
        ind_red[x - 1] = False
    lis[x - 1] -= 1
    lis[y - 1] += 1
print(sum(ind_red))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,m=LI()
  l=[0]*n
  l2=[1]*n

  l[0]=1

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

    if l[x-1]>0:
      l[y-1]+=1

      if l2[x-1]==0:
        l[x-1]=0

  sm=0
  for x in l:
    if x>0:
      sm+=1
  return sm

# main()
print(main())

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

# 箱iの中のボールの個数 箱の番号は問題文に合わせ1-initial
num=[1 for i in range(N+1)];num[0]=0
# 箱iの中に赤いボールが入っている可能性があるか
red=[False for i in range(N+1)];red[1]=True

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

print(len([1 for i in range(N+1) if red[i]]))

Submission status: Accepted

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

ans = sum(red)
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N, M = input().split()
N, M = [int(N), int(M)]
x = [None] * (M+1)
y = [None] * (M+1)

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

R = [0] + [1] + [0] * (N-1) # 赤いボールが入っている可能性があるかどうか
S = [0] + [1] * (N) # ボールの個数

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

    if R[x[i]] == 1:
        R[y[i]] = 1
        if S[x[i]] == 0:
            R[x[i]] = 0

sum = 0
for i in range(1, N+1):
    sum += R[i]

print(sum)

Submission status: Accepted

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

# [ball_num, oppotunity that the red ball in the box]
balls = [[0, 0]] + [[1, 1]] + [[1, 0] for _ in range(n - 1)]

for x, y in transfer:
    # from_box = balls[x]
    # to_box = balls[y]
    balls[x][0] -= 1
    balls[y][0] += 1
    if balls[x][1] == 1:
        balls[y][1] = 1
        if balls[x][0] == 0:
            balls[x][1] = 0

# print(balls)
print(len(list(filter(lambda x: x[1] == 1, balls))))

Submission status: Accepted

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

red[0] = True

for x, y in XY:
  if red[x -1] == True and num[x - 1] > 0:
    red[y - 1] = True
  num[x - 1] -= 1
  num[y - 1] += 1
  if num[x - 1] == 0:
    red[x - 1] = False

cnt = 0
for i, j in zip(red, num):
  if i == True and j > 0:
    cnt += 1
    
# print(red, num)
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
ball_num = [1] * n
red_exist = [0] * n
red_exist[0] = 1
flag = False
red_box = 0
for _ in range(m):
    x, y = LI()
    ball_num[x - 1] -= 1
    ball_num[y - 1] += 1
    if red_exist[x - 1]:
        red_exist[y - 1] = 1
    if ball_num[x - 1] == 0:
        red_exist[x - 1] = 0




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


print(cnt)

Submission status: Accepted

Solution code:
import sys
import os

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

#box[p][n] p:赤いボールが入っている可能性があるかどうか，n：ボールの数
box = [[0,1] for _ in range(N)]
box[0] = [1,1]
move = [0 for _ in range(M)]
for i in range(M):
    s,t = map(int,input().split())
    move[i] = [s-1,t-1]
for i in range(M):
    box[move[i][0]][1] -= 1
    box[move[i][1]][1] += 1
    if box[move[i][0]][0] == 1:
         box[move[i][1]][0] = 1
    if box[move[i][0]][1] == 0:
        box[move[i][0]][0] = 0
cnt = 0
for b in box:
    if b[0] == 1:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
n, m= map(int , input().split())
xy=[list(map(int,input().split())) for _ in range(m)]
count=[1]*n
ans=[True]+[False]*(n-1)
f=True
for i in range(m):
    if ans[xy[i][0]-1]== True:
        ans[xy[i][1]-1]=True
        if count[xy[i][0]-1]==1:
            ans[xy[i][0]-1]=False
    count[xy[i][0]-1]-=1
    count[xy[i][1]-1]+=1

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

print(answer)

Submission status: Accepted

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

cnt=[1 for _ in range(n)]
red=[True]+[False for _ in range(n-1)]
for x,y in XY:
  if cnt[x-1]>=1:
    cnt[x-1]-=1
    cnt[y-1]+=1
    if red[x-1]:
      red[y-1]=True
      if cnt[x-1]==0:
        red[x-1]=False
  else:
    red[x-1]=False
print(sum(red))

Submission status: Accepted

Solution code:
_,*B=open(0);W=[1]*7**6;R=[0,1]+[0]*7**6
for b in B:x,y=map(int,b.split());W[x]-=1;W[y]+=1;R[y]|=R[x];R[x]&=W[x]>0
print(sum(R))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

N, M = map(int, input().split())
box = 1
b = []
a = []
for i in range(N):
    a.append(1)
    b.append(0)
a.append(1)
b.append(0)

b[1] = 1

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

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

check = [[1, 0] for _ in range(N+1)]
check[1] = [1, 1]

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

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

Submission status: Accepted

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

for i in range(M):
  x,y=map(int,input().split())
  A[x]-=1
  A[y]+=1
  if B[x]:
    B[y]=1
    if not A[x]:
      B[x]=0
  #print(A)
  #print(B)
  #print("")
print(sum(B))

Submission status: Accepted

Solution code:
# AGC002B - Box and Ball
def main():
    N, M, *XY = map(int, open(0).read().split())
    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 zip(*[iter(XY)] * 2):
        R[y] = 1 if R[x] or R[y] else 0  # red ball may move from x to y
        C[x] -= 1
        C[y] += 1
        R[x] = 0 if C[x] == 0 else R[x]  # no more ball in x
    ans = sum(R[1:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import heapq
import bisect

mod = 10**9+7
dd = ((-1,0),(1,0),(0,-1),(0,1))

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(sys.stdin.readline()[:-1])
def IR(n): return([I() for _ in range(n)])

def GCD(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def LCM(a,b):
    return a * b // GCD(a,b)

def main():
    N,M = LI()
    XY = []
    for _ in range(M):
        XY.append(LI())

    possibility = [[False,1] for _ in range(N)]
    possibility[0] = [True,1]

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

        if possibility[x-1][1] == 0:
            possibility[x-1][0] = False

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

if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, M = MAP()

has_red = [False] * N
has_red[0] = True
box = [1] * N
for _ in range(M):
    x, y = MAP()
    x -= 1
    y -= 1
    box[x] -= 1
    box[y] += 1
    if has_red[x]:
        has_red[y] = True
    if box[x] == 0:
        has_red[x] = False

ans = 0
for i in range(N):
    if box[i] != 0 and has_red[i]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


def resolve():
    n, m = map(int, input().split())
    xys = list(list(map(int, input().split())) for _ in range(m))
    num = [1] * n
    red = [False] * n
    red[0] = True
    for x, y in xys:
        if red[x - 1]:
            red[y - 1] = True
        num[x - 1] -= 1
        num[y - 1] += 1
        if num[x - 1] == 0:
            red[x - 1] = False
    print(red.count(True))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ns = [1]*N
fs = [False]*N
fs[0] = True
for i in range(M):
  x, y = map(int, input().split())
  x, y = x-1, y-1
  if ns[x] > 1:
    ns[x] -= 1
    ns[y] += 1
    if fs[x]:
      fs[y] = True
  elif ns[x] == 1:
    ns[x] -= 1
    ns[y] += 1
    if fs[x]:
      fs[x] = False
      fs[y] = True
r = fs.count(True)
print(r)

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
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()
cnt = [1]*n
check = [True if i == 0 else False for i in range(n)]
for _ in range(m):
    x,y = LI()
    if check[x-1]:
        check[y-1] = True
        cnt[x-1] -= 1
        cnt[y-1] += 1
        if cnt[x-1] == 0:
            check[x-1] = False
    else:
        if cnt[x-1] > 0:
            cnt[x-1] -= 1
            cnt[y-1] += 1
ans = check.count(True)
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
red = [False] * n
num = [1] * n
red[0] = True
first = 0
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 num[x] == 0:
            red[x] = False
    if not red[x]:
        if num[x] > 0:
            num[y] += 1
            num[x] -= 1
print(red.count(True))

Submission status: Accepted

Solution code:
def main():
    N, M = (int(i) for i in input().split())
    xys = [[int(i)-1 for i in input().split()] for j in range(M)]
    boxes = [[False,1] for _ in range(N)]
    boxes[0][0] = True #Red
    for x,y in xys:
        if boxes[x][0] == True:
            if boxes[x][1] == 1:
                boxes[x][0] = False
            boxes[y][0] = True
        boxes[x][1] -= 1
        boxes[y][1] += 1
    ans = 0
    for tf,c in boxes:
        if tf:
            ans += 1
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M = (int(i) for i in input().split())
xys = [[int(i)-1 for i in input().split()] for j in range(M)]
boxes = [[False,1] for _ in range(N)]
boxes[0][0] = True #Red
for x,y in xys:
    if boxes[x][0] == True:
        if boxes[x][1] == 1:
            boxes[x] = [False,0]
        else:
            boxes[x][1] -= 1
        boxes[y] = [True,boxes[y][1] + 1]
    elif boxes[x][0] == False:
        boxes[x][1] -= 1
        boxes[y][1] += 1
ans = 0
for tf,c in boxes:
    if tf:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
bnum=[1]*n
red=[True]+[False]*(n-1)
for i in range(m):
    x,y=map(int,input().split())
    x-=1;y-=1
    if bnum[x]==1 and red[x]:
        red[x]=False
        bnum[x]-=1
        bnum[y]+=1
        red[y]=True
    elif red[x]:
        bnum[x]-=1
        red[y]=True
        bnum[y]+=1
    else:
        bnum[x]-=1;bnum[y]+=1
print(sum(1 for i in red if i))

Submission status: Accepted

Solution code:
def main():

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

    cand = {1}
    count = dict()
    for i in range(1, N+1):
        count[i] = 1
    for _ in range(M):
        a, b = map(int, input().split())
        if a in cand:
            cand.add(b)
        if count[a] == 1 and a in cand:
            cand.remove(a)
        count[a] -= 1
        count[b] += 1
    return len(cand)



if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n, m = [ int(v) for v in input().split() ]
ball_list = [1 for i in range(n)]
ball_list_2 = [1 for i in range(n)]
ball_list[0] = 2

for i in range(m):
    x, y = [ int(v)-1 for v in input().split() ]
    
    if ball_list[x] == 1:
        if ball_list[y] in [0,1]:
            ball_list[y] = 1
        else:
            ball_list[y] = 3
    elif ball_list[x] == 2:
        if ball_list[y] in [0,2]:
            ball_list[y] = 2
        else:
            ball_list[y] = 3
    elif ball_list[x] == 3:
        ball_list[y] = 3
    
    ball_list_2[y] += 1
    ball_list_2[x] -= 1
    if ball_list_2[x] == 0:
        ball_list[x] = 0
    
print(sum([ 1 for i in ball_list if i > 1]))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X, Y = [], []
for i in range(M):
    tmp = list(map(int, input().split()))
    X.append(tmp[0])
    Y.append(tmp[1])
box_ball = [1] * N
red = [False] * N
red[0] = True
for x, y in zip(X, Y):
    if red[x-1]:
        red[y-1] = True
    box_ball[x-1] -= 1
    box_ball[y-1] += 1
    if box_ball[x-1] == 0:
        red[x-1] = False
print(red.count(True))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ans = set([1])
balls = [1]*N

for _ in range(M):
    x, y = map(int, input().split())
    if x in ans:
        ans.add(y)
    balls[x-1] -= 1
    if balls[x-1] == 0 and x in ans:
        ans.remove(x)
    balls[y-1] += 1

print(len(ans))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[1 for _ in range(N)]
#Aはボールの個数
B=[0 for _ in range(N)]
#Bは箱に赤いボールが入っているか
ans=0
B[0]=1
for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  #インデックス値用
  if B[x]==1 and A[x]>=2:
    A[x]-=1
    A[y]+=1
    B[y]=1
  #取ってきた箱に赤が入っていて個数が二個以上
  #取ってきた箱にまだ赤が残る事がある
  elif B[x]==1 and A[x]==1:
    A[x]-=1
    A[y]+=1
    B[x]=0
    B[y]=1
  #取ってきた箱に赤が入っていて個数は一個
  #赤いボールは絶対に取り出される
  else:
    A[x]-=1
    A[y]+=1
  #白いボールの箱だけでのやり取りの時は個数の変化のみ
print(B.count(1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019/09/06

n,m=map(int,input().split())
box=[[1,1,0]]+[[1,0,1] for _ in range(n-1)]

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

cnt=0
for c,r,w in box:
    if  not c:continue
    if r:
        cnt+=1
print(cnt)

Submission status: Accepted

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

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

ans = sum(1 for v in red if v)
print(ans)

Submission status: Accepted

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

num = [1]*n
red = [0]*n
red[0] = 1

for i in range(m):
    x = list[i][0]-1
    y = list[i][1]-1
    if red[x] == 1:
        if num[x] == 1:
            red[x] = 0
        red[y] = 1
    num[x] -= 1
    num[y] += 1

print(sum(red))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
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

#A
def A():

    return

#B
def B():
    n,m = LI()
    f = [1]*n
    l = [0]*n
    l[0] = 1
    for i in range(m):
        x,y = LI()
        x -= 1
        y -= 1
        if l[x]:
            l[y] = 1
        f[x] -= 1
        f[y] += 1
        if f[x] == 0:
            l[x] = 0
    ans = 0
    for i in range(n):
        if l[i]:
            ans += 1
    print(ans)
    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

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

Submission status: Accepted

Solution code:
N, M = [int(i) for i in input().split()]
red = [False for i in range(N+1)]
red[1] = True
balls = [1 for i in range(N+1)]
for i in range(M):
    x, y = [int(_) for _ in input().split()]
    if red[x]:
        red[y] = True
    balls[x] -= 1
    if balls[x] == 0:
        red[x] = False
    balls[y] += 1
print(len([r for r in red if r]))

Submission status: Accepted

Solution code:
from sys import exit, stdin
from itertools import combinations
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
dp = [False]*(10**6)
dp2 = [0]*(10**6)
dp[1] = True
for i in range(N):
    dp2[i+1] = 1
for i in range(M):
    x, y = map(int, stdin.readline().rstrip().split())
    if dp[x]:
        dp[y] = True
    dp2[x] -= 1
    dp2[y] += 1
    if not dp2[x]:
        dp[x] = False
ans = 0
for i in range(N):
    if dp[i+1]:
        ans += 1
print(ans)

Submission status: Accepted

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

B = [1] * n
R = [0] * n
R[0] = 1
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1; y -= 1

    if B[x] == 1 and R[x] == 1:
        R[x] =  0; R[y] =  1
    elif B[x] > 1 and R[x] == 1:
        R[y] =  1
    B[x] -= 1; B[y] += 1

print(len([i for i in R if i==1]))

Submission status: Accepted

Solution code:
#import pysnooper
#import os,re,sys,operator,math,heapq,string
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline

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())
    num[x-1]-=1
    num[y-1]+=1
    if red[x-1]:
        red[y-1]=True
    if num[x-1]==0:
        red[x-1]=False
ans=0
for i in range(n):
    if red[i]:
        ans+=1
print(ans)

Submission status: Accepted

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

B = [1]*N
R=[False]*N

R[0]=True

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

print(sum(R))

Submission status: Accepted

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

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

R[0]=1

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

print(sum(R))

Submission status: Accepted

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

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

B[0][1] = 1

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

cnt = 0
for i in range(N):
    if B[i][1] == 1:
        cnt += 1

print(cnt)

Submission status: Accepted

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

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

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

	if ball[x] > 0:
		ball[x] -= 1
		ball[y] += 1

		if flag[x] == True:
			flag[y] = True
		if ball[x] == 0:
			flag[x] = False
	else:
		continue

print(sum(flag))

Submission status: Accepted

Solution code:
N, M, *XY = map(int, open(0).read().split())

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

box = [1] * (N + 1)

for x, y in zip(XY[::2], XY[1::2]):
    if red[x]:
        red[y] = True
    box[y] += 1
    box[x] -= 1
    if box[x] == 0:
        red[x] = False

print(red.count(True))