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: Wrong Answer

Solution code:
def INT():
    return int(input())

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

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

from collections import deque

N, M = MI()
red = ["r"] + [None] * (N - 1)
white = [[]] + [["w"] for _ in range(N - 1)]
in_red = 0
ans = 0

for _ in range(M):
    x, y = MI()
    
    if red[x - 1] == "r":
        red[x - 1] = None
        red[y - 1] = "r"
        in_red = y - 1
        ans += 1

    else:
        w = white[x - 1].pop()
        white[y - 1].append(w)

if len(white[in_red]) + 1 == N:
    ans = 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
numbers=[['w',1] for _ in range(N)]
numbers[0][0]='r'
answer=1
number=0
for _ in range(M):
    x,y=map(int,input().split())
    if x==1 and number==0:
        numbers[0][0]='w'
        numbers[0][1]=0
        numbers[y-1][0]='r'
        numbers[y-1][1]+=1
        number+=1
        continue
    elif number==0 and y==1:
        number+=1
    if numbers[x-1][0]=='r':
        numbers[x-1][1]-=1
        numbers[y - 1][0] = 'r'
        numbers[y - 1][1] += 1
        if numbers[x-1][1]==0:
            answer-=1
            numbers[x-1][0]='w'
        else:
            answer+=1
    else:
        numbers[x-1][1]-=1
        numbers[y-1][1]+=1
print(answer)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
numbers=[['w',1] for _ in range(N)]
numbers[0][0]='r'
answer=1
number=0
for _ in range(M):
    x,y=map(int,input().split())
    if x==1 and number==0:
        numbers[0][0]='w'
        numbers[0][1]=0
        numbers[y-1][0]='r'
        numbers[y-1][1]+=1
        number+=1
        continue
    elif number==0 and y==1:
        number+=1
    if numbers[x-1][0]=='r':
        numbers[x-1][1]-=1
        numbers[y - 1][0] = 'r'
        numbers[y - 1][1] += 1
        if numbers[x-1][1]==0:
            answer-=1
        else:
            answer+=1
    else:
        numbers[x-1][1]-=1
        numbers[y-1][1]+=1
print(answer)

Submission status: Wrong Answer

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


# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    n, m = input_list()
    balls = [1] * n
    reds = []
    for _ in range(m):
        x, y = input_list()
        if balls[x-1] > 0:
            balls[x-1] -= 1
            balls[y-1] += 1
        if x == 1:
            reds.append(y)
        if x in reds:
            reds.append(y)
    result = []
    for i, v in enumerate(balls):
        if v > 0:
            result.append(i+1)
    reds = list(filter(lambda x: x > 0, reds))
    a = set(reds) & set(result)
    print(len(a))


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


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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
red_dict = {i:1 for i in range(1,n+1)}
red_set = {1}
st = 1
res = 0
for _ in range(m):
    x,y = map(int,input().split())
    if x == st:
        red_set.add(y)
        st = y
        red_dict[x]-=1
        red_dict[y]+=1
        if red_dict[x]==0:
            red_set.remove(x)
    else:
        red_dict[x]-=1
        red_dict[y]+=1

for v in red_set:
    if 0 < red_dict[v]:
        res += 1
print(res)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ball=[1]*n
red=[0]*n
red[0]=1
for i in range(m):
    x=xy[i][0]-1
    y=xy[i][1]-1
    if red[x]==1 and ball[x]==1:
        red[y]=1
        red[x]-=1
        ball[y]+=1
        ball[x]-=1
    elif red[x]==1 and ball[x]>1:
        red[y]+=1
        ball[y]+=1
        ball[x]-=1
    else:
        ball[y]+=1
        ball[x]-=1

# print(red)
# print(ball)
print(red.count(1))

Submission status: Wrong Answer

Solution code:
import math
import collections
import fractions
import itertools
import functools
import  operator

