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:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

n,m = getNM()

balls = [[1,0] for i in range(n)]
balls[0] = [1,1]
for i in range(m):
    x,y = getNM()
    x -= 1
    y -= 1
    balls[y][0] += 1
    if balls[x][1] == 1:
        balls[y][1] = 1
    balls[x][0] -= 1
    if balls[x][0] == 0:
        balls[x][1] = 0

print(sum([b[1] for b in balls]))

Submission status: Accepted

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

for i in range(m):
  here,next = map(int,input().split())
  next -=1
  here -=1
  
  if possible[here]:
    possible[next]=True
    if num_ball[here] == 1:
      possible[here]=False
  else:
    if num_ball[here] == 1:
      possible[here]=False
      
  num_ball[here] -=1
  num_ball[next] +=1
  
print(possible.count(True))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = list(list(map(int,input().split())) for i in range(M))

A = [1 for i in range(N+1)]

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

ans = 1
for i in range(M) :
    A[L[i][0]] -= 1
    A[L[i][1]] += 1
    if red[L[i][0]] == 1 :
        red[L[i][1]] = 1
    if A[L[i][0]] == 0 :
        red[L[i][0]] = 0
        
ans = sum(red)
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**8) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,m = li()
ball = [1]*n
inred = [0]*n
inred[0] = 1

for _ in range(m):
    x,y = li_()
    if inred[x] and ball[x] == 1:
        inred[x] = 0
        ball[x] = 0
        
        inred[y] = 1
        ball[y] += 1
        
    elif inred[x] and ball[x] > 1:
        ball[x] -= 1
        
        inred[y] = 1
        ball[y] += 1
        
    else:
        ball[x] -= 1
        ball[y] += 1
        
ans = 0
for ri in inred:
    ans += ri
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**8) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,m = li()
ball = [1]*n
inred = [0]*n
inred[0] = 1

for _ in range(m):
    x,y = li_()
    if inred[x] and ball[x] == 1:
        inred[x] = 0
        ball[x] = 0
        
        inred[y] = 1
        ball[y] += 1
        
    elif inred[x] and ball[x] > 1:
        ball[x] -= 1
        
        inred[y] = 1
        ball[y] += 1
        
    else:
        ball[x] -= 1
        ball[y] += 1
        
ans = 0
for ri in inred:
    ans += ri
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(M)]
red = [0] * N
red[0] = 1
ballnum = [1] * N

for a,b in src:
    a,b = a-1,b-1
    ballnum[b] += 1
    ballnum[a] -= 1
    if red[a]:
        red[b] = 1
        if not ballnum[a]:
            red[a] = 0
print(sum(red))

Submission status: Accepted

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

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

box = [1] * n
color = [0] * n
color[0] = 1

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

print(sum(color))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
c = []
for i in range(m):#h:高さ
    c.append([int(m) for m in input().split()])
ball = []
for i in range(n + 1):
    if i == 1:
        ball.append([1, 1])
    else:
        ball.append([0, 1])
for i in range(m):
    if ball[c[i][0]][0] == 0:#赤ボールがないとき
        ball[c[i][0]][1] -= 1
        ball[c[i][1]][1] += 1
    elif ball[c[i][0]][1] == 1:#赤ボールがひとつ，白が0のとき
        ball[c[i][0]][1] -= 1
        ball[c[i][1]][1] += 1
        ball[c[i][1]][0] = 1
        ball[c[i][0]][0] -= 1
    else:#白がひとつ以上のとき
        ball[c[i][0]][1] -= 1
        ball[c[i][1]][1] += 1
        ball[c[i][1]][0] = 1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

for x, y in XY:
    num[x] -= 1
    num[y] += 1
    if red[x] == True:
        red[y] = True
        if num[x] == 0:
            red[x] = False
print(sum(red))

# http://agc002.contest.atcoder.jp/data/agc/002/editorial.pdf

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
xy = []
for _ in range(M):
    xy.append(list(map(int,input().split())))
    xy[-1][0] -= 1
    xy[-1][1] -= 1
redp = [[False,1] for _ in range(N)]
redp[0] = [True,1]
for x,y in xy:
    if redp[x][0]:
        if redp[x][1] == 1:
            redp[x][0] = False
            redp[x][1] = 0
            redp[y][0] = True
            redp[y][1] += 1
        else:
            redp[x][1] -= 1
            redp[y][0] = True
            redp[y][1] += 1
    else:
        redp[x][1] -= 1
        redp[y][1] += 1


