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())
boxes = [[0, 1] for _ in range(n)]
boxes[0][0] = 1
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    boxes[x][1] -= 1
    boxes[y][1] += 1
    if boxes[x][0] == 1:
        boxes[y][0] = 1
    else:
        if boxes[y][0] == 1:
            continue
        else:
            boxes[y][0] = 0  
    if boxes[x][1] == 0:
        boxes[x][0] = -1
ans = 0
for a, b in boxes:
    # print(a, b)
    if a > 0:
        ans += 1
# print(boxes)
print(ans)

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))
reds = [False]*n
reds[0] = True
balls = [1]*n
for x, y in xy:
    if reds[x-1] and balls[x-1]>1:
        reds[y-1] = True
    elif reds[x-1]:
        reds[y-1] = True
        reds[x-1] = False
    balls[x-1] -= 1
    balls[y-1] += 1
print(sum(reds))

Submission status: Accepted

Solution code:
import sys
from collections import Counter
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, M = map(int, readline().split())
    R = [0]*(N)
    W = [1]*(N)
    R[0] = 1
    W[0] = 0
    for _ in range(M):
        x, y = map(int, readline().split())
        if R[x-1]==1 and W[x-1]>=1:
            W[x-1] -= 1
            if R[y-1]==0:
                R[y-1]+=1
            else:
                W[y-1]+=1
        elif R[x-1]==0 and W[x-1]>=1:
            W[x-1] -= 1
            W[y-1] += 1
        else:
            R[x-1] -= 1
            R[y-1] += 1
    ans = 0
    for i in range(N):
        if R[i]>=1:
            ans += 1
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===
def main():
    n, m = ns()

    table_TF = [False for _ in range(n)]
    table_TF[0] = True

    table_cnt = [1 for _ in range(n)]

    for _ in range(m):
        x, y = ns()
        x -= 1
        y -= 1

        table_cnt[x] -= 1
        table_cnt[y] += 1
        if table_TF[x]:
            table_TF[y] = True
        if table_cnt[x] == 0:
            table_TF[x] = False

    ans = 0
    for tf in table_TF:
        if tf:
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def mov(xy,N):
    Box_red = set([1])
    Box = [1 for _ in range(N)]

    for i in range(len(xy)):
        Box[xy[i][0]-1] -= 1
        Box[xy[i][1]-1] += 1
        if xy[i][0] in Box_red:
            Box_red.add(xy[i][1])
        if Box[xy[i][0]-1] == 0:
            Box_red.discard(xy[i][0])

    return len(Box_red)

def main():
    N,M = map(int,input().split())
    xy = [list(map(int,input().split())) for _ in range(M)]
    ans = mov(xy,N)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    n, m = MI()

    B = [1] * n
    J = [False] * n
    J[0] = True

    for i in range(m):
        x, y = MI1()

        if J[x] == True:
            if B[x] == 1:
                J[x] = False
            B[x] -= 1
            J[y] = True
            B[y] += 1
        else:
            if B[x] > 0:
                B[x] -= 1
                B[y] += 1
    print(sum(J))





if __name__ == '__main__':
    solve()

Submission status: Accepted

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    N, M = readnums()
    xy = [tuple(readnums()) for _ in range(M)]
    dic = {i: 1 for i in range(1, N + 1)}
    r = {1}
    for (x, y) in xy:
        dic[x] -= 1
        dic[y] += 1
        if x in r:
            r.add(y)
            if not dic[x]:
                r.remove(x)

    print(len(r))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
kosu = [1 for _ in range(n)]
aka = [0 for _ in range(n)]
aka[0] += 1
for i in range(m):
  a,b = map(int,input().split())
  a -= 1
  b -= 1
  if aka[a]==1:
    #論理和
    aka[b] |= 1
    if kosu[a]<2:
      aka[a] -=1
  kosu[a] -=1
  kosu[b] += 1
 # print("kosu",kosu)
  #print("aka",aka)
print(aka.count(1))

Submission status: Accepted

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

red_boxes = [False for i in range(n)]
red_boxes[0] = True
n_ball = [1 for i in range(n)]


