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())
red = {}
ball = {}
ans = 1
for i in range(N):
    red[i+1] = 0
    ball[i+1] = 1
red[1] = 1
for i in range(M):
    x,y = map(int,input().split())
    if red[x]:
        if ball[x] == 1:
            red[x] -= 1
            ball[x] -= 1
            if red[y] == 0:
                red[y] += 1
                ball[y] += 1
            else:
                ball[y] += 1
                ans -= 1
        else:
            if red[y] == 0:
                ball[x] -= 1
                red[y] += 1
                ball[y] += 1
                ans += 1
            else:
                ball[x] -= 1
                ball[y] += 1
    else:
        ball[x] -= 1
        ball[y] += 1
            
print(ans)

Submission status: Accepted

Solution code:
# B - Box and Ball
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n,m=nm()
rb=[False]*n
rb[0]=True
ball=[1]*n
for i in range(m):
    x,y=nm()
    x-=1
    y-=1
    ball[x]-=1
    ball[y]+=1
    if rb[x]==True:
        rb[y]=True
    if ball[x]==0:
        rb[x]=False
ans=0
for r in rb:
    if r:
        ans+=1
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
import bisect
import collections
import itertools
import heapq
import string
import sys
sys.setrecursionlimit(10**9)
def gcd(a,b):return math.gcd #最大公約数
def lcm(a,b):return (a*b) // math.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i: divisors.append(n//i)
    return divisors
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True
def primes(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n + 1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def sieve_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N,M = imn()
xy = [iln() for _ in range(M)]
b = [1] * (N+1)
r_exist = [False] * (N+1)
r_exist[1] = True

for i in range(M):
    s = xy[i][0]
    d = xy[i][1] 
    b[s] -= 1
    b[d] += 1
    if r_exist[s]: r_exist[d] = True
    if b[s] == 0: r_exist[s] = False
ans = 0
for i in range(1, N+1):
    if r_exist[i]: ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
A = [1 for _ in range(N+1)]
B = [0 for _ in range(N+1)]
B[1] = 1
for _ in range(M):
    a,b = map(int,input().split())
    A[b] += 1
    if B[a]==1 and B[b]==0:
        B[b] = 1
    A[a] -= 1
    if B[a]==1 and A[a]==0:
        B[a] = 0
print(sum(B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
f=[1]*n
ans=[0]*n
ans[0]=1
for i in range(m):
  if ans[l[i][0]-1]==1:
    if f[l[i][0]-1]==1:
      ans[l[i][0]-1]=0
      ans[l[i][1]-1]=1
    else:
      ans[l[i][1]-1]=1
  f[l[i][0]-1]-=1
  f[l[i][1]-1]+=1
print(sum(ans))

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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

N,M=two_int()

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

dicts={0:1}
for i in range(M):
    A,B=two_int()
    boxes[A-1] -= 1
    boxes[B-1] += 1
    
    if A-1 in dicts:
        dicts[B-1]=1
        
        if boxes[A-1]==0:
            del dicts[A-1]

print(len(dicts))

Submission status: Accepted

Solution code:
# coding: utf-8
N, M = map(int, input().split())
b = [False] * N
l = [1] * N
b[0] = True
for i in range(M):
    x, y = map(int, input().split())
    if b[x-1] == True:
        b[y-1] = True
    if l[x-1] > 0:
        l[y-1] += 1
        l[x-1] -= 1
    if l[x-1] == 0:
        b[x-1] = False

ans = 0
for i in range(N):
    if b[i] == True and l[i] > 0:
        ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,M = map(int,readline().split())
m = map(int,read().split())
XY = zip(m,m)

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

for x,y in XY:
    if red[x]:
        red[y] = 1
    ball[x] -= 1; ball[y] += 1
    if not ball[x]:
        red[x] = 0

answer = sum(red)
print(answer)

Submission status: Accepted

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

def examB():
    N, M = LI()
    boal = [1]*N
    d = defaultdict(bool)
    d[0] = True
    for _ in range(M):
        x,y = LI()
        boal[x-1] -=1
        boal[y-1] +=1
        if d[x-1]:
            d[y-1] = True
        if boal[x-1]==0:
            d[x-1] = False
    ans = sum(d.values())
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
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()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

Submission status: Accepted

Solution code:
#x!=y
#赤があったことがあってボールが残っていれば行先も赤があるかも
#空なら赤いボールもない

n,m = map(int, input().split( ))
red = [0]*(n+1)
cnt = [1]*(n+1)
red[1] = 1
flag = False
for _ in range(m):
    x,y = map(int, input().split( ))
    if red[x] and cnt[x]:
        red[y] = 1
    cnt[x] -= 1
    cnt[y] += 1
    if not cnt[x]:
        red[x] = 0
ans = 0
for i in range(n+1):
    if red[i] and cnt[i]:
        ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

C = [1 for _ in range(N)]
R = [1] + [0 for _ in range(N-1)]
for x,y in XY:
  x -= 1
  y -= 1
  if R[x] == 1:
    R[y] = 1
    if C[x] == 1:
      R[x] = 0
  C[x] -= 1
  C[y] += 1
  
print(sum(R))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
balls = [1]*n #i番目の箱に入っているボールの数
red = [1] +[0]*(n-1) #i番目の箱に赤いボールが入っている可能性があるか否か
for i in range(m):
    x,y = map(int,input().split())
    x -= 1
    y -= 1 #0-indexedに直す
    if red[x] :red[y] = 1
    balls[x] -= 1
    balls[y] += 1
    if balls[x] == 0:
        red[x] = 0
print(sum(red))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
# status = [(1, 0)]  # 赤, 白の個数のステータス
# status.extend([(0, 1)]*(N-1))
status = [1]*N
candidate = [False]*N
candidate[0] = True
# print(status)
for _ in range(M):
    x, y = map(lambda x: int(x)-1, input().split())
    if candidate[x]:
        candidate[y] = True
    status[x] -= 1
    if status[x] == 0:
        candidate[x] = False
    status[y] += 1
print(sum(candidate))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

print(len(trace) - trace.count(0))

Submission status: Accepted

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

print(len(trace) - trace.count(0))

Submission status: Accepted

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

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

    if red[x] > 0:
        red[y] += 1
    else:
        white[y] += 1

    if white[x] > 0:
        white[x] -= 1
    else:
        red[x] -= 1


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

Submission status: Accepted

Solution code:
from sys import stdin

n,m = [int(x) for x in stdin.readline().rstrip().split()]
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(m)]

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

for i in li:
    ball[i[0]] -= 1
    ball[i[1]] += 1
    if red[i[0]] == 1:
        if ball[i[0]] == 0:
            red[i[0]] = 0
        red[i[1]] = 1

print(sum(red))

Submission status: Accepted

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

Submission status: Accepted

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

#赤フラグ 玉の個数
arr = [1 for _ in range(n)]
arr1 = [False for _ in range(n)]
arr1[0]=True
ch1 = True

for i in range(m):
    x,y = map(int,input().split())
    x = x-1
    y = y-1
    if ch1 == True and x == 0:
        ch1 = None
        arr[y] +=1
        arr1[y] = True
        arr[x] -=1
        if arr[x] == 0:
            arr1[x] = False
        continue
    
    arr[y] +=1
    arr[x] -=1
    if ch1 == None and arr1[x] == True:
        
        arr1[y] = True
        
    if ch1 == None and arr[x] == 0:
        arr1[x] = False

cnt=0
for i in range(n):
    if arr1[i] == True:
        cnt +=1   
print(cnt)

Submission status: Accepted

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

print(sum([flag for flag, _ in XY]))

Submission status: Accepted

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

Submission status: Accepted

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

cnt = [1] * n
r = [1] + [0] * (n-1)
for x, y in xy:
    cnt[x-1] -= 1
    cnt[y-1] += 1
       
    if r[x-1] == 1:
        r[y-1] = 1
        
    if cnt[x-1] == 0:
        r[x-1] = 0
    
print(sum(r))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def main():
    N, M = list(map(int, input().split()))
    XY = []
    for i in range(M):
        x, y = list(map(int, input().split()))
        XY.append([x, y])

    boxes = []
    for i in range(N):
        if i == 0:
            boxes.append([1, True])
        else:
            boxes.append([1, False])

    # print(':', boxes)

    for x, y in XY:
        if boxes[x-1][1]:
            # 赤ボールが運ばれた可能性がある
            boxes[y-1][1] = True
        if boxes[x-1][1] and boxes[x-1][0] == 1:
            # box(x-1)にボールがなくなるのでREDの可能性はない
            boxes[x-1][1] = False

        boxes[x-1][0] -= 1
        boxes[y-1][0] += 1
        # print(boxes)

    # print(':', boxes)
    ans = 0
    for c, b in boxes:
        if b:
            ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
n_list = [1 for i in range(N)]
bool_list = [True]
for i in range(N - 1):
    bool_list.append(False)

def move(n_bef, n_aft, bool_bef, bool_aft):
    n_bef -= 1
    n_aft += 1

    if bool_bef:
        bool_aft = True

        if n_bef == 0:
            bool_bef = False
    
    return [n_bef, n_aft, bool_bef, bool_aft]

for i in range(M):
    x, y = map(int, input().split())
    n_list[x - 1], n_list[y - 1], bool_list[x - 1], bool_list[y - 1] = move(n_list[x - 1], n_list[y - 1], bool_list[x - 1], bool_list[y - 1])

ans = 0
for bool_ in bool_list:
    if bool_:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
# 赤が入ってる可能性が0になる場合（すべての球を取り出した場合）があることに注意
n, m = map(int, input().split())
balls = [1 for _ in range(n)]
reddable = [False for _ in range(n)]
reddable[0] = True
for _ in range(m):
    x, y = map(lambda x: int(x) - 1, input().split())
    if reddable[x] == True:
        reddable[y] = True
        balls[x] -= 1
        balls[y] += 1
    else:
        balls[x] -= 1
        balls[y] += 1
    if balls[x] == 0:
        reddable[x] = False

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

Submission status: Accepted

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

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
isRed = [False] * N
isRed[0] = True
cnt = [1] * N
for i in range(M):
   cnt[y[i]]+=1
   if(isRed[x[i]]):
      isRed[y[i]] = True
   cnt[x[i]]-=1
   if(cnt[x[i]] == 0):
      isRed[x[i]] = False
ans = 0
for i in range(N):
   if(isRed[i]):
      ans+=1
print(ans)

Submission status: Accepted

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

red = [0 for _ in range(N)]
red[0] = 1
num = [1 for _ in range(N)]

for o in operation:
    num[o[0]-1] -= 1
    num[o[1]-1] += 1
    if red[o[0]-1]:
        if num[o[0]-1] == 0:
            red[o[0]-1] = 0
        red[o[1]-1] = 1
    
print(sum(red))

Submission status: Accepted

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

Submission status: Accepted

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

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())
    
    if(b[x]==1):
        b[y] = 1
    
    a[x]-=1
    a[y]+=1
    
    if(a[x]==0):
        b[x] = 0
    
cn = 0    

for i in range(1,n+1):
    if(b[i]==1 and a[i]>0):
        cn += 1
        
print(cn)

Submission status: Accepted

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

p = [0] * N
p[0] = 1
ball = [1] * N

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

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

print(ans)

Submission status: Accepted

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

for _ in range(M):
    x, y = map(int,input().split())
    if ball_num[x-1] == 1:
        if red[x-1] == 1:
            ball_num[x - 1] -= 1
            ball_num[y - 1] += 1
            red[x-1] = 0
            red[y-1] = 1
        else:
            ball_num[x-1] = 0
            ball_num[y-1] += 1
    else: #ボールが複数ある場合
        if red[x-1] == 1:
            ball_num[x-1] -= 1
            ball_num[y-1] += 1
            red[y-1] = 1
        else:
            ball_num[x-1] -= 1
            ball_num[y-1] += 1

print(sum(red))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
x = []
y = []
for i in range(M):
    x_i,y_i = map(int,input().split())
    x.append(x_i)
    y.append(y_i)

box_c = [1 for i in range(N+1)]
box_r = [0 for i in range(N+1)]
box_r[1] = 1


for i in range(M):
    box_c[x[i]] -= 1
    box_c[y[i]] += 1
    
    if box_r[x[i]]==1:
        box_r[y[i]] = 1
        if box_c[x[i]]==0:
            box_r[x[i]] = 0
   
print(box_r.count(1))

Submission status: Accepted

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

num_b = [1 for i in range(N+1)]
num_b[0] = 0
psb = [0]*(N+1)
psb[1] = 1
for i in range(M):
    num_b[X[i][0]] -= 1
    num_b[X[i][1]] += 1
    if psb[X[i][0]] == 1:
        psb[X[i][1]] = 1 
    if num_b[X[i][0]] == 0:
        psb[X[i][0]] = 0

print(psb.count(1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019-11-24 12:12:53(JST)
import sys

def main():
    n, m = map(int, sys.stdin.readline().split())
    xy = map(int, sys.stdin.read().split())
    xy = zip(xy, xy)

    count = dict((i, 1) for i in range(1, n+1))
    may_contain_red = set([1])

    for x, y in xy:
        count[x] -= 1; count[y] += 1
        if x in may_contain_red:
            may_contain_red.add(y)
            if count[x] == 0:
                may_contain_red ^= set([x])
    
    ans = len(may_contain_red)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 2019-11-24 12:12:53(JST)
import sys
from collections import deque

def main():
    n, m = map(int, sys.stdin.readline().split())
    xy = map(int, sys.stdin.read().split())
    xy = zip(xy, xy)


    count = dict((i, 1) for i in range(1, n+1))
    may_contain_red = set([1])

    for x, y in xy:
        count[x] -= 1; count[y] += 1
        if x in may_contain_red:
            may_contain_red.add(y)
            if count[x] == 0:
                may_contain_red -= set([x])
    
    ans = len(may_contain_red)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(M)]
s = [1] * N
a = [0] * N
a[0] = 1
for i in range(M):
    s[x[i][0] - 1] -= 1
    if a[x[i][0] - 1] == 1:
        a[x[i][1] - 1] = 1
        if s[x[i][0] - 1] == 0:
            a[x[i][0] - 1] = 0
    s[x[i][1] - 1] += 1


print(sum(a))

Submission status: Accepted

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

#print(transferlist)

box = [[1,0] for i in range(N)] #ボールの数、1つ赤い球があるか
box[0][1] = 1

for x,y in transferlist:
    box[x][0] -=1
    box[y][0] += 1
    if box[x][1] == 1:
        box[y][1] = 1
    if box[x][0] == 0:
        box[x][1] = 0

ans = sum(i[1] for i in box)
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

b = [1] * N
b[0] = -1
for i in range(M):
  x, y = map(int, input().split())
  if b[x - 1] < 0:
    b[x - 1] += 1
    b[y - 1] = -(abs(b[y - 1]) + 1)
  else:
    b[x - 1] -= 1
    if b[y - 1] < 0:
      b[y - 1] -= 1
    else:
      b[y - 1] += 1
print(len([x for x in b if x < 0]))

Submission status: Accepted

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

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

for i in range(M):
    X,Y=map(int,input().split())
    if(L[X][0]!=0):
        L[X][0]-=1
        L[Y][0]+=1
        if(L[X][1]!=0):
            L[Y][1]=1
            if(L[X][0]==0):
                 L[X][1]=0

t=0
for i in range(N+1):
    t+=L[i][1]
print(t)

Submission status: Accepted

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

box = [1 for i in range(n)]
red = [0 if i != 0 else 1 for i in range(n)]

for xy in move:
    x,y = xy

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

    if red[x-1] is 1: red[y-1] = 1
    if box[x-1] is 0: red[x-1] = 0

print(sum(red))

Submission status: Accepted

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

c = [1] * n
bl = [False] * n
bl[0] = True

for _ in range(m):
    x, y = (int(j) - 1 for j in input().split())
    c[x] -= 1
    c[y] += 1
    if bl[x]:
        if c[x] == 0:
            bl[x] = False
        bl[y] = True
    # print(x, y, c, bl)
print(bl.count(True))

Submission status: Accepted

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

Submission status: Accepted

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

N, M = map(int, input().split())
m = [list(map(int, input().split())) for _ in range(M)]
b = [1] * N
p = [0] * (N + 1)
p[1] = 1
for move in m:
  b[move[0] - 1] -= 1
  b[move[1] - 1] += 1
  p[move[1]] += p[move[0]]
  if b[move[0] - 1] == 0:
    p[move[0]] = 0
res = 0
for i in range(N):
  if p[i + 1] > 0 and b[i] > 0:
    res += 1
print(res)

Submission status: Accepted

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

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

for _ in range(M):
    x, y = list(map(int, input().split()))
    xy_table.append((x, y))

red_box_dict = {i: False for i in range(1, N + 1)}
red_box_dict[1] = True
# 添字0はダミー
# 添字 = 箱の番号として扱う
n_box_list = [0] + [1] * N

for xy in xy_table:
    x, y = xy
    n_box_list[x] -= 1
    n_box_list[y] += 1
    if red_box_dict[x]:
        red_box_dict[y] = True

        if n_box_list[x] == 0:
            red_box_dict[x] = False

ans = len([v for v in red_box_dict.values() if v])

print(ans)

Submission status: Accepted

Solution code:
import collections
N, M = map(int, input().split())
xy = [list(map(int, input().split())) for i in range(M)]
table = collections.defaultdict(int)
table2 = collections.defaultdict(int)
table2[1] += 1

for i in range(N):
    table[i+1] = 1
reached = [1]
for i in range(M):
    table[xy[i][1]] += 1
    table[xy[i][0]] -= 1
    if xy[i][0] in table2 and table2[xy[i][0]] > 0:
        table2[xy[i][0]] = table[xy[i][0]]
        table2[xy[i][1]] += table[xy[i][1]] + 1

table2 = list(table2.items())
table2 = [x for x in table2 if x[1] > 0]
print(len(table2))

Submission status: Accepted

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

Submission status: Accepted

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

N, M = map(int, readline().split())
xy = map(int, read().split())

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

for x, y in zip(xy, xy):
    if red[x]:
        red[y] = True

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

    if ball[x] == 0:
        red[x] = False

print(sum(red[1:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:


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

lis = [False] * N
num = [1] * N
lis[0] = True

for i in range(M):

    x,y = map(int,input().split())

    x -= 1
    y -= 1

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

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

    if num[x] == 0:
        lis[x] = False

ans = 0

for i in range(N):
    if lis[i]:
        ans += 1

print (ans)

Submission status: Accepted

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

red = [True] + [False] * (N - 1)
total = [1] + [1] * (N - 1)
ans = 1
for i in range(M):
    xi, yi = map(int, input().split())
    if red[xi - 1]:
        red[yi - 1] = True
    total[xi - 1] -= 1
    total[yi - 1] += 1
    if total[xi - 1] == 0:
        red[xi - 1] = False
print(red.count(True))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
XY=[list(map(int,input().split())) for i in range(M)]
borldata=[1 for i in range(N)]
akaidxlist=[0 for i in range(N)]
akaidxlist[0]=1
for i in XY:
    x,y=i
    x,y=x-1,y-1
    if akaidxlist[x]==1:
        akaidxlist[y]=1
        if borldata[x]==1:akaidxlist[x]=0
    borldata[x]-=1
    borldata[y]+=1
print(len([i for i in akaidxlist if i]))