Problem:


Problem Statement

We have N boxes, numbered 1 through N. At first, box 1 contains one red ball, and each of the other boxes contains one white ball.

Snuke will perform the following M operations, one by one. In the i-th operation, he randomly picks one ball from box x_i, then he puts it into box y_i.

Find the number of boxes that may contain the red ball after all operations are performed.

Constraints

2≤N≤10^5

1≤M≤10^5

1≤x_i,y_i≤N

x_i≠y_i

Just before the i-th operation is performed, box x_i contains at least 1 ball.

Input

The input is given from Standard Input in the following format:

N M
x_1 y_1
:
x_M y_M

Output

Print the number of boxes that may contain the red ball after all operations are performed.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

2

Just after the first operation, box 1 is empty, box 2 contains one red ball and one white ball, and box 3 contains one white ball.

Now, consider the second operation. If Snuke picks the red ball from box 2, the red ball will go into box 3. If he picks the white ball instead, the red ball will stay in box 2.
Thus, the number of boxes that may contain the red ball after all operations, is 2.

Sample Input 2

3 3
1 2
2 3
2 3

Sample Output 2

1

All balls will go into box 3.

Sample Input 3

4 4
1 2
2 3
4 1
3 4

Sample Output 3

3



Sample input:
3 2
1 2
2 3


Sample output:
2

Submission status: Accepted

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

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

    vol = [1 for i in range(N)]
    prob = [False for _ in range(N)]
    prob[0] = True
    
    for _ in range(M):
        a,b = map(int,input().split())
        if prob[a-1]:
            prob[b-1] = True
        vol[a-1] -= 1
        vol[b-1] += 1
        if vol[a-1] == 0:
            prob[a-1] = False

    print(prob.count(True))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

cnt = [1 for i in range(N+1)]
flag = [0 for i in range(N+1)]
flag[1] = 1

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

print(sum(flag))

Submission status: Accepted

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

Submission status: Accepted

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

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

    if x in ans:
        if cnt[x] == 0:
            ans.remove(x)
        ans.add(y)

print(len(ans))

Submission status: Accepted

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

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

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

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

ans = sum(red)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def examA():
    a, b = LI()
    if b<a:
        a,b = b,a
    if a<=0 and 0<=b:
        print("Zero")
    elif 0<a:
        print("Positive")
    else:
        judge = b-a+1
        if judge%2==0:
            print("Positive")
        else:
            print("Negative")
    return

def examB():
    N, M = LI()
    flag_R = [False]*N
    cnt = [1]*N
    flag_R[0] = True
    for _ in range(M):
        x, y = LI()
        x -= 1; y -= 1
        if flag_R[x]:
            flag_R[y] = True
        cnt[y] += 1
        cnt[x] -= 1
        if cnt[x]==0:
            flag_R[x] = False
    ans = sum(flag_R)
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Accepted

Solution code:
N, M = map(int, input().split(' '))
nums = [1 for _ in range(N)]
has_balls = [False for _ in range(N)]
has_balls[0] = True

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

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

    nums[y] += 1
    nums[x] -= 1
    if not nums[x]:
        has_balls[x] = False



ret = 0
for h in has_balls:
    if h:
        ret += 1

print(ret)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n,m = map(int,input().split())
cnt = [1]*n
visit = [0]*n
visit[0] = 1
for i in range(m):
    x,y = map(int,input().split())
    if visit[x-1] > 0:
        if cnt[x-1] == 1:
            visit[x-1] = 0
        visit[y-1] =1
    cnt[x-1] -=1
    cnt[y-1] += 1
print(sum(1 for i in visit if i > 0))

Submission status: Accepted

Solution code:
# import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n, m = list(map(int,ipti().split()))

    ball = [1] * n
    ball[0] = -1

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

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

    ans = 0

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

