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:
from collections import defaultdict
n, m = map(int, input().split())
r = set()
r.add(1)
d = defaultdict(int)
for i in range(m):
  x, y = map(int, input().split())
  d[x] -= 1
  d[y] += 1
  if x in r:
    r.add(y)
ans = len(list(r))
for e in d:
  if d[e] == -1 and e in r:
    ans -= 1
print(ans)

Submission status: Wrong Answer

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

b = [1] * n
r = [1] + [0] * (n-1)

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

        if b[x] > 1:
            r[y] = 1
            b[x] -= 1
            b[y] += 1

    elif r[x] == 0:
        if b[x] >= 1:
            b[x] -= 1
            b[y] -= 1

print(sum(r))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
dic = {}
ls = [1]*(n+1)
ls[0] = 0
ans = 0
for i in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    ls[a] -= 1
    ls[b] += 1
    if dic.get(b,0) == 0:
        dic[b] = 1
for j in range(n+1):
    if ls[j] > 0 and dic.get(j,0) != 0:
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
dic = {}
ls = [1]*(n+1)
ls[0] = 0
cnt = 0
for i in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    ls[a] -= 1
    ls[b] += 1
    if dic.get(b,0) == 0:
        cnt += 1
        dic[b] = 1
print(sum([1 for e in range(1,n+1) if ls[e] > 0 and dic.get(e,0)!= 0]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

box = []
box.append([1, True])
for _ in range(N - 1):
    box.append([1, False])

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

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

Submission status: Wrong Answer

Solution code:
import sys, math
from functools import lru_cache
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

N, M = mi()
x, y = i2(M)

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

for i in range(M):
    boxes[x[i]-1] -= 1
    boxes[y[i]-1] += 1

    red[y[i]-1] = red[x[i]-1]
    if boxes[x[i]-1] == 0:
        red[x[i]-1] = False

print(red.count(True))

Submission status: Wrong Answer

Solution code:
import sys, math
from functools import lru_cache
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

N, M = mi()
x, y = i2(M)

boxes = [1]*N
white = [True for i in range(N)]
white[0] = False

for i in range(M):
    white[y[i]-1] = white[x[i]-1]

    boxes[x[i]-1] -= 1
    boxes[y[i]-1] += 1

    if boxes[x[i]-1] == 0:
        white[x[i]-1] = True

print(white.count(False))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
s = {1}
t = [1]*N
res = 0
for _ in range(M):
    x, y = map(int, input().split())
    t[x-1] -= 1
    t[y-1] += 1
    if x-1 in s:
        s.add(y-1)
for i in range(N):
    if t[i] >= 1 and i in s:
        res += 1
print(res)

Submission status: Wrong Answer

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

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

def main():
    mod=10**9+7
    N,M=MI()
    L=[0]*N
    L[0]=1
    cnt=[1]*N
    for i in range(M):
        x,y=MI()
        x-=1
        y-=1
        if L[x]==1:
            L[y]=1
            if cnt[x]==1:
                L[x]=0
                flag=0
            cnt[x]-=1
            cnt[y]+=1
                
    print(sum(L))
        

main()

Submission status: Wrong Answer

Solution code:
#  AtCoder Grand Contest 002 B

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

xn = [0] * M
yn = [0] * M

for i in range(M):
    xn[i],yn[i] = map(int,input().split())

for i in range(M):
    xn[i] -=1
    yn[i] -=1

box = []
for i in range(N):
    box.append([i,1,0])
box[0][2] = 1

a = [0]
for i in range(M):
    if xn[i] in a:
        if box[xn[i]][1] !=0:
            box[xn[i]][1] -=1
            box[yn[i]][1] +=1
            box[yn[i]][2] =1
            a.append(yn[i])  

count = 0
for i in range(N):
    if box[i][1] !=0 and box[i][2] ==1:
        count +=1
        
print(count)

Submission status: Wrong Answer

Solution code:

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

xn = [0] * M
yn = [0] * M

for i in range(M):
    xn[i],yn[i] = map(int,input().split())

for i in range(M):
    xn[i] -=1
    yn[i] -=1

box = []
for i in range(N):
    box.append([i,1,0])
box[0][2] = 1

a = [0]
for i in range(M):
    if xn[i] in a:
        if xn[i] !=0:
            box[xn[i]][1] -=1
            box[yn[i]][1] +=1
            box[yn[i]][2] =1
            a.append(yn[i])  
        if xn[i] ==0:
            continue

count = 0
for i in range(N):
    if box[i][1] !=0 and box[i][2] ==1:
        count +=1
        
print(count)

Submission status: Wrong Answer

Solution code:
#  AtCoder Grand Contest 002 B

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

xn = [0] * M
yn = [0] * M

for i in range(M):
    xn[i],yn[i] = map(int,input().split())

for i in range(M):
    xn[i] -=1
    yn[i] -=1

box = []
for i in range(N):
    box.append([i,1,0])
box[0][2] = 1

a = [0]
for i in range(M):
    if xn[i] in a:
        box[xn[i]][1] -=1
        box[yn[i]][1] +=1
        box[yn[i]][2] =1
        a.append(yn[i])    

count = 0
for i in range(N):
    if box[i][1] !=0 and box[i][2] ==1:
        count +=1
print(count)

Submission status: Wrong Answer

Solution code:
#  AtCoder Grand Contest 002 B

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

xn = [0] * M
yn = [0] * M

for i in range(M):
    xn[i],yn[i] = map(int,input().split())

if 1 not in xn:
    print(1)
    exit()

a = [1]
count = 0

for i in range(M):
    if xn[i] in a:
        count +=1
        a.append(yn[i])
        
print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
possible = [0]*n
kosuu = [1]*n
possible[0] = 1
flag = False
L = []
idx = 0
for i in range(m):
    x,y = map(int,input().split())
    L.append((x,y))
for i in range(m):
    x,y = L[i][0],L[i][1]
    if x == 1:
        flag = True
        idx = i
        break

if flag == False:
    print(1)
    exit()
for i in range(idx,m):
    x,y = L[i][0],L[i][1]
    x,y = x-1,y-1
    if kosuu[x] >=2 and possible[x] == 1:
        possible[y] = 1
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 1:
        possible[x] = 0
        possible[y] = 1
        kosuu[x] = 0
        kosuu[y] +=1
    elif kosuu[x] >=2 and possible[x] == 0:
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 0:
        kosuu[x] = 0
        kosuu[y] +=1
print(sum(possible))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
possible = [0]*n
kosuu = [1]*n
possible[0] = 1
flag = False
L = []
idx = 0
for i in range(m):
    x,y = map(int,input().split())
    L.append((x,y))
for i in range(m):
    x,y = L[i][0],L[i][1]
    if x == 1:
        flag = True
        idx = i
        break

if flag == False:
    print(1)
    exit()
for i in range(idx,m):
    x,y = L[i][0],L[i][1]
    x,y = x-1,y-1
    if kosuu[x] >=2 and possible[x] == 1:
        possible[y] = 1
        kosuu[x] -=1
        kosuu[y] +=1
    elif kosuu[x] == 1 and possible[x] == 1:
        possible[x] = 0
        possible[y] = 1
        kosuu[x] = 0
        kosuu[y] +=1
    
print(sum(possible))

Submission status: Wrong Answer

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

possible = [0]*n
possible[0] = 1
kosuu = [1]*n

for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
        
    if kosuu[a] >=1 and possible[a] == 1:
        possible[a] = 1 
        possible[b] = 1
        kosuu[a] -=1
        kosuu[b] +=1
        if kosuu[a] == 0:
            possible[a] = 0
    elif kosuu[a] ==1 and possible[a] == 0 : 
        possible[a] = 0
        possible[b] = 0
        kosuu[a] = 0
        kosuu[b] +=1

    #print(possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

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

possible = [0]*n
possible[0] = 1
kosuu = [1]*n

for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
        
    if kosuu[a] >= 2 and possible[a] ==1:
        possible[a] = 1 
        possible[b] = 1
        kosuu[a] -=1
        kosuu[b] +=1
    elif kosuu[a] ==1 and possible[a] ==1 : 
        possible[a] = 0
        possible[b] = 1
        kosuu[a] = 0
        kosuu[b] +=1
    elif kosuu[a] >= 2 and possible[a] ==0:
        possible[a] = 0
        possible[b] = 0
        kosuu[a] -=1
        kosuu[b] +=1
    elif kosuu[a] ==1 and possible[a] ==0 : 
        possible[a] = 0
        possible[b] = 0
        kosuu[a] = 0
        kosuu[b] +=1
    #print(possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

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

possible = [0]*n
possible[0] = 1
kosuu = [1]*n

for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
        
    if kosuu[a] >= 2 and possible[a] ==1:
        possible[a] = 1 
        possible[b] = 1
        kosuu[a] -=1
        kosuu[b] +=1
    elif kosuu[a] ==1 and possible[a] ==1 : 
        possible[a] = 0
        possible[b] = 1
        kosuu[a] = 0
        kosuu[b] +=1

    #print(possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

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

possible = [0]*n
possible[0] = 1
kosuu = [1]*n

for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
        
    if kosuu[a] >= 2:
        possible[a] = 1 
        possible[b] = 1
        kosuu[a] -=1
        kosuu[b] +=1
    elif kosuu[a] ==1 and possible[a] ==1 : 
        possible[a] = 0
        possible[b] = 1
        kosuu[a] = 0
        kosuu[b] +=1

    #print(possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

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

possible = [0]*n
possible[0] = 1
kosuu = [1]*n

for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
        
    if kosuu[a] >= 2:
        possible[a] = 1 
        possible[b] = 1
        kosuu[a] -=1
        kosuu[b] +=1
    elif kosuu[a] ==1:
        possible[a] = 0
        possible[b] = 1
        kosuu[a] = 0
        kosuu[b] +=1

    #print(visit,possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
visit = [-1]*n
visit[0] = 1
possible = [0]*n
possible[0] = 1
visit[0] = 1
kosuu = [1]*n
for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    if visit[a] == 1:
        visit[a] = -1
        visit[b] = 1
    #elif visit[a] == -1:
        #visit[b] = 0
        
    if kosuu[a] >= 2:
        possible[a] = 1 
        possible[b] = 1
    elif kosuu[a] ==1 and visit[a] == 1:
        possible[a] = 0
        possible[b] = 1
    if kosuu[a] >=1:    
        kosuu[a] -=1
        kosuu[b] +=1
    
    if kosuu[a] == 0:
        possible[a] = 0
    #print(visit,possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
visit = [-1]*n
visit[0] = 1
possible = [0]*n
possible[0] = 1
visit[0] = 1
kosuu = [1]*n
for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    if visit[a] == 1:
        visit[a] = -1
        visit[b] = 1
    #elif visit[a] == -1:
        #visit[b] = 0
        
    if kosuu[a] >= 2:
        possible[a] = 1 
        possible[b] = 1
    elif kosuu[a] ==1 and visit[a] == 1:
        possible[a] = 0
        
    if kosuu[a] >=1:    
        kosuu[a] -=1
        kosuu[b] +=1
    
    if kosuu[a] == 0:
        possible[a] = 0
    #print(visit,possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
kouho = {}
red = 1
Nlist = [1]*(N+1)
ans = 0
for i in range(M):
    x, y = map(int, input().split())
    if x == red:
        red = y
        kouho[y] = True
        Nlist[x] -= 1
        Nlist[y] += 1
    else:
        Nlist[x] -= 1
        Nlist[y] += 1
    if Nlist[x]==0:
        if x in kouho:
            del kouho[x]

for i in range(1, N+1):
    if Nlist[i] == 0:
        continue
    if i in kouho:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
kouho = {}
red = 1
Nlist = [1]*(N+1)
ans = 0
for i in range(M):
    x, y = map(int, input().split())
    if x == red:
        red = y
        kouho[y] = True
        Nlist[x] -= 1
        Nlist[y] += 1
    else:
        Nlist[x] -= 1
        Nlist[y] += 1

for i in range(1, N+1):
    if Nlist[i] == 0:
        continue
    if i in kouho:
        ans += 1

print(ans)

Submission status: Wrong Answer

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

check = [1]+[0]*n
numlis = [1]*(n+1)

for i in range(m):
    x, y = map(int, input().split())
    if numlis[x-1] > 1 and check[x-1] == 1:
        check[y-1] = 1
        numlis[x-1] -= 1
        numlis[y-1] += 1
    elif numlis[x-1] == 1 and check[x-1] == 1:
        check[y-1] = 1
        check[x-1] = 0
        numlis[x-1] -= 1
        numlis[y-1] += 1

    
print(sum(check))

Submission status: Wrong Answer

Solution code:
def main3():
    N, M = map(int, input().split())
    b_i = [0 for i in range(N + 1)]
    b_o = [0 for i in range(N + 1)]
    ball = ["", "R"] + ["" for i in range(N - 1)]
    rflag = [0 for i in range(N + 1)]

    for i in range(M):
        x, y = map(int, input().split())
        b_o[x] += 1
        b_i[y] += 1
        if ball[x] == "R":
            ball[x] = ""
            ball[y] = "R"
            rflag[y] = 1

    # print(b_i[1:])
    # print(b_o[1:])
    # print(ball[1:])
    # print(rflag[1:])

    ans = 0
    if b_o[1] == 0:
        ans = 1
    else:
        for i in range(1, N + 1):
            if rflag[i] == 1 and b_i[i] >= b_o[i]:
                ans += 1

    print(ans)

if __name__ == "__main__":
    main3()

Submission status: Wrong Answer

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

    rball = ["", "R"] + ["" for i in range(N - 1)]
    rflag = [0 for i in range(N + 1)]
    m_count = [0 for i in range(N + 1)]
    ball = [0, 1] + [1 for i in range(N - 1)]

    rflag[1] = 1

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

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

        if rball[x] == "R":
            rflag[y] = 1
            rball[x] = ""
            rball[y] = "R"

    # print("m_count    : ", m_count[1:])
    # print("rflag      : ", rflag[1:])
    # print("ball       : ", ball[1:])

    ans = 0
    if m_count[1] == 0:
        ans = 1

    else:
        for i in range(1, N + 1):
            if rflag[i] == 1 and ball[i] >= 1:
                ans += 1

    print(ans)

if __name__ == "__main__":
    main2()

Submission status: Wrong Answer

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

    rball = ["", "R"] + ["" for i in range(N - 1)]
    rflag = [0 for i in range(N + 1)]
    m_count = [0 for i in range(N + 1)]
    ball = [0, 1] + [1 for i in range(N - 1)]

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

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

        if rball[x] == "R":
            rflag[y] = 1
            rball[x] = ""
            rball[y] = "R"

    # print("m_count    : ", m_count[1:])
    # print("rflag      : ", rflag[1:])
    # print("ball       : ", ball[1:])

    ans = 0
    for i in range(1, N + 1):
        if rflag[i] == 1 and ball[i] >= 1:
            ans += 1

    print(ans)

if __name__ == "__main__":
    main2()

Submission status: Wrong Answer

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

    rball = ["", "R"] + ["" for i in range(N - 1)]
    rflag = [0 for i in range(N + 1)]
    count = [0 for i in range(N + 1)]
    max_ball = [0, 1] + [1 for i in range(N - 1)]

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

        count[x] += 1
        max_ball[y] = max(max_ball[y], max_ball[y] + 1) 

        if rball[x] == "R":
            rflag[y] = 1
            rball[x] = ""
            rball[y] = "R"

    # print("move_count : ", count[1:])
    # print("rflag      : ", rflag[1:])
    # print("max_ball   : ", max_ball[1:])

    ans = 0
    for i in range(1, N + 1):
        if rflag[i] == 1 and max_ball[i] > count[i]:
            ans += 1

    print(ans)

if __name__ == "__main__":
    main2()

Submission status: Wrong Answer

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

    rball = ["", "R"] + ["" for i in range(N - 1)]
    max_ball = [1 for i in range(N + 1)]
    red_flag = [0 for i in range(N + 1)]
    move_count = [0 for i in range(N + 1)]

    max_ball[0] = 0

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

        if rball[x] == "R":
            rball[x] = ""
            rball[y] = "R"
            max_ball[y] += 1
            red_flag[y] = 1
        else:
            max_ball[y] += 1

        move_count[x] += 1

    ans = 0
    for i in range(1, N + 1):
        if red_flag[i] == 1 and max_ball[i] > move_count[i]:
            ans += 1

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    ball = [list(), list("R")] + [list("W") for i in range(N - 1)]
    max_ball = [1 for i in range(N + 1)]
    red_flag = [0 for i in range(N + 1)]
    move_count = [0 for i in range(N + 1)]

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

        if "R" in ball[x]:
            ball[x].remove("R")
            ball[y].append("R")
            max_ball[y] += 1
            red_flag[y] = 1
        else:
            ball[x].remove("W")
            ball[y].append("W")
            max_ball[y] += 1

        move_count[x] += 1

    ans = 0
    for i in range(1, N + 1):
        if red_flag[i] == 1 and max_ball[i] > move_count[i]:
            ans += 1

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

print(sum(aka))

Submission status: Wrong Answer

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

start = 1
aka_list = [1]
for i in range(m):
    if x_y[i][0] == start:
        if start == 1:
            aka_list.remove(1)
        aka_list.append(x_y[i][1])
        start = x_y[i][1]
balls = [1]*n
for i in range(m):
    balls[x_y[i][0]-1] -= 1
    balls[x_y[i][1]-1] += 1

count = 0
for i in range(len(aka_list)):
    if balls[aka_list[i]-1]:
        count += 1
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

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

ans = sum(r)
print(ans)

Submission status: Wrong Answer

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

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

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

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

ans = sum(r)
print(ans)

Submission status: Wrong Answer

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

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

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

for y in x:
    if r[y[0]] > 0:
        r[y[1]] = 1
    if w[y[0]] > 0:
        w[y[0]] += 1
    if r[y[0]] > 0 and w[y[0]] == 0:
        r[y[0]] = 0
    if r[y[0]] == 0 and w[y[0]] > 0:
        w[y[0]] -= 1

ans = sum(r)
print(ans)

Submission status: Wrong Answer

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

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

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

for i in range(M):
  x, y = map(int, input().split())
  
  boxes[x] -= 1
  boxes[y] += 1
  
  if ball_exists[x]:
    if boxes[x] == 0:
      ball_exists[x] = False
    
      if boxes[y] == 1:
        ball_exists = [False] * (N + 1)
      
    ball_exists[y] = True

  #print(boxes)
  #print(ball_exists)

print(ball_exists.count(True))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(flg.count(True))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
visit = [-1]*n
possible = [0]*n
visit[0] = 1
kosuu = [1]*n
for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    if visit[a] == 1:
        visit[a] = -1
        visit[b] = 1
    elif visit[a] >=2:
        visit[b] = 1
        
    if kosuu[a] >= 2:
        possible[a] = 1 
        possible[b] = 1
    elif kosuu[a] <=1 and visit[a] == 1:
        possible[a] = 0
        
    if kosuu[a] >=1:    
        kosuu[a] -=1
        kosuu[b] +=1
    
    if kosuu[a] == 0:
        possible[a] = 0
#print(visit,possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
visit = [-1]*n
possible = [0]*n
visit[0] = 1
kosuu = [1]*n
for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    if visit[a] == 1:
        visit[a] = -1
        visit[b] = 1
        
    if kosuu[a] >= 2:
        possible[a] = 1 
        possible[b] = 1
    elif kosuu[a] <=1 and visit[a] == 1:
        possible[a] = 0
        
    if kosuu[a] >=1:    
        kosuu[a] -=1
        kosuu[b] +=1
    
    if kosuu[a] == 0:
        possible[a] = 0
#print(visit,possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
visit = [-1]*n
possible = [0]*n
visit[0] = 1
kosuu = [1]*n
for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    
    if kosuu[a] >= 2:
        possible[a] = 1 
        possible[b] = 1
    elif kosuu[a] <=1 and visit[a] == 1:
        possible[a] = 0
        
    if kosuu[a] >=1:    
        kosuu[a] -=1
        kosuu[b] +=1
    
    if visit[a] == 1:
        visit[a] = -1
        visit[b] = 1
    if kosuu[a] == 0:
        possible[a] = 0
#print(visit,possible,kosuu)
print(sum(possible))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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