def solve():
    n, m = map(int, input().split())
    ball, can = [1]*n, [True]*n
    can[0] = True
    for _ in range(m):
        x, y = map(int, input().split())
        if can[x-1] == True:
            can[y-1] = True
        ball[x-1] -= 1
        ball[y-1] += 1
        if ball[x-1] == 0:
            can[x-1] = False
    print(can.count(True))
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import math
import collections
import fractions
import itertools
import functools
import  operator

def solve():
    n, m = map(int, input().split())
    ball, can = [0]*n, [True]*n
    ball[0], can[0] = 1, True
    for _ in range(m):
        x, y = map(int, input().split())
        if can[x-1] == True:
            can[y-1] = True
        ball[x-1] -= 1
        ball[y-1] += 1
        if ball[x-1] == 0:
            can[x-1] = False
    print(can.count(True))
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

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

ans_list = [0 for i in range(10**5)]
ans_list[0] = 1
ball_list = [1 for i in range(10**5)]
print(ball_list[0])

for tmp in b_list:
    x, y = tmp[0], tmp[1]
    print(x, y)
    if ans_list[x-1] == 1 and ans_list[y-1] == 0:
        ans_list[y-1] += 1
    ball_list[y-1] += 1
    ball_list[x-1] -= 1
    if ball_list[x-1] == 0:
        ans_list[x-1] = 0

ans = 0

for i in ans_list:
    if i == 1:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N, M = map(int, input().split())
    XY = [tuple(map(int, input().split())) for _ in [0] * M]

    box = [1] * (N + 1)
    red = [0] * (N + 1)
    box[0], red[1] = 0, 1
    for x, y in XY:
        box[x] -= 1
        box[y] += 1
        red[y] = red[x]
        if not box[x]:
            red[x] = 0
    print(sum(red))


main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,m = readInts()
box = [1] * n
nya = [True] + [False]*(n-1)
for i in range(m):
    x,y = map(lambda x:int(x)-1,input().split())
    if box[x] < 2:
        if nya[x] == True:
            if box[x]-1 == 0:
                nya[x] = False
            nya[y] = True
        else:
            nya[y] = False
        box[x] -= 1
        box[y] += 1
    else:
        nya[y] = True
        box[x] -= 1
        box[y] += 1
print(nya.count(True))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(m)]
l=[1]*n
out_=[0]*n
in_=[0]*n#入れられたことがある履歴。0番目だけは初期値1
c=1
#print(in_)
check=0
for i in range(m):
    if check==0:
        if xy[i][0]==1:#箱1に入ってくる前に初めて箱1から赤いボールを移す場合
            in_[xy[i][1]-1]+=1
            check=1
        else:#箱１から赤いボールを移す前に白いボールが入ってくる場合
            if xy[i][1]==1:
                in_[xy[i][1]-1]+=1
                check=1
        l[xy[i][0]-1]-=1
        l[xy[i][1]-1]+=1
    else:
        l[xy[i][0]-1]-=1
        l[xy[i][1]-1]+=1
        if in_[xy[i][0]-1]!=0 and in_[xy[i][1]-1]==0:#今までボールを入れられたことがなければ移しようがない。既に入れられたことあるものは重複するので除外。
            c+=1
            in_[xy[i][1]-1]+=1
        #print(l,in_)
#print(l,in_,c)
c2=0#ボールが入っている箱の数
ans=0
for i in l:
    if i!=0:
        c2+=1
for i in range(n):
    if l[i]!=0 and in_[i]!=0:
        ans+=1
#print(min(c,c2))
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(m)]
l=[1]*n
out_=[0]*n
in_=[0]*n#入れられたことがある履歴。0番目だけは初期値1
c=1
#print(in_)
check=0
for i in range(m):
    if check==0:
        if xy[i][0]==1:#箱1に入ってくる前に初めて箱1から赤いボールを移す場合
            in_[xy[i][1]-1]+=1
            check=1
        else:#箱１から赤いボールを移す前に白いボールが入ってくる場合
            if xy[i][1]==1:
                in_[xy[i][1]-1]+=1
                check=1
        l[xy[i][0]-1]-=1
        l[xy[i][1]-1]+=1
    else:
        l[xy[i][0]-1]-=1
        l[xy[i][1]-1]+=1
        if in_[xy[i][0]-1]!=0 and in_[xy[i][1]-1]==0:#今までボールを入れられたことがなければ移しようがない。既に入れられたことあるものは重複するので除外。
            c+=1
            in_[xy[i][1]-1]+=1
        #print(l,in_)