for i in range(m):
    x_i = xy[i][0]-1
    y_i = xy[i][1]-1
    
    if n_ball[x_i] == 1:
        if red_boxes[x_i] == True:
            n_ball[x_i] -= 1
            n_ball[y_i] += 1
            red_boxes[x_i] = False
            red_boxes[y_i] = True
        
        else:
            n_ball[x_i] -= 1
            n_ball[y_i] += 1
    
    else:
        if red_boxes[x_i] == True:
            n_ball[x_i] -= 1
            n_ball[y_i] += 1
            red_boxes[y_i] = True
        
        else:
            n_ball[x_i] -= 1
            n_ball[y_i] += 1
    
    
    
print(red_boxes.count(True))

Submission status: Accepted

Solution code:
# diff 693
n, m = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(m)]
# indに気を付ける
ans = 0

# judについて0:red 1:white
jud = [0] + [1] * (n - 1)
num = [1] * n

for i in xy:
    if num[i[0] - 1] >= 2 and jud[i[0] - 1] == 0:
        jud[i[1] - 1] = 0
        num[i[0] - 1] -= 1
        num[i[1] - 1] += 1
    elif num[i[0] - 1] < 2 and jud[i[0] - 1] == 0:
        jud[i[1] - 1] = 0
        jud[i[0] - 1] = 1
        num[i[0] - 1] -= 1
        num[i[1] - 1] += 1
    else:
        num[i[0] - 1] -= 1
        num[i[1] - 1] += 1

print(jud.count(0))

Submission status: Accepted

Solution code:
boxnum, sousanum = map(int, input().split(" "))
sousa = []
for i in range(sousanum):
  sousa.append(list(map(int, input().split(" "))))

#aka[0][0] ...[ボール個数][赤フラグ]
ball = [[1, 0] for i in range(boxnum)]
ball[0][1] = 1
for i in range(sousanum):
  if ball[sousa[i][0] - 1][1] == 1:#赤色あり
    ball[sousa[i][1] - 1][1] = 1
    
  ball[sousa[i][0] - 1][0] -= 1
  ball[sousa[i][1] - 1][0] += 1
  
  if ball[sousa[i][0] - 1][0] == 0:#ボール尽きた
    ball[sousa[i][0] - 1][1] = 0
total = 0
for i in range(boxnum):
    total += ball[i][1]
print(total)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
counts = [1] * n
cands = [False] * n
cands[0] = True
for _ in range(m):
  x, y = map(lambda t: int(t) - 1, input().split())
  counts[x] -= 1
  counts[y] += 1
  if cands[x]:
    cands[y] = True
    if counts[x] == 0:
      cands[x] = False
print(sum(cands))

Submission status: Accepted

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

Submission status: Accepted

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

ans = [1] * (n)
ans[0] = 1
ball = [0] * (n)
ball[0] = 1
for i in range(m):
    x, y = xy[i]
    ans[y-1] += 1
    ans[x-1] -= 1
    if ball[x-1]:
      ball[y-1] = 1
    if ans[x-1] == 0:
      ball[x-1] = 0
      
print(sum(ball))

Submission status: Accepted

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

ans = [1] * (n)
ans[0] = 1
ball = [False] * (n)
ball[0] = True
for i in range(m):
    x, y = xy[i]
    ans[y-1] += 1
    ans[x-1] -= 1
    if ball[x-1]:
      ball[y-1] = True
    if ans[x-1] == 0:
      ball[x-1] = False
      
cnt = 0
for i in ball:
  if i:
    cnt += 1

print(cnt)

Submission status: Accepted

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