count = 0
for i in redp:
    if i[0]:
        count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

num = [1]*n
r = [False]*n
r[0] = True

for i in range(m):
    if r[x[i]] == True:
        r[y[i]] = True
    num[x[i]] -= 1
    num[y[i]] += 1
    
    if num[x[i]] == 0:
        r[x[i]] = False

print(r.count(True))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]

N, M = inpl()
A = [1 for _ in range(N)]
B = [False for _ in range(N)]
B[0] = True
for _ in range(M):
    x, y = [int(i)-1 for i in input().split()]
    if B[x]:
        B[y] = True
    if A[x] == 1:
        B[x] = False
    A[x] -= 1
    A[y] += 1
print(sum(B))

Submission status: Accepted

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

ball_list = [1 for i in range(N)]
flag_list = [False for i in range(N)]
flag_list[0] = True

for i in range(M):
    x,y = list(map(int,input().split()))
    if flag_list[x-1]:
        flag_list[y-1]=True
        if ball_list[x-1]==1:
            flag_list[x-1]=False
    ball_list[x-1]-=1
    ball_list[y-1]+=1
count=0    
for i in range(N):
    if flag_list[i]:
        count+=1

print(count)

Submission status: Accepted

Solution code:

import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [long(input_raw[0]), long(input_raw[1])]
    is_red = [False for i in range(N+1)]
    is_red[1] = True
    ball_num = [1 for i in range(N+1)]
    for i in range(M):
        input_raw = read_func().strip().split()
        [x, y] = [long(input_raw[0]), long(input_raw[1])]
        ball_num[x] -= 1
        ball_num[y] += 1
        if is_red[x] == True:
            is_red[y] = True
        if ball_num[x] == 0:
            is_red[x] = False

    print sum(is_red)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M = (int(i) for i in input().split())  

class Box:
      def __init__(self):
         self.ball = 1
         self.pos = False
box = [] 
for i in range(N):
    box.append(Box())
box[0].pos = True

for i in range(M):
    x,y = (int(i) for i in input().split())  
    if box[x - 1].pos:
       box[y - 1].pos = True
    box[x - 1].ball -= 1
    box[y - 1].ball += 1
    if box[x - 1].ball == 0:
       box[x - 1].pos = False
cnt = 0;
for i in range(N):
   if box[i].pos:
      cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,m = map(int, input().split())
s = set([1])
d = {}
d[1] = 1

for _ in range(m):
    x,y = map(int, input().split())
    if x not in d:
        d[x] = 0
    else:
        d[x] -= 1
    if y not in d:
        d[y] = 2
    else:
        d[y] += 1
    if x in s:
        if x in d and d[x]==0:
            s.remove(x)
        s.add(y)
# print(d)
# print(s)
print(len(s))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def operation(box:list, frm:int, to:int):
    box[frm] -= 1
    box[to] += 1
    
n,m = li()
box = [1 for _ in range(n)]
cand = [False for _ in range(n)]
cand[0] = True
ope = []
for _ in range(m):
    x,y = li_()
    ope.append((x,y))
    
for x,y in ope:
    operation(box,x,y)
    if (not cand[y]) and cand[x]:
        cand[y] = True
    
    if cand[x] and box[x] > 0:
        cand[x] = True
        
    elif cand[x] and box[x] == 0:
        cand[x] = False
        
print(sum(cand))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
grid = []
for i in range(M):
    array = list(map(int, input().strip().split(' ')))
    grid.append(array)
nums = []
judge = []
while len(nums)<N:
  nums.append(1)
  judge.append(False)
judge[0] = True
for arrays in grid:
  if judge[arrays[0]-1] == True:
    judge[arrays[1]-1] = True
  nums[arrays[0]-1] += -1
  nums[arrays[1]-1] += 1
  if nums[arrays[0]-1]==0:
    judge[arrays[0]-1] = False
ans = 0
for states in judge:
  if states == True:
    ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Q = [] # 1-index
for i in range(M):
  Q.append(list(map(int,input().split())))

Red = [0] * N # 0-index
Count = [1] * N # 0-index

Red[0] = 1

for i in range(M):
  x, y = Q[i]
  x, y = x-1, y-1 # 0-1_index
  Count[x] -= 1
  Count[y] += 1
  if Red[x] == 1:
    Red[y] = 1
  if Count[x] == 0:
    Red[x] = 0
    
print(sum(Red))

Submission status: Accepted