#print(l,in_,c)
c2=0#ボールが入っている箱の数
for i in l:
    if i!=0:
        c2+=1
print(min(c,c2))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(m)]
l=[1]*n
out_=[0]*n
in_=[0]*n#入れられたことがある履歴。0番目だけは初期値1
in_[0]=1
c=1
#print(in_)
check=0
for i in range(m):
    if check==0:
        if xy[i][0]==1:
            in_[0]=0
            in_[xy[i][1]-1]+=1
            check=1
        else:
            if xy[i][1]==1:
                in_[0]=1
                in_[xy[i][1]-1]+=1
                check=1
        l[xy[i][0]-1]-=1
        l[xy[i][1]-1]+=1
    else:
        l[xy[i][0]-1]-=1
        l[xy[i][1]-1]+=1
        if in_[xy[i][0]-1]!=0 and in_[xy[i][1]-1]==0:#今までボールを入れられたことがなければ移しようがない。既に入れられたことあるものは重複するので除外。
            c+=1
        in_[xy[i][1]-1]+=1
#print(l,in_,c)
c2=0#ボールが入っている箱の数
for i in l:
    if i!=0:
        c2+=1
print(min(c,c2))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(m)]
l=[1]*n
out_=[0]*n
in_=[0]*n#入れられたことがある履歴。0番目だけは初期値1
in_[0]=1
c=0
#print(in_)
check=0
for i in range(m):
    if check==0:
        if xy[i][0]==1:
            l[xy[i][0]-1]-=1
            l[xy[i][1]-1]+=1
            in_[0]=0
            c+=1
            in_[xy[i][1]-1]+=1
            check=1
        else:
            l[xy[i][0]-1]-=1
            l[xy[i][1]-1]+=1
    else:
        l[xy[i][0]-1]-=1
        l[xy[i][1]-1]+=1
        if in_[xy[i][0]-1]!=0 and in_[xy[i][1]-1]==0:#今までボールを入れられたことがなければ移しようがない。既に入れられたことあるものは重複するので除外。
            c+=1
        in_[xy[i][1]-1]+=1
#print(l,in_,c)
c2=0#ボールが入っている箱の数
for i in l:
    if i!=0:
        c2+=1
print(min(c,c2))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(m)]
l=[1]*n
out_=[0]*n
in_=[0]*n#入れられたことがある履歴。0番目だけは初期値1
in_[0]=1
c=0
#print(in_)
for i in range(m):
    l[xy[i][0]-1]-=1
    l[xy[i][1]-1]+=1
    if in_[xy[i][0]-1]!=0 and in_[xy[i][1]-1]==0:#今までボールを入れられたことがなければ移しようがない。既に入れられたことあるものは重複するので除外。
        c+=1
    in_[xy[i][1]-1]+=1
#print(l,in_,c)
c2=0#ボールが入っている箱の数
for i in l:
    if i!=0:
        c2+=1
print(min(c,c2))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(m)]
l=[1]*n
out_=[0]*n
in_=[0]*n#入れられたことがある履歴。0番目だけは初期値1
in_[0]=1
c=0
#print(in_)
for i in range(m):
    l[xy[i][0]-1]-=1
    l[xy[i][1]-1]+=1
    if in_[xy[i][0]-1]!=0:
        c+=1
    in_[xy[i][1]-1]+=1
#print(l,in_,c)
c2=0
for i in l:
    if i!=0:
        c2+=1