print(c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N, M = map(int, sys.stdin.readline().rstrip().split())

opes = []
for _ in range(M):
    x, y = map(int, sys.stdin.readline().rstrip().split())
    opes.append((x-1, y-1))

transported = set()
transported.add(0)
boxes = [1 for _ in range(N)]

for (x, y) in opes:
    boxes[x] -= 1
    boxes[y] += 1
    if x in transported:
        transported.add(y)
    if boxes[x] == 0 and x in transported:
        transported.remove(x)

print(len(transported))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

A = [1] * (n + 1)
red = [False] * (n + 1)
red[1] = True

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

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

Submission status: Accepted

Solution code:
def main():
    N, M = (int(_) for _ in input().split())
    haco = [1] * (N+1)
    # 0:空, 1:白のみ取り出せる, 2:赤のみ, 3:どっちもあり得る
    cc = [1] * (N+1)
    haco[1] = 2
    for i in range(M):
        x, y = (int(_) for _ in input().split())
        cc[x] -= 1
        cc[y] += 1
        if haco[x] == 1:
            haco[y] = 3 if haco[y] in (2, 3) else 1
        elif haco[x] == 2:
            haco[y] = 2 if haco[y] == 0 else 3
        else:
            haco[y] = 3
        if cc[x] == 0: haco[x] = 0
    output = 0
    for i in range(1, N+1):
        if haco[i] in (2, 3):
            output += 1
    print(output)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ball_count = [1 for _ in range(N)]
if_red_exists = [1] + [0 for _ in range(N-1)]
count = 1
for _ in range(M):
    x, y = map(int, input().split())
    if if_red_exists[x-1]:
        # 赤が存在する場合
        if_red_exists[y-1] = 1
        if ball_count[x-1] <= 1:
            if_red_exists[x-1] = 0
    ball_count[x-1] -= 1
    ball_count[y-1] += 1

count = 0
for e in if_red_exists:
    if e == 1:
        count += 1
print(count)

Submission status: Accepted

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


def main():
    N, M = map(int, input().split())
    red = [0] * N
    red[0] = 1
    num = [1] * N

    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
    print(sum(red))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = [1] * n
d = set()
d.add(1)

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

s = 0
for i in d:
    if(l[i-1] != 0):s += 1
print(s)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N,M = map(int,input().split())
    possible = [0] * N
    possible[0] = 1
    ball_cnt = [1] * N
    for _ in range(M):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        if possible[x]:
            possible[y] = 1
            if ball_cnt[x] == 1:
                possible[x] = 0
        ball_cnt[x] -= 1
        ball_cnt[y] += 1
    ans = sum((b > 0)*p for b,p in zip(ball_cnt,possible))
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[]
for _ in range(m):
    l.append(list(map(int,input().split())))
a=[0]*(n+1)
a[1]=1
c=[1]*(n+1)
for i in range(m):
    c[l[i][0]]-=1
    c[l[i][1]]+=1
    if a[l[i][0]]:
        a[l[i][1]]=1
    if c[l[i][0]]==0:
        a[l[i][0]]=0
print(a.count(1))

Submission status: Accepted

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

n, m = map(int, input().split())
x = [0]* n
y = [0]* n

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

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

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

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

print(ans)

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):
        x[i],y[i]=map(int,readline().split())

    box=[1]*(N+1)
    flags=[False]*(N+1)
    flags[1]=True

    for i in range(M):
        out=x[i]
        inn=y[i]
        if flags[out]==True and box[out]==1:
            flags[out]=False
            flags[inn]=True
        elif flags[out]==True:
            flags[inn]=True
        box[out]-=1
        box[inn]+=1

    res=0
    for i in range(N+1):
        if flags[i]==True:
            res+=1
    print(res)
        
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:

n,m = list(map(int, input().split()))
manu = []

for i in range(m):
    manu.append(list(map(int, input().split())))

num = [1 for i in range(n+1)]
poss = [0 for i in range(n+1)]
poss[1] = 1

for i in range(m):
    a,b = manu[i][0],manu[i][1]
    if(poss[a]==1):
        poss[b]=1
    num[a]-=1
    num[b]+=1
    if(num[a]==0):
        poss[a]=0

print(sum(poss))

Submission status: Accepted

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


def main():
    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())

    ball = [1] * N
    exist = [0] * N
    exist[0] = 1
    for a, b in zip(x, y):
        ball[a-1] -= 1
        ball[b-1] += 1
        if exist[a-1] == 1:
            exist[a-1] = 1 if ball[a-1] > 0 else 0
            exist[b-1] = 1

    ans = sum(exist)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np

def transfer(state, x, y):
    state[x][0] -= 1
    state[y][0] += 1
    if state[x][0] == 0:
        if state[x][1] == 1:
            state[x][1] = 0
            state[y][1] = 1
    else:
        if state[x][1] == 1:
            state[y][1] = 1
            
n, m = map(int, input().split())

state = [[1, 0] for _ in range(n)]
state[0][1] = 1
for i in range(m):
    x, y = map(lambda x: int(x) - 1, input().split())
    transfer(state, x, y)

print(np.sum(state, axis=0)[1])

Submission status: Accepted

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

balls = [1 for _ in range(n)]
red = [1] + [0 for _ in range(1,n)]

for _ in range(m):
  x, y = map(int,input().split())
  x,y = x-1, y-1
  
  if balls[x] ==0:
    continue
    
  balls[x] -= 1
  balls[y] += 1
  if red[x]:
    red[y] = 1
  if balls[x]==0:
    red[x] = 0
print(sum(red))