print(len(list(filter(lambda x: x == True, flag))))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
#全部シュミレートして、0個だったやつは弾く
#赤いボールが入っている可能性があったもの、からうつったものもすべて候補に入る
candidate = set([1])
ball = [1 for i in range(n+1)]
for i in range(m):
    x, y = map(int, input().split())
    if x in candidate:
        candidate.add(y)
    else:
        if ball[y] == 0 and y in candidate:
            candidate.remove(y)
    ball[x] -= 1
    ball[y] += 1
for i in range(1,n+1):
    if ball[i] == 0 and i in candidate:
        candidate.remove(i)
print(len(candidate))

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

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

Submission status: Accepted

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

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

print(len([True for r in red if r>0]))

Submission status: Accepted

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

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

print(len([True for r in red if r>0]))

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(200000000)


# my functions here!

#入力

def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    N,M=pin()
    ans=0
    red=[False]*N
    red[0]=True
    balls=[1]*N
    
    for i in range(M):
        x,y=pin()
        x,y=x-1,y-1
        #ボールの持ち出し
        if red[x]==True:
            red[y]=True
            if balls[x]==1:
                red[x]=False
        balls[x]-=1
        balls[y]+=1    
    print(sum(red))
    
    
"""
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

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

if __name__=="__main__":resolve()

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

Submission status: Accepted

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

num = [1]*n
red = [1] + [0]*(n-1)

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

    if red[x]:
        red[y] = 1
    if num[x] == 0:
        red[x] = 0

print(sum(red))

Submission status: Accepted

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


A = [[1, 1]] + [[0, 1] for _ in range(n-1)]

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

    if A[x][1] == 0:
        A[x][0] = 0

print(sum(a[0] for a in A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N,M=map(int,readline().split())
    x=[0]*M
    y=[0]*M
    for i in range(M):
        a,b=map(int,readline().split())
        x[i]=a
        y[i]=b

    li=[[1,0] for _ in range(N+1)]
    li[1][1]=1
    for i in range(M):
        if li[x[i]][0]>=2 and li[x[i]][1]==1:
            li[y[i]][0]+=1
            li[y[i]][1]=1

            li[x[i]][0]-=1

        elif li[x[i]][0]==1 and li[x[i]][1]==1:
            li[y[i]][0]+=1
            li[y[i]][1]=1

            li[x[i]][0]-=1
            li[x[i]][1]=0

        else:
            li[y[i]][0]+=1

            li[x[i]][0]-=1
            
    count=0
    for i in range(1,N+1):
        if li[i][1]==1: count+=1
    print(count)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
#B
n,m = [int(i) for i in input().split()]
"""
xs = []
ys = []
for _ in range(m):
    x,y = [int(i) for i in input().split()]
    xs.append(x)
    ys.append(y)