Solution code:
i=lambda:map(int,input().split());n,m=i();a=[0]+[1]*n;b=[0,1]+[0]*n
for _ in[0]*m:x,y=i();a[x]-=1;a[y]+=1;b[y]|=b[x];b[x]&=a[x]>0
print(sum(b))

Submission status: Accepted

Solution code:
i=lambda:map(int,input().split());n,m=i();l=[0,1]+[0]*n;c=[0]+[1]*n
for _ in[0]*m:x,y=i();l[y]|=l[x];c[y]+=1;c[x]-=1;l[x]=c[x]and l[x]
print(sum(c[i]and l[i]for i in range(n+1)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
list1=[["w",1]for i in range(N+1)]
list1[0]=[0,0]
list1[1]=["r",1]
#print(list1)
for j in range(M):
    x,y=map(int,input().split())
    list1[x][1]-=1
    list1[y][1]+=1
    if list1[x][0]=="r":
        list1[y][0]="r"
        if list1[x][1]==0:
            list1[x][0]="w"
cnt=0
for k in range(1,N+1):
    if list1[k][0]=="r":
        cnt+=1
print(cnt)

Submission status: Accepted

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

count = [1 for i in range(n)]
possible = [0 for i in range(n)]
possible[0] = 1

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

ans = 0
for i in range(n):
    ans += possible[i]
print(ans)

Submission status: Accepted

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

exist = [False for i in range(n)]
counts = [1 for i in range(n)]
exist[0] = True

for i in range(m):
    counts[procedure[i][0]] += -1
    counts[procedure[i][1]] += 1
    if exist[procedure[i][0]]:
        exist[procedure[i][1]] = True
    if counts[procedure[i][0]] == 0:
        exist[procedure[i][0]] = False

counter = 0
for i in range(n):
    if exist[i]:
        counter += 1

print(counter)

Submission status: Accepted

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

print(sum(red))

Submission status: Accepted

Solution code:
import sys
N, M = map(int, input().split())
num = [1] * (N + 1)
red = [0] * (N + 1)
red[1] = 1
for e in sys.stdin:
    x, y = map(int, e.split())
    if red[x]:
        red[y] = 1
    num[y] += 1
    num[x] -= 1
    if num[x] == 0:
        red[x] = 0
print(sum(red))

Submission status: Accepted

Solution code:
# coding:utf-8

INF = float('inf')


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


N, M = inpl()
# move = [inpl() for _ in range(M)]

ans = [0 for _ in range(N)]
ans[0] = 1
box = [1 for _ in range(N)]
# print(ans, box)
for i in range(M):
    # x, y = move[i]
    x, y = inpl()
    box[x - 1] -= 1
    box[y - 1] += 1
    if ans[x - 1]:
        ans[y - 1] = 1
        if box[x - 1] == 0:
            ans[x - 1] = 0
    # print(ans, box)


print(sum(ans))

Submission status: Accepted

Solution code:
# coding:utf-8

INF = float('inf')


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


N, M = inpl()
move = [inpl() for _ in range(M)]

ans = [0 for _ in range(N)]
ans[0] = 1
box = [1 for _ in range(N)]
# print(ans, box)
for i in range(M):
    x, y = move[i]
    box[x - 1] -= 1
    box[y - 1] += 1
    if ans[x - 1]:
        ans[y - 1] = 1
        if box[x - 1] == 0:
            ans[x - 1] = 0
    # print(ans, box)


print(sum(ans))

Submission status: Accepted

Solution code:
f = lambda: list(map(int,input().split()))
f_ = lambda: [int(x)-1 for x in input().split()]

n,m = f()
sus = set()
conf = [0]
num = [1 for _ in range(n)]
for _ in range(m):
    x,y = f_()
    num[x] -= 1; num[y] += 1
    if x in conf:
        if num[y] == 1:
            conf = [y]
        else:
            conf = []
            sus.add(y)
    elif y in conf:
        conf = []
        sus.add(y)
    elif x in sus:
        sus.add(y)
        if num[x] == 0:
            sus.remove(x)
print(max(len(sus),1))

Submission status: Accepted

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

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

print(sum(r))

Submission status: Accepted

Solution code:
def main21():
    strbuf = input("");
    strbufs = strbuf.split();
    buf = [];
    for i in range(2):
        buf.append(int(strbufs[i]));
    kensa = [];
    for j in range(buf[1]):
        strbuf = input("");
        strbufs = strbuf.split();
        kari = [];
        for i in range(2):
            kari.append((int(strbufs[i]))-1);
        kensa.append(kari);
    flag = [];
    kazu = [];
    for i in range(buf[0]):
        flag.append(0);
        kazu.append(1);
    flag[0] = 1;
    for i in range(buf[1]):
        kazu[kensa[i][0]] -= 1;
        kazu[kensa[i][1]] += 1;
        if flag[kensa[i][0]] == 1:
            flag[kensa[i][1]] = 1;
        if kazu[kensa[i][0]] == 0:
            flag[kensa[i][0]] = 0;
    result = 0;
    for i in range(buf[0]):
        if flag[i] == 1:
            result+=1;
    print(result);

if __name__ == '__main__':
    main21()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


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


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


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, XY):
    rc = [False] * (N + 1)
    bn = [1] * (N + 1)

    rc[1] = True
    for x, y in XY:
        bn[x] -= 1
        bn[y] += 1
        if rc[x]:
            rc[y] = True
        if bn[x] == 0 and rc[x]:
            rc[x] = False

    return rc.count(True)


def main():
    N, M = read_int_n()
    XY = [read_int_n() for _ in range(M)]
    print(slv(N, M, XY))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

        if red[x-1] == 1:
            red[y-1] = 1
        if balls[x-1] == 0:
            red[x-1] = 0

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

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ps = [False] * (N + 1)
cs = [1] * (N + 1)

ps[1] = True

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

result = sum(ps)

print(result)

Submission status: Accepted

Solution code:
def main():
  t = input().split(" ")
  N, M = int(t[0]), int(t[1])
  x = set([0])
  n = [1]*N
  for i in range(M):
    t = input().split(" ")
    fr, to = int(t[0])-1, int(t[1])-1
    if fr in x:
      x.add(to)
    n[fr] -= 1
    n[to] += 1
    if (fr in x) and n[fr] == 0:
        x.remove(fr)
  print(len(x))

main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ops = [list(map(lambda x: int(x) - 1, input().split())) for i in range(M)]

# num[i]: 箱iに入っているボールの数
# flg[i]: 箱iに赤いボールが入っている可能性があるかどうか
num = [1] * N
flg = [True] + [False] * (N - 1)

for x, y in ops:
    num[y] += 1
    if flg[x]:
        flg[y] = True

    num[x] -= 1
    if num[x] == 0:
        flg[x] = False

print(flg.count(True))

Submission status: Accepted

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

x = []
for i in range(m):
    a,b = list(map(int,input().split()))
    x.append([a-1,b-1])


a = [1 for i in range(n)]
b = [0 for i in range(n)]
b[0] = 1
for i in x:
    a[i[0]] -= 1
    a[i[1]] += 1
    if b[i[0]] == 1:
        b[i[1]] = 1
    if a[i[0]] == 0:
        b[i[0]] = 0
print(sum(b))

Submission status: Accepted

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

red = [False] * (n+1)
num = [1] * (n+1)

red[1] = True

for x, y in X:
    num[x] -= 1
    num[y] += 1

    if red[x]:
        red[y] = True    
        if num[x] == 0:
            red[x] = False

print(red.count(True))

Submission status: Accepted

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

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

red[1] = 1
white[1] = 0

for x, y in X:
    if red[x] == 1:
        if white[x] >= 1:
            white[x] -= 1
        else:
            red[x] -= 1

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

    else:
        white[x] -= 1
        white[y] += 1

print(sum(red))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
a = []
for i in range(M):
    a.append([int(j) for j in input().split()])

num = np.ones((N+1,))
red = np.zeros((N+1,))
red[1] = 1

for i in range(M):
    if red[a[i][0]] == 1:
        red[a[i][1]] = 1
    num[a[i][0]] -= 1
    num[a[i][1]] += 1
    if num[a[i][0]] == 0:
        red[a[i][0]] = 0

print(np.sum(red == 1))

Submission status: Accepted

Solution code:
initial = list(map(int, input().split(" ")))
move_list=[]
for i in range(initial[1]):
    move_list.append(list(map(int,input().split(" "))))

#[ball_num, red_ball_exist]
contents_list = [[1, False] for i in range(initial[0]-1)]
contents_list.insert(0,[1,True])

for move in move_list:
    x_ball_num,x_red_ball_exist = contents_list[move[0]-1]
    if x_red_ball_exist:
        contents_list[move[1]-1][1] = True
        if x_ball_num == 1:
            contents_list[move[0]-1][1] = False


    contents_list[move[0]-1][0] -= 1
    contents_list[move[1]-1][0] += 1

cnt = 0
for content in contents_list:
    if content[1] == True:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
actions = [list(map(int, input().split())) for _ in range(m)]
boxes = [[1, True]] + [[1, False] for _ in range(n - 1)]

for i in range(m):
	boxes[actions[i][0] - 1][0] -= 1
	boxes[actions[i][1] - 1][0] += 1
	if boxes[actions[i][0] - 1][1] == True:
		boxes[actions[i][1] - 1][1] = True
	if boxes[actions[i][0] - 1][0] == 0:
		boxes[actions[i][0] - 1][1] = False

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

print(ans)

Submission status: Accepted

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

B = [1 for i in range(N)]
P = set([1])
for i in range(M):
    x,y = map(int,input().split())
    B[x-1] -= 1
    B[y-1] += 1
    if x in P:
        P.add(y)
    if B[x-1] == 0 and x in P:
        P.remove(x)
        
print(len(P))

Submission status: Accepted

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

amt = [1 for i in range(n)]
red = [i == 0 for i in range(n)]

for i in range(m):
	a, b = input().split(' ')
	a, b = int(a), int(b)
	a -= 1
	b -= 1

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

	red[b] |= red[a]

	if amt[a] == 0:
		red[a] = False

print(sum(red))

Submission status: Accepted

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

arr = [1 for i in range(n)]
isexist = [False for i in range(n)]

isexist[0] = True

for i in range(m):
	frm,to = map(int,raw_input().split())
	frm -= 1
	to -= 1

	arr[frm] -= 1
	arr[to] += 1

	if isexist[frm] :
		isexist[to] = True

	if arr[frm] == 0:
		isexist[frm] = False




count = 0
for i in range(n):
	if isexist[i] : 
		count += 1

print count

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())