Submission status: Accepted

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

Ball = [1] * n
Done = [False] * n
Done[0] = True

for i in range(m):
	x, y = XY[i]
	x -= 1
	y -= 1
	if Done[x]:
		Done[y] = True
	
	Ball[x] -= 1
	Ball[y] += 1
	
	if Ball[x] == 0:
		Done[x] = False
				
res = 0
for i in range(n):
	if Done[i]:
		res += 1

print(res)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, m = map(int, readline().split())
xy = [list(map(int, readline().split())) for _ in range(m)]
flag = [False] * n
flag[0] = True
cnt = [1] * n
for x, y in xy:
    if flag[x - 1]:
        flag[y - 1] = True
    cnt[x - 1] -= 1
    cnt[y - 1] += 1
    if cnt[x - 1] == 0:
        flag[x - 1] = False
print(flag.count(True))

Submission status: Accepted

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

possibility=[False]*n
possibility[0]=True
count=[1]*n

for x,y in a:
    if possibility[x-1]==True and count[x-1]>1:
        possibility[y-1]=True
    elif possibility[x-1]==True and count[x-1]==1:
        possibility[x-1]=False
        possibility[y-1]=True
    count[x-1]-=1
    count[y-1]+=1

print(possibility.count(True))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ball = [1] * n  # 各箱のボールの個数。初めは各箱に１つずつ。
red = set([0])  # 赤いボールがある可能性のある箱のインデックス。初めは0番目の箱に赤いボールがある。
for _ in range(m):
  x, y = map(int, input().split())
  ball[x-1] -= 1  # x-1番目の箱のボールを1つ減らす
  ball[y-1] += 1  # y-1番目の箱のボールを1つ増やす
  if x-1 in red:  # x-1番目の箱に赤いボールがあるなら、y-1番目の箱に移動したとする。
  	red.add(y-1)
  if ball[x-1] == 0:  # x-1番目の箱にボールがなくなったら、赤いボールもないはずなので
    red.discard(x-1)  # 可能性のある箱から削除しとく
print(len(red))  # 可能性のある箱の個数を出力

Submission status: Accepted

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

Submission status: Accepted

Solution code:
int1 = lambda x: int(x) - 1

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

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

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

print(sum(red))

Submission status: Accepted

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

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

ans=0

for chk in check:
    if chk:
        ans+=1

print(ans)

Submission status: Accepted

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

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

print(check.count(True))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,m=map(int,input().split())
    lis=[1]*n
    rlis=[False]*n
    rlis[0]=True
    for i in range(m):
        x,y=map(int,input().split())
        x-=1
        y-=1
        if lis[x]==1:
            lis[x]-=1
            lis[y]+=1
            if rlis[x]:
                rlis[x]=False
                rlis[y]=True
        else:
            lis[x]-=1
            lis[y]+=1
            if rlis[x]:
                rlis[y]=True
    print(rlis.count(True))


if __name__=='__main__':
    main()

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 heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
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 = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
L = [LIST() for i in range(M)]

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

for a, b in L:
    if num[a-1] == 0:
        # 水がない
        red[a-1] = False
        continue
    else:
        num[b-1] += 1
        num[a-1] -= 1
        red[b-1] = red[b-1] or red[a-1]
        if num[a-1] == 0:
            red[a-1] = False
    
print(sum(red))
# print(sum(num))

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 itertools import combinations # (string,3) 3回
#from collections import deque
#import collections.defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

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

def readInts():
  return list(map(int,input().split()))
def main():
    n,m = readInts()
    # 数が入っているのと、
    # 箱の中が0かどうかを判別するものを定義する
    # 全部まず1個入ってる
    # 赤は最初だけ!!!!!!!!
    nums = [1] * n
    In = [False] * n
    In[0] = True
    for i in range(m):
        a,b = map(lambda x:int(x) - 1, input().split())
        nums[a] -=1
        nums[b] +=1
        if In[a]:
            In[b] = True
        if nums[a] == 0:
            In[a] = False
    cnt = 0
    #print(In)
    for i in range(n):
        if In[i]:
            cnt += 1
    print(cnt)
if __name__ == '__main__':
  main()

Submission status: Accepted

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

moves = [list(map(int,input().split())) for i in range(M)]
isRed = [False for i in range(N)]
isRed[0] = True
Ball = [1 for i in range(N)]