print(min(c,c2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
N = [1]*n
P = [True] + [False]*(n-1)
for i in range(m):
    x, y = list(map(lambda tmp: tmp-1, map(int, input().split())))
    P[y] = True if P[x]==True else False
    N[x] -= 1; N[y] += 1
    P[x] = False if N[x]==0 else P[x]
print(sum(P))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
N = [1]*n
P = [1] + [0]*(n-1)
for i in range(m):
    x, y = list(map(lambda tmp: tmp-1, map(int, input().split())))
    P[y] = (P[y]*N[y]+P[x])/(N[y]+1)
    N[x] -= 1; N[y] += 1
    P[x] = 0 if N[x]==0 else P[x]
print(sum(map(bool, P)))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
mizu = [1]*n
iro = [1] + [0]*(n-1)
for i in range(m):
    x, y = list(map(lambda tmp: tmp-1, map(int, input().split())))
    iro[y] = (iro[y]*mizu[y]+iro[x])/(mizu[y]+1)
    iro[x] = (iro[x]-1)/(mizu[x]-1+1e-5)
    mizu[x]-=1; mizu[y]+=1
    if mizu[x]==0:
        iro[x] = 0
print(sum(map(lambda x: int(bool(x)), iro)))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
mizu = [1]*n
iro = [1] + [0]*(n-1)
for i in range(m):
    x, y = list(map(lambda tmp: tmp-1, map(int, input().split())))
    iro[y] = (iro[y]+iro[x])/(mizu[y]+1)
    mizu[x]-=1; mizu[y]+=1
    if mizu[x]==0:
        iro[x] = 0
print(sum(map(lambda x: int(bool(x)), iro)))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
mizu = [1]*n
iro = [1] + [0]*(n-1)
for i in range(m):
    x, y = list(map(lambda x: x-1, map(int, input().split())))
    iro[y] += (iro[y]+iro[x])/(mizu[y]+1)
    mizu[x]-=1; mizu[y]+=1
    if mizu[x]==0:
        iro[x] = 0
print(sum(map(lambda x: int(bool(x)), iro)))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split(" "))
ar = [[0,1] for i in range(n)]
ar[0] = [1,1]
r = 0
for i in range(m):
    a,b = map(int,input().split(" "))
    a -= 1
    b -= 1
    if ar[a][0] == 1 and ar[a][1] >= 1:
        ar[b][0] = 1
        ar[b][1] += 1
    ar[a][1] -= 1
    if ar[a][1] == 0:
        ar[a][0] = 0
count = 0
for r in ar:
    if r[0] == 1:
        count += 1
print(count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_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

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

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

N,M = IL()
data = ILL(M)
count = [1 for i in range(N)]
p = [0 for i in range(N)]
p[0] = 1
for x,y in data:
  x,y = x-1,y-1
  if count[x] == 1:
    p[y] += p[x]
    p[x] = 0
  else:
    N = count[x]
    p[y] += 1
  count[x] -= 1
  count[y] += 1
ans = 0
for i in p:
  if i!=0:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_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

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

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

N,M = IL()
data = ILL(M)
count = [1 for i in range(N)]
p = [0 for i in range(N)]
p[0] = 1
for x,y in data:
  x,y = x-1,y-1
  if count[x] == 1:
    p[y] += p[x]
    p[x] = 0
  else:
    N = count[x]
    p[y] += p[x]/N
    p[x] *= 1-1/N
  count[x] -= 1
  count[y] += 1
ans = 0
for i in p:
  if i!=0:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_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

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

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

N,M = IL()
data = ILL(M)
count = [1 for i in range(N)]
p = [0 for i in range(N)]
p[0] = 1
for x,y in data:
  x,y = x-1,y-1
  if count == 1:
    p[y] += p[x]
    p[x] = 0
  else:
    N = count[x]
    p[y] += +p[x]/N
    p[x] *= 1-1/N
  count[x] -= 1
  count[y] += 1
ans = 0
for i in p:
  if i!=0:
    ans += 1
print(ans)