N, M = inpl()

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

balls = [1]*(N+1)

for _ in range(M):
    x, y = inpl()
    if prob[x]:
        prob[y] = True

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

    if balls[x] == 0:
        prob[x] = False

print(sum(prob))

Submission status: Accepted

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

for _ in range(m):
    a, b = map(int, input().split())
    xtoy.append([a, b])
    
exist_red = [0]*n
exist_red[0] = 1
exist_ball = [1]*n

for x, y in xtoy:
    if exist_red[x-1] == 1:
        exist_red[y-1] = 1
        
        if exist_ball[x-1] == 1:
            exist_red[x-1] = 0
    
    exist_ball[x-1] -= 1
    exist_ball[y-1] +=1
    

print(exist_red.count(1))

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
move = [list(map(int,input().split())) for _ in range(m)]
balls = [1] * n
flag = [1] + [0] * n
for i in range(m):
    balls[move[i][0] - 1] -= 1
    balls[move[i][1] - 1] += 1
    if flag[move[i][0] - 1]:
        flag[move[i][1] - 1] = 1
        if balls[move[i][0] - 1] == 0:
            flag[move[i][0] - 1] = 0

print(sum(flag))

Submission status: Accepted

Solution code:
#-- define function ---#
def init_box(n):
	b = []
	for i in range(n):
		b.append([False, 1])

	b[0][0] = True

	return b

def move(a, b, lst):
	if lst[a][1] == 1:
		if lst[a][0]:
			lst[a][0] = False
			lst[b][0] = True
		else:
			lst[a][0] = False
	else:
		if lst[a][0]:
			lst[b][0] = True

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

def count_red(lst):
	c = 0
	for i in lst:
		if i[0]:
			c += 1

	return c

#--- main ---#
N, M = input().split()
N, M = int(N), int(M)
box = init_box(N)
act = []
for i in range(M):
	x, y = input().split()
	x, y = int(x), int(y)
	act.append([x - 1, y - 1])

for i in act:
	move(i[0], i[1], box)
print(count_red(box))

Submission status: Accepted

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

x=[0 for i in range(m)]
y=[0 for i in range(m)]
ans=[0 for i in range(n)]
count=[1 for i in range(n)]
ans[0]=1

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

for i in range(m):
  count[x[i]-1]-=1
  count[y[i]-1]+=1
  
  if ans[x[i]-1]==1:
    ans[y[i]-1]=1
  if count[x[i]-1]==0:
    ans[x[i]-1]=0
    
#print(count)
#print(ans)
print(sum(ans))

Submission status: Accepted

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