for i in range(M):
    p = moves[i][0] - 1
    q = moves[i][1] - 1

    if isRed[p] == True:
        isRed[q] = True
        Ball[p] -= 1
        Ball[q] += 1
        if Ball[p] < 1:
            isRed[p] = False
        
    else:
        Ball[p] -= 1
        Ball[q] += 1
    
ans = isRed.count(True)

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
all_li = []
for _ in range(m):
    li = list(map(int,input().split()))
    all_li.append(li)
#print(all_li)
flag_memo = [False]*n
flag_memo[0] = True
#print(flag_memo)
kazu_li = [1]*n
#print(kazu_li)

for a in all_li:
    kazu_li[a[0]-1] -= 1
    kazu_li[a[1]-1] += 1
    if flag_memo[a[0]-1]:
        flag_memo[a[1]-1] = True
    if kazu_li[a[0]-1] == 0:
        flag_memo[a[0]-1] = False
print(flag_memo.count(True))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

ball_num = [1] * N
is_red = [False] * N
is_red[0] = True

for i in range(M):
    s = XY[i][0]
    g = XY[i][1]
    if is_red[s]:
        if ball_num[s] == 1:
            ball_num[s] -= 1
            ball_num[g] += 1
            is_red[s] = False
            is_red[g] = True
        else:
            ball_num[s] -= 1
            ball_num[g] += 1
            is_red[g] = True
    else:
        ball_num[s] -= 1
        ball_num[g] += 1

print(sum(is_red))

Submission status: Accepted

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

num_l=[1 for i in range(n)]
bit_l=[1]+[0 for i in range(n-1)]

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

  if bit_l[x]==1:
    bit_l[y]=1
    if num_l[x]==1:
      bit_l[x]=0
  num_l[x]-=1
  num_l[y]+=1

print(bit_l.count(1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = (int(x) for x in input().split())
ab = []
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a-1, b-1])

box = [1]*n

red = [0]*n

red[0] = 1

for x,y in ab:
    if 0 < red[x]:
        red[y] = 1
    box[x] -= 1
    box[y] += 1

    if box[x] == 0:
        red[x] = 0
print(sum(red))

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:
        ans.add(y)
        if cnt[x] == 0: # 絶対に赤いボールがyに移される
            ans.remove(x)
 
print(len(ans))

Submission status: Accepted

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

prob=[0]*N
prob[0]=1
num=[1]*N
num[0]=1

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n,m=map(int, input().split())
v = [0]*n
v[0]=1
ans = [1]*n
fi = 1
for i in range(m):
    a,b=map(int, input().split())
    ans[a-1]-=1
    ans[b-1]+=1
    if v[a-1]:
        v[b-1]=1
        fi+=1
    if ans[a-1]==0:
        v[a-1]=0
        fi-=1

print(sum(v))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
B = [list(map(int,input().split())) for k in range(M)]
H = [1]*N
E = [0]*N
E[0] = 1
for e in B:
    x, y = e[0]-1, e[1]-1
    if E[x] == 1:
        if H[x] == 1:
            E[x] = 0
            E[y] = 1
        else:
            E[y] = 1
    H[x] -= 1
    H[y] += 1
print(E.count(1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
xy = [[int(i) for i in input().split()] for _ in range(m)]
num = [1]*n
red = [False]*n
red[0] = True
flag = True
for i in range(m):
    p = xy[i][0]
    q = xy[i][1]
    num[p-1] -= 1
    num[q-1] += 1
    if red[p-1]==True:
        red[q-1] = True
    
    if num[p-1]==0:
        red[p-1] = False
        

count = 0
for i in range(n):
    if num[i]>0 and red[i]:
        count += 1
print(count)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(M)]
red = [0] * (N+1) #赤になりうる場所を１とする
red[1] = 1
red[0] = ""
ball = [1] * (N+1) #個数
ball[0] = ""
for x, y in xy:
  ball[y] += 1
  ball[x] -= 1
  if red[x] == 1:
    red[y] = 1
    if ball[x] == 0: red[x] = 0
print(red.count(1))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():

    N,M=map(int,input().split())
    X, Y = [0] * M, [0] * M
    for i in range(M):
        X[i], Y[i] = map(int, input().split())

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

    for i in range(M):
        cnt[X[i]-1]-=1
        cnt[Y[i]-1]+=1
        if red[X[i]-1]:
            red[Y[i]-1]=True
        if cnt[X[i]-1]==0:
            red[X[i] - 1]=False

    print(red.count(True))


resolve()