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
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

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

def solve():
    #n = int(stdin.readline().rstrip())
    n,m = map(int, stdin.readline().rstrip().split())
    #l = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    red = [0] * n
    red[0] = 1
    ball = [1] * n
    for i in range(m):
        x,y = map(int, stdin.readline().rstrip().split())
        if red[x-1] != 0 and ball[x-1] != 1:
            red[y-1] = 1
            ball[x-1] -= 1
            ball[y-1] += 1
        elif red[x-1] != 0 and ball[x-1] == 1:
            red[x-1] = 0
            red[y-1] = 1
            ball[x-1] -= 1
            ball[y-1] += 1
        else:
            ball[x-1] -= 1
            ball[y-1] += 1
    print(sum(red))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

aka_list = [True]
for i in range(1, n):
    aka_list.append(False)
balls = [1] * n

for i in range(m):
    if aka_list[x_y[i][0] - 1]:
        aka_list[x_y[i][1]-1] = True
    balls[x_y[i][0] - 1] -= 1
    balls[x_y[i][1] - 1] += 1
    if balls[x_y[i][0] - 1] == 0:
        aka_list[x_y[i][0] - 1] = False

print(sum([1 for i in range(len(aka_list)) if aka_list[i]]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def solve():
    N, M = map(int, input().split())
    exist = [1] * N
    red = [False] * N
    red[0] = True
    for i in range(M):
        x, y = map(int, input().split())
        x -= 1; y -= 1
        exist[x] -= 1; exist[y] += 1
        if red[x]:
            red[y] = True
        if exist[x] == 0:
            red[x] = False
    ans = 0
    for i in range(N):
        if red[i] and exist[i] > 0:
            ans += 1
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
x = []
for i in range(m):
    x0, x1 = MAP()
    x.append([x0-1, x1-1])

r = [0]*n
w = [1]*n
t = [1]*n
r[0] = 1
w[0] = 0

for y in x:
    t[y[0]] -= 1
    t[y[1]] += 1

    if r[y[0]] > 0 and w[y[0]] == 0:
        r[y[0]] = 0
        r[y[1]] = 1
    elif r[y[0]] == 0 and w[y[0]] > 0:
        w[y[0]] -= 1
        w[y[1]] += 1
    elif r[y[0]] > 0 and w[y[0]] > 0:
        r[y[1]] = 1
        w[y[1]] += 1
    if t[y[0]] == 0:
        r[y[0]] = w[y[0]] = 0

ans = sum(r)
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M = mapint()
boxes = [0]*N
balls = [1]*N
boxes[0] = 1
for _ in range(M):
    x, y = mapint()
    boxes[y-1] = 1 if boxes[x-1]==1 else boxes[y-1]
    balls[x-1] -= 1
    balls[y-1] += 1
    if balls[x-1]==0:
        boxes[x-1] = 0

print(sum(boxes))

Submission status: Accepted

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

def main():
    N, M = map(int, input().split())
    xy = [list(map(int, input().split())) for i in range(M)]
    box = [1] * N
    red_flag = [0] * N
    red_flag[0] = 1
    ans = 0
    for i in range(M):
        if red_flag[xy[i][0] - 1] == 1:
            red_flag[xy[i][1] - 1] = 1
        box[xy[i][0] - 1] -= 1
        if box[xy[i][0] - 1] == 0:
            red_flag[xy[i][0] - 1] = 0
        box[xy[i][1] - 1] += 1
    print(sum(red_flag))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def main():
    N, M = map(int, input().split())
    xy = [list(map(int, input().split())) for i in range(M)]
    box = [1] * N
    red_flag = [0] * N
    red_flag[0] = 1
    ans = 0
    for i in range(M):
        if red_flag[xy[i][0] - 1] == 1:
            red_flag[xy[i][1] - 1] = 1
        box[xy[i][0] - 1] -= 1
        if box[xy[i][0] - 1] == 0:
            red_flag[xy[i][0] - 1] = 0
        box[xy[i][1] - 1] += 1
    for i in range(N):
        if box[i] != 0 and red_flag[i] == 1:
            ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

# B - Box and Ball
N, M = map(int, input().split())

boxes = [1] * (N + 1)
boxes[0] = 0

ball_exists = set()
ball_exists.add(1)

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

  boxes[x] -= 1
  boxes[y] += 1
  
  if x in ball_exists:
    if boxes[x] == 0:
      ball_exists.remove(x)

      if len(ball_exists) == 1 and y in ball_exists:
        ball_exists.clear()

    ball_exists.add(y)

  #print(boxes)
  #print(ball_exists)

print(len(ball_exists))

Submission status: Accepted

Solution code:
import collections
n,m = map(int,input().split())
box = [[1,"white"] for i in range(n)]
box[0][1] = "red"
for i in range(m):
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    if box[x][1] == "red":
        box[y][1] = "red"
    box[y][0] += 1
    box[x][0] -= 1
    if box[x][0] == 0:
        box[x][1] = "white"
ans = 0
for i in range(n):
    if box[i][1] == "red":
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
(n, m), *q = [[*map(int, i.split())] for i in open(0)]
b = [0] * (n + 1)
b[1] = 1
c = [1] * (n + 1)
for x, y in q:
    c[x] -= 1
    c[y] += 1
    b[y] |= b[x]
    b[x] *= c[x] > 0
print(sum(b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
(n, m), *q = [[*map(int, i.split())] for i in open(0)]
b = [1] + [0] * (n - 1)
c = [1] * n
for x, y in q:
    c[x - 1] -= 1
    c[y - 1] += 1
    b[y - 1] |= b[x - 1]
    b[x - 1] *= c[x - 1] > 0
print(sum(b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
(n, m), *q = [[*map(int, i.split())] for i in open(0)]
b = [1] + [0] * (n - 1)
c = [1] * n
for x, y in q:
    c[x - 1] -= 1
    c[y - 1] += 1
    b[y - 1] |= b[x - 1]
    if c[x - 1] == 0:
        b[x - 1] = 0
print(sum(b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[27]:


N,M = map(int, input().split())
xy = []
b = [1]*N
p = [False]*N
p[0] = True
for _ in range(M):
    x,y = map(int, input().split())
    b[x-1] -= 1
    b[y-1] += 1
    if p[x-1]:
        p[y-1] = True
    if b[x-1] == 0:
        p[x-1] = False


# In[28]:


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


# In[ ]:

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# agc002_b.py
N, M = map(int,input().split())
flg = [False]*N
flg[0] = True
cnt = [1]*N
for i in range(M):
    x, y = map(int,input().split())
    if flg[x-1]:
        if cnt[x-1]>1:
            flg[y-1] = True
        elif cnt[x-1]==1:
            flg[y-1] = True
            flg[x-1] = False
        else:
            continue
        cnt[x-1]-=1
        cnt[y-1]+=1
    else:
        if cnt[x-1]>0:
            cnt[x-1]-=1
            cnt[y-1]+=1


print(flg.count(True))

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
C = [1] * (N + 1)
B = [True, True] + [False] * (N - 1)
flg = False
for _ in range(M):
    x, y = list(map(int, input().split()))
    if B[x]:
        B[y] = True
    if x == 1 and not flg and C[1] == 1:
        B[1] = False
        flg = True
    if y == 1 and not flg:
        flg = True
    C[x] -= 1
    C[y] += 1
    if C[x] == 0:
        B[x] = False
res = 0
for i in range(1, N + 1):
    if B[i] and C[i] > 0:
        res += 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
z = [list(map(int,input().split())) for i in range(M)]
box = [[1,1]]
for i in range(N-1):
  box.append([0,1])
for i in range(M):
  box[z[i][0]-1][1] -= 1
  box[z[i][1]-1][1] += 1
  if (box[z[i][0]-1][0] == 1):
    box[z[i][1]-1][0] = 1
  if (box[z[i][0]-1][1] == 0):
    box[z[i][0]-1][0] = 0
ans = 0
for i in range(N):
  if (box[i][0] != 0):
    ans += 1
print(ans)

Submission status: Accepted

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

may_red = [0] * N
may_red[0] = 1
balls = [1] * N

for x,y in XY:
    x,y = x-1,y-1
    if may_red[x]:
        may_red[y] = 1
        if balls[x] == 1:
            may_red[x] = 0
    balls[x] -= 1
    balls[y] += 1
print(sum(may_red))

Submission status: Accepted

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

Submission status: Accepted

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

boxl = [1]*(n+1)
redl = [False]*(n+1)
redl[1] = True

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

ans = 0
for r in redl:
    if r: ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N, M = inm()


def solve():
    r = [False] * N
    r[0] = True
    cnt = [1] * N
    for i in range(M):
        x, y = inm()
        x -= 1
        y -= 1
        if r[x]:
            r[y] = True
        cnt[x] -= 1
        cnt[y] += 1
        if cnt[x] == 0:
            r[x] = False

    return sum(r)


print(solve())

Submission status: Accepted

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

Submission status: Accepted

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

box = [1] * (n+1)
red = [0] * (n+1)
red[1] = 1
for x,y in xy:
    box[x] -= 1
    box[y] += 1
    if red[x] == 1: red[y] = 1
    if box[x] == 0: red[x] = 0
print(red.count(1))

Submission status: Accepted

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

print(sum(X))

Submission status: Accepted

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

countR=[0]+[1]+[0]*(N-1)
countBall=[0]+[1]*N

for x,y in xy:
    countBall[x]-=1
    countBall[y]+=1
    if countR[x]>0:
        countR[y]+=1
    if countBall[x]<1:
        countR[x]=0

print(sum(r*b>0 for r,b in zip(countR,countBall)))

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N, M = getNM()
que = []
for i in range(M):
    a, b = getNM()
    que.append([a - 1, b - 1])
ball = [1] * N
red = [0] * N
red[0] = 1

for a, b in que:
    ball[b] += 1
    if red[a] == 1:
        red[b] = 1
    ball[a] -= 1
    if ball[a] == 0:
        red[a] = 0
print(sum(red))

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
from fractions  import gcd
#input=sys.stdin.readline
import bisect
n,m=map(int,input().split())
num=[1]*n
s=set()
s.add(1)
ans=1
for i in range(m):
    x,y=map(int,input().split())
    num[x-1]-=1
    num[y-1]+=1
    if x in s:
        if num[x-1]!=0 and y not in s:
          ans+=1
        elif num[x-1]==0:
          s.discard(x)
          if y in s:
            ans-=1
        s.add(y)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

num = [1 for i in range(N)]
red = [False for i in range(N)]
red[0] = True

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

print(red.count(True))

Submission status: Accepted

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

# from collections import deque

def linput(ty=int, cnv=list):
    return cnv(map(ty, input().split()))

def vinput(rep=1, ty=int, cnv=list):
    return cnv(ty(input().rstrip()) for _ in "*"*rep)

def gcd(n,m):
    while m: n,m = m, n%m
    return n

def lcm(n,m): return n*m//gcd(n,m)

def main():
    N,M = linput()

    vR = [0, 1,] + [0,]*(N-1)
    vN = [0, 1,] + [1,]*(N-1)

    res = 0

    for _ in range(M):
        a,b = linput()
        if vR[a]:
            vR[b] = 1
        if vN[a]==1:
            vR[a] = 0

        vN[a] -= 1
        vN[b] += 1

    res = sum(vR)
    print(res)
    # sT = "No Yes".split()
    # print(sT[res])
    # for vR in mR:
    #     print(*vR, sep="")

main()

Submission status: Accepted

Solution code:
n, m = [int(w) for w in input().split()]

lb = [False] * n
lb[0] = True
lbn = [1] * n

for i in range(m):
    x, y = [int(w)-1 for w in input().split()]
    if lb[x] and lbn[x] > 0:
        lb[y] = True
    lbn[x] -= 1
    lbn[y] += 1

    if lb[x] and lbn[x] == 0:
        lb[x] = False

print(len([_ for _ in lb if _]))

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    N, M = [int(item) for item in input().split()]
    queries = [[int(item)-1 for item in input().split()] for _ in range(M)]

    memo = [['w'] for _ in range(N)]
    memo[0] = ['r']
    is_first_0 = True
    for x, y in queries:
        if 'r' in memo[x]:
            memo[y] += ['r']
            if 'w' in memo[x]:
                memo[x].remove('w')
            else:
                memo[x].remove('r')
        else:
            memo[y] += ['w']
            memo[x].remove('w')
    res = 0
    for line in memo:
        for item in set(line):
            if item == 'r':
                res += 1
    print(res)



if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ball_cnt = [1] * N
exist_red = [False] * N
exist_red[0] = True
for _ in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    ball_cnt[x] -= 1
    ball_cnt[y] += 1
    if exist_red[x]:
        exist_red[y] = True
    if ball_cnt[x] == 0:
        exist_red[x] = False
print(exist_red.count(True))

Submission status: Accepted

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

ans = {1}
l = [1] * n

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

    if x in ans:
        if l[x - 1] == 0: ans.remove(x); ans.add(y)
        else: ans.add(y)
print(len(ans))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc002/tasks/agc002_b

n, m = map(int, input().split())

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

box = [0] * n
box[0] = 1
num = [1] * n
for x, y in xy:
    if box[x]:
        box[y] |= 1
    num[x] -= 1
    num[y] += 1
    if not num[x]:
        box[x] = 0
ans = 0
for i in range(n):
    if box[i] and num[i]:
        ans += 1
print(ans)

Submission status: Accepted

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

red, cnt = set([1]), {i: 1 for i in range(1, n + 1)}
for xi, yi in xy:
    if xi in red:
        red.add(yi)
        if cnt[xi] == 1:
            red.remove(xi)
    cnt[xi] -= 1
    cnt[yi] += 1
print(len(red))

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

lst=[1]*n
anslst=[0]*n
anslst[0]=1

for x,y in xy:
    x,y=x-1,y-1
    if anslst[x]==1:
        anslst[y]=1
    lst[x]-=1
    lst[y]+=1
    if lst[x]==0:
        anslst[x]=0

print(sum(anslst))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, m = [int(x) for x in input().split()]
    g = defaultdict(list)
    cnt = [1] * (n + 1)
    w = [0] * (n + 1)
    w[1] = 1
    for _ in range(m):
        a, b = [int(x) for x in input().split()]
        g[a].append(b)
        cnt[a] -= 1
        cnt[b] += 1
        if w[a]:
            if cnt[a] == 0:
                w[a] = 0
            w[b] = 1
    
    ans = 0
    for i in range(1, n+1):
        if cnt[i] and w[i]:
            ans += 1
    print(ans)
    

        
    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

R s
S p
P r

"""

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
ball_in_boxes = [1]*n
redball_flag = [False]*n
redball_flag[0] = True
for _ in range(m):
    x,y = map(int,input().split())
    if redball_flag[x-1]: # x番目の箱に赤いボールが入っている可能性がある
        redball_flag[y-1] = True
    ball_in_boxes[x-1] -= 1
    ball_in_boxes[y-1] += 1

    if ball_in_boxes[x-1] == 0: # x番目の箱にボールが入っていないので
        redball_flag[x-1] = False
print(redball_flag.count(True))

Submission status: Accepted

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


# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    n, m = input_list()
    balls = [1] * n
    reds = [False] * n
    reds[0] = True
    for _ in range(m):
        x, y = input_list()
        if reds[x-1]:
            reds[y-1] = True
        balls[x-1] -= 1
        balls[y-1] += 1
        if balls[x-1] == 0:
            reds[x-1] = False
    c = 0
    for v in reds:
        if v:
            c += 1
    print(c)


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, m, *XY = map(int, open(0).read().split())
A = [1] * n
B = [0] * n
B[0] = 1
for x, y in zip(XY[::2], XY[1::2]):
  x, y = x-1, y-1
  A[x] -= 1
  A[y] += 1
  if B[x]:
    B[y] = 1
  if A[x] == 0:
    B[x] = 0
print(sum(B))

Submission status: Accepted

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

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

for i in range(m):
    cnt[x[i]]-=1
    cnt[y[i]]+=1

    if red[x[i]]==1:
        red[y[i]]=1
    
    if cnt[x[i]]==0:
        red[x[i]]=0

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

print(ans)

Submission status: Accepted

Solution code:
def operate(x, y):
    if is_red[x]:
        is_red[y] = True
        if num_ball[x] == 1:
            is_red[x] = False
    num_ball[x] -= 1
    num_ball[y] += 1


if __name__ == "__main__":

    N, M = [int(x) for x in input().split(" ")]
    num_ball = [1 for _ in range(N)]
    is_red = [True] + [False for _ in range(N - 1)]

    for _ in range(M):
        x, y = [int(x) for x in input().split(" ")]
        x, y = x - 1, y - 1
        operate(x, y)
    print(sum(is_red))

Submission status: Accepted

Solution code:
def solve(n, m, x, y):
    x = list(map(lambda _:_-1, x))
    y = list(map(lambda _:_-1, y))
    p = [0] * n
    p[0] = 1
    c = [1] * n
    for i in range(m):
        if c[x[i]] > 1:
            if p[x[i]] == 1:
                p[y[i]] = 1
        else:
            if p[x[i]] == 1:
                p[y[i]] = 1
            p[x[i]] = 0
        c[x[i]] -= 1
        c[y[i]] += 1
    return sum(p)

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())
print(solve(n, m, x, y))

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
n, m = map(int, input().split())
may_contain_red = [0 for _ in range(n + 1)]
num_of_ball = [1 for _ in range(n + 1)]
may_contain_red[1] = 1
for i in range(m):
    x, y = map(int, input().split())
    num_of_ball[x] -= 1
    num_of_ball[y] += 1
    if may_contain_red[x] != 0:
        may_contain_red[y] = 1
    if num_of_ball[x] == 0:
        may_contain_red[x] = 0

print(sum(may_contain_red))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
arr=[[0,1,0] for _ in range(N)]
arr[0]=[1,1,1]
for i in range(M):
  a,b=map(int,input().split())
  arr[a-1][1]-=1
  arr[b-1][1]+=1
  if arr[a-1][0]==1:
    arr[b-1][0]=1
  if arr[a-1][2]==1:
    arr[a-1][2],arr[b-1][2]=0,1
  if arr[a-1][1]==0:
    arr[a-1][0]=0
ans=0
for i in range(N):
  if arr[i][0]==1 and arr[i][1]!=0:
    ans+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(m):
    if red[xy[i][0]-1]>=1:
        if ball[xy[i][0]-1]==1:
            red[xy[i][0]-1]=0
        red[xy[i][1]-1]+=1
    ball[xy[i][0]-1]-=1
    ball[xy[i][1]-1]+=1

ans=len(red)-red.count(0) 
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    red = [False] * n
    red[0] = True
    balls = [1] * n
    ans = 1
    for _ in range(m):
        x, y = map(int, input().split())
        if red[x - 1]:
            if balls[x - 1] == 1:
                red[x - 1] = False
                ans -= 1
            if not red[y - 1]:
                red[y - 1] = True
                ans += 1
        balls[x - 1] -= 1
        balls[y - 1] += 1
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

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

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