"""

counts = [1] * n
possible = [1] + [0]*(n-1)

for _ in range(m):
    x,y = [int(i) for i in input().split()]
    
    counts[x-1] -= 1
    counts[y-1] += 1
    
    if possible[x-1]==1:
        possible[y-1] = 1
    
    if counts[x-1]==0:
        possible[x-1] = 0
        
cnt = 0
for v in possible:
    if v==1:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
#B
n,m = [int(i) for i in input().split()]
xs = []
ys = []
for _ in range(m):
    x,y = [int(i) for i in input().split()]
    xs.append(x)
    ys.append(y)
    
counts = [1] * n
possible = [1] + [0]*(n-1)

for i in range(m):
    x = xs[i]
    y = ys[i]
    
    counts[x-1] -= 1
    counts[y-1] += 1
    
    if possible[x-1]==1:
        possible[y-1] = 1
    
    if counts[x-1]==0:
        possible[x-1] = 0
        
cnt = 0
for v in possible:
    if v==1:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N,M=map(int,input().split())

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

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

print(sum(red))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    N, M = map(int, input().split())
    n_ball = [1] * N
    could_exist = [False] * N
    could_exist[0] = True

    for _ in range(M):
        frm, to = map(lambda elem:int(elem)-1, input().split())
        if could_exist[frm] and n_ball[frm] > 0:
            could_exist[to] = True
        if could_exist[frm] and n_ball[frm] == 1:
            could_exist[frm] = False
        n_ball[frm] -= 1
        n_ball[to] += 1
    
    print(sum(could_exist))
    
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def main():
  n, m = input_list()
  box = [False]*n
  box[0] = True
  nums = [1]*n
  for i in range(m):
    x, y = input_list()
    if box[x-1]:
      box[y-1] = True
    nums[x-1] = nums[x-1] - 1
    nums[y-1] = nums[y-1] + 1
    if nums[x-1] == 0:
      box[x-1] = False
  print(box.count(True))
    

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

import math
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(m)]
count=[1]*(n+1)
s=set()
s.add(1)
for a,b in arr:
  count[b]+=1
  count[a]-=1
  if a in s:
    s.add(b)
  if count[a]==0:
    s.discard(a)
print(len(s))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
data=[]
for i in range(m):
    x,y=map(int,input().split())
    data.append((x,y))
ball=[[1,False] for i in range(n)]
ball[0][1]=True
#一番めのボールは赤色であるため、Trueを返す
for i in range(m):
    x,y=data[i][0],data[i][1]
    #xからyへ
    if ball[x-1][0]==1:
        #x番目のところにボールが一つ入っていた場合
        if ball[x-1][1]==True:
            ball[x-1][0]=0
            ball[y-1][0]+=1
            ball[x-1][1]=False
            ball[y-1][1]=True      
        elif ball[x-1][1]==False:
            ball[x-1][0]=0
            ball[y-1][0]+=1
    elif ball[x-1][0]>=2:
        #x番めのところにボールが二つ入っていた場合
        if ball[x-1][1]==True:
            ball[x-1][0]-=1
            ball[y-1][0]+=1
            ball[y-1][1]=True
        elif ball[x-1][1]==False:            
            ball[x-1][0]-=1
            ball[y-1][0]+=1
cnt=0
for i in range(n):
    if ball[i][1]:
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
def main():
    n, m = map(int,input().split())
    x = []
    y = []
    for _ in range(m):
        xx, yy = map(int,input().split())
        x.append(xx-1)
        y.append(yy-1)
    ans = [0] * n
    ans[0] = 1
    red = [0] * n
    red[0] = 1
    num = [1] * n
    for i in range(m):
        if num[x[i]] > 0:
            num[x[i]] -= 1
            num[y[i]] += 1
            if ans[y[i]] == 0 and red[x[i]] == 1:
                ans[y[i]] = 1
                red[y[i]] = 1
            if num[x[i]] == 0 and ans[x[i]] == 1:
                red[x[i]] = 0
                ans[x[i]] = 0
    
    print(sum(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
"""考え方
以下の2種類の配列を用意する。（いずれも、サイズは（1,N）)
1.どこに赤い玉がある可能性があるか
2.どこに何個玉があるか
"""

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

for m in range(M):
  x,y = map(int, input().split())
  
  ## 1.xがredの配列にHitするかを確認
  if red[x] > 0:
    ## 1-1.拡散する
    red[y] = 1
    
  ## 1-2.玉の個数の調整
  num[x] -= 1
  num[y] += 1
  
  ## 1-3.感染状態の更新
  if red[x] > 0:
    if num[x] == 0:
      red[x] = 0
    
ans = len([True for r in red if r > 0])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
l1=[1]*n
l2=[1]+[0]*(n-1)

for x,y in l:
  x-=1
  y-=1

  l1[y]+=1
  if l2[y]==0 and l2[x]==1:
    l2[y]=1

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

print(l2.count(1))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(m):
    moveFrom = xy[i][0] - 1
    moveTo   = xy[i][1] - 1
    if red[moveFrom] == 1 and num[moveFrom] == 1:
        red[moveFrom] = 0
        red[moveTo] = 1
    elif red[moveFrom] == 1:
        red[moveTo] = 1
    num[moveFrom] -= 1
    num[moveTo] += 1

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

Submission status: Accepted

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

gray = [False] * n
gray[0] = True
ball = [1] * n

for x, y in xy:
    x -= 1; y -= 1
    ball[x] -= 1
    ball[y] += 1
    if gray[x]:
        gray[y] = True
    if not ball[x]:
        gray[x] = False

print(sum(gray))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, M: int, x: "List[int]", y: "List[int]"):
    c = [1] * (N+1)
    p = [False] * (N+1)
    p[1] = True

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

        if p[x[i]] is True:
            p[y[i]] = True
        
        if c[x[i]] == 0:
            p[x[i]] = False

    print([i for i in p if i is True].count(True))

    return



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))
        y[i] = int(next(tokens))
    solve(N, M, x, y)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
N,M=map(int,input().split())
ball = [1 for i in range(N+1)]
ball[0] = 0
red = [0 for i in range(N+1)]
red[1] = 1


for i 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 = [0 for i in range(n)]
amount = [1 for i in range(n)]
red[0] = 1
for _ in range(m):
    x,y = map(int,input().split())
    amount[x-1] -= 1
    amount[y-1] += 1
    if red[x-1]:
      red[y-1] = 1
    if amount[x-1] == 0:
      red[x-1] = 0
ans = 0
for r,a in zip(red, amount):
  if r > 0 and a > 0:
    ans += 1



print(ans)

Submission status: Accepted

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

box_count=[1]*N
box_color=[1]
buf=[0]*(N-1)
box_color.extend(buf)

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

ans=0
for cnt,color in zip(box_count,box_color):
    if cnt>0 and color==1:
        ans+=1

print(ans)

Submission status: Accepted

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

l = [0] * (N + 1)
l[1] = 1
c = [1] * (N + 1)

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

print(sum(l))

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())
    x[i] -= 1
    y[i] -= 1
cnt = [1]*n
flag = [False]*n
flag[0] = True
for i in range(m):
    if flag[x[i]]: 
        flag[y[i]] = True
        if cnt[x[i]] == 1: 
            flag[x[i]] = False
    cnt[x[i]] -= 1
    cnt[y[i]] += 1
    
print(flag.count(True))

Submission status: Accepted

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

s = {1}
cnt = [1] * (N + 1)
for i in range(M):
    x, y = map(int, input().split())
    cnt[x] -= 1
    cnt[y] += 1
    if x in s:
        s.add(y)
        if cnt[x] == 0:
            s.discard(x)

print(len(s))

Submission status: Accepted

Solution code:
#codng utf-8
N,M=map(int,input().split())
B=[1]*(N+1)
F=[0]*(N+1)#0がある場所
F[1]=1


for i in range(M):
    a,b=map(int,input().split())
    if F[a]==1:
        if B[a]==1:
            F[a]=0
            F[b]=1
        else:
            F[b]=1
        B[a]-=1
        B[b]+=1
    else:
        B[a]-=1
        B[b]+=1


print(F.count(1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!s = input()
n, m = map(int, input().split())
x = []
y = []
for i in range(m):
    xi, yi = map(int, input().split())
    x.append(xi)
    y.append(yi)
boxes = [False]*n
boxes[0] = True
boxescnt = [1]*n
for i in range(m):
    if boxes[x[i]-1] == True:
        if boxescnt[x[i]-1] == 1:
            boxes[x[i]-1] = False
        boxes[y[i]-1] = True
    boxescnt[x[i]-1] -= 1
    boxescnt[y[i]-1] += 1
print(sum(boxes))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ball = [1] * N
red = {1}
ans = 0
for _ in range(M):
    x, y = map(int, input().split())
    ball[x - 1] -= 1
    ball[y - 1] += 1
    if x in red:
        red.add(y)
        if ball[x - 1] == 0:
            red.remove(x)
print(len(red))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
ball = [1] * (N+1) # 1-indexed
red = [True if x == 1 else False for x in range(N+1)] # 1-indexed
for _ in range(M):
    x, y = lr()
    if red[x]: red[y] = True
    ball[x] -= 1
    ball[y] += 1
    if ball[x] == 0:
        red[x] = False

answer = sum(red)
print(answer)
# 37

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = []
for i in range(m):
    data = list(map(int,input().split()))
    l.append(data)
ball_nums=[1]*n
red_nums=[0]*n
red_nums[0]=1

for i in range(m):
    ball_nums[l[i][0]-1] -= 1
    ball_nums[l[i][1]-1] += 1

    if red_nums[l[i][0]-1]==1:
        red_nums[l[i][1]-1] = 1
        if ball_nums[l[i][0]-1] == 0:
            red_nums[l[i][0]-1] = 0

print(sum(red_nums))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

xx = []
yy = []
for _ in range(m):
    a,b = map(int,input().split())
    xx.append(a-1)
    yy.append(b-1)

for i in range(m):
    x,y = xx[i], yy[i]
    if red_lst[x]==True and red_lst[y]==False:
        red_lst[y]=True
    if lst[x]==1 and red_lst[x]:
        red_lst[x] = False
    lst[x] -= 1
    lst[y] += 1

print(red_lst.count(True))

Submission status: Accepted

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

dp = [0 for i in range(N)] #dp[m][n]
dp[0]=1

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

  if dp[x-1]>=1:
    if Q[x-1]==1:
      dp[x-1]=0
    else:
      dp[x-1]=1
    dp[y-1]+=1
  Q[x-1]-=1
  Q[y-1]+=1
  
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())
y_if = {1}
num = [1] * N
for i in range(M):
    x, y = map(int, input().split())
    num[x - 1] -= 1
    num[y - 1] += 1
    if x in y_if:
        if num[x - 1] == 0:
            y_if.remove(x)
        y_if.add(y)
print(len(y_if))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[list(map(int,input().split())) for i in range(M)]
#Bは色　Cは個数
B=[0]*N
C=[1]*N
B[0]=1
for i in range(M):   
  if (B[(A[i][0]-1)]==1)and(C[(A[i][0]-1)]==1):
    B[(A[i][1]-1)]=1
    B[(A[i][0]-1)]=0
    C[(A[i][0]-1)]=0
    C[(A[i][1]-1)]+=1
  elif (B[(A[i][0]-1)]==1)and(C[(A[i][0]-1)]>1):
    B[(A[i][1]-1)]=1
    C[(A[i][0]-1)]-=1
    C[(A[i][1]-1)]+=1
  else:
    C[(A[i][0]-1)]-=1
    C[(A[i][1]-1)]+=1
    
    
print(sum(B))

Submission status: Accepted

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

B = [["w", 1] for _ in range(n)]
B[0][0] = "r"

for i in range(m):
    x, y = XY[i]
    if B[y][1] == 0:
        B[y][0] = B[x][0]
    elif B[x][0] != B[y][0]:
        B[y][0] ="rw"
    B[x][1] -= 1
    B[y][1] += 1
    if B[x][1] == 0:
        B[x][0] = " "


ans = 0
for i in range(n):
    if B[i][1] >= 1 and B[i][0] == "r" or B[i][0] == "rw":
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
x,y = [],[]
for _ in range(M):
	tx,ty = map(int, input().split())
	x.append(tx)
	y.append(ty)

flg = [0] * (N+1)
flg[1] = 1
cnt = [1] * (N+1)
#フラグが１が経っているところからは、移動することで赤を移せる。
for i in range(M):
	cp,np = x[i],y[i]

	if flg[cp] == 1:
		if cnt[cp] == 1:
			flg[cp] = 0
		cnt[cp] -= 1
		cnt[np] += 1
		flg[np] = 1
	else:
		cnt[cp] -= 1
		cnt[np] += 1

print(sum(flg))

Submission status: Accepted

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

box = [[0, 0], [1, 1]] + [[1, 0] for i in range(n-1)]

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

ans = 0
for b in box:
    ans += b[1]

print(ans)