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:
n,m = map(int, raw_input().split())
rl = [0.0 for _ in range(n)]
wl = [1.0 for _ in range(n)]
rl[0] = 1.0
wl[0] = 0.0
for _ in range(m):
    x,y = map(int, raw_input().split())
    p = rl[x-1] + wl[x-1]
    rl[y-1] += rl[x-1] / p
    wl[y-1] += wl[x-1] / p
    rl[x-1] -= rl[x-1] / p
    wl[x-1] -= wl[x-1] / p
cnt = 0
for p in rl:
    if p != 0:
        cnt += 1
print cnt

Submission status: Wrong Answer

Solution code:
n,m = map(int, raw_input().split())
rl = [0.0 for _ in range(n)]
wl = [1.0 for _ in range(n)]
rl[0] = 1.0
wl[0] = 0.0
for _ in range(m):
    x,y = map(int, raw_input().split())
    p = rl[x-1] + wl[x-1]
    rl[y-1] += rl[x-1] / p
    wl[y-1] += wl[x-1] / p
    rl[x-1] -= rl[x-1] / p
    wl[x-1] -= wl[x-1] / p
print len(rl) - rl.count(0.0)

Submission status: Wrong Answer

Solution code:
NM = input()
NM = "".join(NM).rstrip("\n").split(" ")
lis = [1 for s in range(int(NM[0]))]
lis2 =[]
lis2.append(1)
#print(lis)
for s in range(int(NM[1])):
	temp=input()
	temp = "".join(temp).rstrip("\n").split(" ")
	#print(lis2)

	lis[int(temp[1])-1]+=1
	if int(temp[0]) in lis2:
		lis2.append(int(temp[1]))
	lis[int(temp[0])-1]-=1
	if lis[int(temp[0])-1] ==0:
		if int(temp[0]) in lis2:
			lis2.remove(int(temp[0]))
#print(lis)
#print(lis2)
print(len(set(lis2)))

Submission status: Wrong Answer

Solution code:
n, m = (int(s) for s in input().strip().split(' '))
count = [1 for _ in range(n)]
for _ in range(m):
    x, y = (int(s) for s in input().strip().split(' '))
    count[x-1] -= 1
    count[y-1] += 1
print(len(list(filter(lambda x: x>0, count))))

Submission status: Wrong Answer

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

num_balls = [1] * (n+1)
r_boxes = [1]

for x, y in zip(xs, ys):
    num_balls[x] -= 1
    num_balls[y] += 1
    if x in r_boxes:
        r_boxes.append(y)

ans = [box for box in r_boxes if num_balls[box]]
print(1 if(len(ans) == 1) else len(ans)-1)

Submission status: Wrong Answer

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

num_balls = [1] * (n+1)
r_boxes = [1]

for x, y in zip(xs, ys):
    num_balls[x] -= 1
    num_balls[y] += 1
    if x in r_boxes:
        r_boxes.append(y)
    if not num_balls[x]:
        r_boxes.remove(x)

print(1 if(len(r_boxes) == 1) else len(r_boxes)-1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split())
red = set([1])
num = [1 for i in xrange(N+1)]
for i in xrange(M):
    x, y = map(int, raw_input().split())
    if x in red:
        num[x] -= 1
        num[y] += 1
        red.add(y)
        if num[x] == 0:
            red.remove(x)
print len(red)

Submission status: Wrong Answer

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

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

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

    f[y] = f[x]

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

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

print(cnt)

Submission status: Wrong Answer

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

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

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

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

    f[y] = True

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

print(cnt)

Submission status: Wrong Answer

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

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

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

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

    f[y] = True

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

print(cnt)

Submission status: Wrong Answer

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

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

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

    if num[x] == 0:
        f[x] = False
    else:
        f[y] = True

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

print(cnt)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

x = []
y = []
ans = 0

flag = 1

for i in range(M):
  s = map(int, raw_input().split())
  x.append(s[0])
  y.append(s[1])

for i in range(M):
  if x[i] == flag:
    ans += 1
    flag = y[i]

print ans

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
x = []
y = []
for i in range(1,M+1):
	z = list(map(int,input().split()))
	x.append(z[0])
	y.append(z[1])
a = [0]
b = [1]*N
for i in range(M):
	b[x[i]-1] -= 1
	b[y[i]-1] += 1
	if x[i]-1 in a:
		if y[i]-1 not in a:
			a.append(y[i]-1)
		elif b[x[i]-1] == 0:
			a.remove(x[i]-1)
c = 0
for i in range(len(a)):
	if b[a[i]] != 0:
		c += 1
print(c)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        n, m = map(int, input().split())
        
        # ps = [2 for i in range(n)]
        # ps[0] = 1
        # ans = [False for i in range(n)]
        
        # for i in range(m):
        #     x, y = map(int, input().split())
        #     if ps[x-1] == 1:
        #         ps[x-1] = 0
        #         if ps[y-1] == 0:
        #             ps[y-1] = 2
        #         else:
        #             ps[y-1] = 3
        #     elif ps[x-1] == 2:
        #         if ps[y-1] == 0:
        #             ps[y-1] = 2
        #         elif ps[y-1] == 1:
        #             ps[y-1] = 3
        #     elif ps[x-1] == 3:
        #         ans[x-1] = True
        #         ans[y-1] = True
                        
        
        reds = [0 for i in range(n)]
        reds[0] = 1
        whites = [1 for i in range(n)]
        whites[0] = 0
        ans = [False for i in range(n)]
        ans[0] = True
        for i in range(m):
            x, y = map(int, input().split())
            if reds[x-1] == 1:
                if whites[x-1] == 0:
                    ans[x-1] = False
                    ans[y-1] = True
                    reds[x-1] = 0
                    reds[y-1] = 1
                else:
                    ans[x-1] = True                    
                    ans[y-1] = True
                    reds[y-1] = 1
                    whites[x-1] -= 1
                    whites[y-1] += 1
            else:
                whites[x-1] -= 1
                whites[y-1] += 1
            # print(reds)
            # print(whites)
            # print(ans)
            # print
        print(len([b for b in ans if b == True]))
        
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
x = []
y = []
for i in range(1,M+1):
	z = list(map(int,input().split()))
	x.append(z[0])
	y.append(z[1])
a = [0]
b = [1]*N
for i in range(M):
	b[x[i]-1] -= 1
	b[y[i]-1] += 1
	if x[i]-1 in a:
		a.append(y[i]-1)
		if b[x[i]-1] == 0:
			a.remove(x[i]-1)
c = 0
for i in range(len(a)):
	if b[a[i]] != 0:
		c += 1
print(c)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        n, m = map(int, input().split())
        
        # ps = [2 for i in range(n)]
        # ps[0] = 1
        # ans = [False for i in range(n)]
        
        # for i in range(m):
        #     x, y = map(int, input().split())
        #     if ps[x-1] == 1:
        #         ps[x-1] = 0
        #         if ps[y-1] == 0:
        #             ps[y-1] = 2
        #         else:
        #             ps[y-1] = 3
        #     elif ps[x-1] == 2:
        #         if ps[y-1] == 0:
        #             ps[y-1] = 2
        #         elif ps[y-1] == 1:
        #             ps[y-1] = 3
        #     elif ps[x-1] == 3:
        #         ans[x-1] = True
        #         ans[y-1] = True
                        
        
        reds = [0 for i in range(n)]
        reds[0] = 1
        whites = [1 for i in range(n)]
        whites[0] = 0
        ans = [False for i in range(n)]
        ans[0] = True
        for i in range(m):
            x, y = map(int, input().split())
            if reds[x-1] == 1 or ans[x-1] == True:
                if whites[x-1] == 0:
                    ans[x-1] = False
                    ans[y-1] = True
                    reds[x-1] = 0
                    reds[y-1] = 1
                else:
                    ans[x-1] = True                    
                    ans[y-1] = True
                    whites[x-1] -= 1
                    whites[y-1] += 1
            else:
                whites[x-1] -= 1
                whites[y-1] += 1
            # print(reds)
            # print(whites)
            # print(ans)
            # print
        print(len([b for b in ans if b == True]))
        
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Wrong Answer

Solution code:
N,M=[int(x) for x in input().split()]
b=[1]*(N+1)
lst=[1]
for i in range(M):
    x,y=[int(j) for j in input().split()]
    b[x]-=1
    b[y]+=1
    if x in lst:
        lst.append(y)
lst=set(lst[1:])
ans=0
for i in range(1,N+1):
    if(b[i]!=0 and i in lst):
        ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
x = []
y = []
for i in range(1,M+1):
	z = list(map(int,input().split()))
	x.append(z[0])
	y.append(z[1])
a = [0]
b = [1]*N
for i in range(M):
	b[x[i]-1] -= 1
	b[y[i]-1] += 1
	if x[i]-1 in a:
		a.append(y[i]-1)
	elif b[x[i]-1] == 0:
		a.remove(x[i]-1)
c = 0
for i in range(len(a)):
	if b[a[i]] != 0:
		c += 1
print(c)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
x = []
y = []
for i in range(1,M+1):
	z = list(map(int,input().split()))
	x.append(z[0])
	y.append(z[1])
a = [0]
b = [1]*N
for i in range(M):
	b[x[i]-1] -= 1
	b[y[i]-1] += 1
	if x[i]-1 in a:
		a.append(y[i]-1)
c = 0
for i in range(len(a)):
	if b[a[i]] != 0:
		c += 1
print(c)

Submission status: Wrong Answer

Solution code:
n,m = map(int,raw_input().split())
arr = [1]*n
x = [False]*n
f = True
counter = 0
for i in xrange(m) :
    a,b = map(int,raw_input().split())
    if a == 1 and f:
        x[b-1] = True
        f = False
    if x[a-1] :
        x[b-1] = True
    arr[a-1] -= 1
    arr[b-1] += 1
    if arr[a-1] == 0 and x[a-1] :
        x[a-1] = False
for j in x :
    if j :
        counter += 1
print counter

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
b = [[0] * 3 for i in range(n)]
b[0][0] = 1
b[0][2] = 1

for i in range(1, n):
    b[i][1] = 1
    b[i][2] = 1

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

    if b[x][1] == 0:
        b[x][0] = 0
        b[x][2] -= 1
        b[y][0] = 1
        b[y][2] += 1

    elif b[x][0] == 0:
        b[x][1] -= 1
        b[x][2] -= 1
        b[y][1] += 1
        b[y][2] += 1

    else:
        b[x][2] -= 1
        b[y][0] = 1
        b[y][2] += 1

    if b[x][2] == 0:
        b[x][0] = 0
        b[x][1] = 0


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

print(cnt)

Submission status: Wrong Answer

Solution code:
n,m = map(int,raw_input().split())
arr = [1]*n
x = [False]*n
counter = 0
for i in xrange(m) :
    a,b = map(int,raw_input().split())
    if a == 1 :
        x[b-1] = True
    if x[a-1] :
        x[b-1] = True
    arr[a-1] -= 1
    arr[b-1] += 1
    if arr[a-1] == 0 and x[a-1] :
        x[a-1] = False
for j in x :
    if j :
        counter += 1
print counter

Submission status: Wrong Answer

Solution code:
import sys

N, M = map(int, sys.stdin.readline().split())
xy_s = []
for i in range(M):
    x, y = map(int, sys.stdin.readline().split())
    xy_s.append((x-1, y-1))

flags = [False] * N
ns = [1] * N
flags[0] = True

for x, y in xy_s:
    if flags[x]:
        if ns[x] == 1:
            flags[x] = False
        flags[y] = True
    ns[x] -= 1
    ns[y] += 1

res = 0
for f in flags:
    if f:
        res += 1

print(flags)
print(ns)
print res

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

N, M = map(int, raw_input().split())
a = [-1]*N
a[0] = 1 
for i in xrange(M):
    x, y = map(int, raw_input().split())
    x, y = x - 1, y - 1
    if a[x] >= 1:
        a[y] = abs(a[y]) + 1
        a[x] -= 1
    elif a[x] <= -1:
        if a[y] <= 0:
            a[y] = a[y] - 1
        elif a[y] > 0:
            a[y] += 1
        a[y] += 1
ans = 0
for ai in a:
    if ai > 0:
        ans += 1
print ans

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

N, M = map(int, raw_input().split())
a = [-1]*N
a[0] = 1 
for i in xrange(M):
    x, y = map(int, raw_input().split())
    x, y = x - 1, y - 1
    if a[x] >= 1:
        if a[y] <= -1:
            a[y] = abs(a[y]) + 1
        elif a[y] > 2:
            a[y] += 1
        else:
            a[y] = 1
        a[x] -= 1
    elif a[x] <= -1:
        if a[y] <= 1:
            a[y] = a[y] - 1
        elif a[y] > 2:
            a[y] += 1
        else:
            a[y] = -1
        a[y] += 1

ans = 0
for ai in a:
    if ai > 0:
        ans += 1
print ans

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

red = set([1])
pos = [0] + [1] * N
for m in range(M):
    x,y = map(int,input().split())
    if x in red:
        red.add(y)
        if pos[x] == 1:
            red.remove(x)
        pos[x] -= 1
        pos[y] += 1
        
print(len(red))

Submission status: Time Limit Exceeded

Solution code:
def myAnswer(N:int,M:int,X:list,Y:list) -> int:
   red = 0
   ans = {0}
   box = [1] * N
   for x,y in zip(X,Y):
      if(x-1 in ans):
         ans.add(y-1)
      box[x-1] -= 1
      box[y-1] += 1
      for i in list(ans):
         if(box[i] == 0):
            ans.remove(i)
   return len(ans)

def modelAnswer():
   return
def main():
   N,M = map(int,input().split())
   X = []
   Y = []
   for _ in range(M):
      x,y=map(int,input().split())
      X.append(x)
      Y.append(y)
   print(myAnswer(N,M,X,Y))
if __name__ == '__main__':
   main()

Submission status: Time Limit Exceeded

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

d = dict()
for i in range(1,n+1):
    d[str(i)] = 1

from collections import deque
ans = deque('1')
for _ in range(m):
    x,y = input().split()
    d[x] -= 1
    d[y] += 1
    if x in ans and y not in ans:
        ans.append(y)
    if d[x] == 0 and x in ans:
        ans.remove(x)
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n, m = map(int, input().split())

    red = set([1])
    balls = [1 for _ in range(n)]

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

        if x in red and balls[x - 1] >= 2:
            red = red | set([y])
        elif x in red and balls[x - 1] == 1:
            red = (red | set([y])) - set([x])

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

        # print(red, balls)

    print(len(red))

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例1(self):
        input = """3 2
1 2
2 3"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例2(self):
        input = """3 3
1 2
2 3
2 3"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例3(self):
        input = """4 4
1 2
2 3
4 1
3 4"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Time Limit Exceeded

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

for i in range(m):
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    if (x in red_lst) and (y not in red_lst):
        red_lst.append(y)
    if lst[x]==1 and x in red_lst:
        red_lst.remove(x)
    lst[x] -= 1
    lst[y] += 1

cnt = 0
print(len(red_lst))

Submission status: Time Limit Exceeded

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

for i in range(m):
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    if (x in red_lst) and (y not in red_lst):
        red_lst.append(y)
    if lst[x]==1 and x in red_lst:
        red_lst.remove(x)
    lst[x] -= 1
    lst[y] += 1

cnt = 0
for i in range(n):
    if lst[i] != 0 and i in red_lst:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

dp = [[0 for i in range(N)] for i in range(M+1)] #dp[m][n]
dp[0][0]=1

for i in range(1,M+1):
  x=XY[i-1][0]
  y=XY[i-1][1]
  for j in range(N):
    dp[i][j]=dp[i-1][j]

  if dp[i-1][x-1]>=1:
    if Q[x-1]==1:
      dp[i][x-1]=0
    else:
      dp[i][x-1]=1
    dp[i][y-1]=dp[i-1][y-1]+1
  Q[x-1]-=1
  Q[y-1]+=1
  
ans=0
for i in range(N):
  if dp[M][i]>=1:
    ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

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

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

red_box_list = [1]
# 添字0はダミー
# 添字 = 箱の番号として扱う
n_box_list = [0] + [1] * N

for xy in xy_table:
    x, y = xy
    n_box_list[x] -= 1
    n_box_list[y] += 1
    if x in red_box_list:
        if y not in red_box_list:
            red_box_list.append(y)

        if n_box_list[x] == 0:
            red_box_list.remove(x)

ans = len(red_box_list)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N,M = [int(x) for x in stdin.readline().rstrip().split()]
box = [1]*(N+1)
flag = False
n1_pass = {1}
for _ in range(M):
    x,y = [int(x) for x in stdin.readline().rstrip().split()]
    if (x in n1_pass):
        n1_pass.add(y)
    box[x] -= 1
    box[y] += 1
    if box[x] == 0:
        n1_pass = n1_pass - {x}
    
print(len(n1_pass))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
XY=[[int(j)for j in input().split()]for _ in range(M)]
cnt=[1]*(N+1)
ans=[1]
for x,y in XY: 
  cnt[x]-=1
  cnt[y]+=1
  if x in ans and not y in ans: 
    ans.append(y)
  if x in ans and cnt[x]==0:
    ans.pop(ans.index(x))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
def main():
  t = input().split(" ")
  N, M = int(t[0]), int(t[1])
  x = [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) and (to not in x):
      x.append(to)
    n[fr] -= 1
    n[to] += 1
    if (fr in x) and n[fr] == 0:
        x.remove(fr)
  print(len(x))

main()

Submission status: Time Limit Exceeded

Solution code:
def plus(a,b):
    c=a[0]*b[1]+b[0]*a[1]
    d=a[1]*b[1]
    return (c,d)
def div(a,b):
    c=a[0]*b[1]
    d=a[1]*b[0]
    return (c,d)
def times(a,b):
    c=a[1]*b[1]
    d=a[0]*b[0]
    return (c,d)
def minus(a,b):
    c=a[0]*b[1]-b[0]*a[1]
    d=a[1]*b[1]
    return (c,d)

N,M=map(int,input().split())
x=[0 for i in range(M)]
y=[0 for i in range(M)]
for i in range(M):
    x[i],y[i]=map(int,input().split())
    x[i]-=1
    y[i]-=1
a=[(0,1) for i in range(N)]
a[0]=(1,1)
b=[1 for i in range(N)]
for i in range(M):
    b[x[i]]-=1
    b[y[i]]+=1
    t=a[x[i]]
    a[x[i]]=minus(a[x[i]],div(t,(b[x[i]]+1,1)))
    a[y[i]]=plus(a[y[i]],div(t,(b[x[i]]+1,1)))
n=0
for i in range(N):
    if a[i][0]!=0:
        n+=1
print(n)

Submission status: Time Limit Exceeded

Solution code:
def gcd(p,q):
    r=max([p,q])
    s=min([p,q])
    if s==0:
        return 1
    elif r%s==0:
        return s
    else:
        return gcd(s,r%s)
def plus(a,b):
    c=a[0]*b[1]+b[0]*a[1]
    d=a[1]*b[1]
    g=gcd(c,d)
    return (c//g,d//g)
def div(a,b):
    c=a[0]*b[1]
    d=a[1]*b[0]
    g=gcd(c,d)
    return (c//g,d//g)
def times(a,b):
    c=a[1]*b[1]
    d=a[0]*b[0]
    g=gcd(c,d)
    return (c//g,d//g)
def minus(a,b):
    c=a[0]*b[1]-b[0]*a[1]
    d=a[1]*b[1]
    g=gcd(c,d)
    return (c//g,d//g)

N,M=map(int,input().split())
x=[0 for i in range(M)]
y=[0 for i in range(M)]
for i in range(M):
    x[i],y[i]=map(int,input().split())
    x[i]-=1
    y[i]-=1
a=[(0,1) for i in range(N)]
a[0]=(1,1)
b=[1 for i in range(N)]
for i in range(M):
    b[x[i]]-=1
    b[y[i]]+=1
    t=a[x[i]]
    a[x[i]]=minus(a[x[i]],div(t,(b[x[i]]+1,1)))
    a[y[i]]=plus(a[y[i]],div(t,(b[x[i]]+1,1)))
n=0
for i in range(N):
    if a[i][0]!=0:
        n+=1
print(n)

Submission status: Time Limit Exceeded

Solution code:
N,M = list(map(int,input().split()))
x = []
y = []
red = [0]
mas = [1 for i in range(N)]

for i in range(M):
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    mas[x] -= 1
    mas[y] += 1
    if x in red:
        if y not in red:
            red.append(y)
        if mas[x] == 0:
            red.remove(x)


print(len(red))

Submission status: Time Limit Exceeded

Solution code:
N,M = list(map(int,input().split()))
x = []
y = []

for i in range(M):
    u,v = map(int,input().split())
    x.append(u-1)
    y.append(v-1)
    
red = [0]
mas = [1 for i in range(N)]

for j in range(M):
    mas[x[j]] -= 1
    mas[y[j]] += 1
    if x[j] in red:
        if y[j] not in red:
            red.append(y[j])
        if mas[x[j]] == 0:
            red.remove(x[j])

print(len(red))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
x = []
y = []
for i in range(1,M+1):
	z = list(map(int,input().split()))
	x.append(z[0])
	y.append(z[1])
a = [0]
b = [1]*N
for i in range(M):
	b[x[i]-1] -= 1
	b[y[i]-1] += 1
	if x[i]-1 in a:
		if y[i]-1 not in a:
			a.append(y[i]-1)
		if b[x[i]-1] == 0:
			a.remove(x[i]-1)
c = len(a)
print(c)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
x = []
y = []
for i in range(1,M+1):
	z = list(map(int,input().split()))
	x.append(z[0])
	y.append(z[1])
a = [0]
b = [1]*N
for i in range(M):
	b[x[i]-1] -= 1
	b[y[i]-1] += 1
	if x[i]-1 in a:
		if y[i]-1 not in a:
			a.append(y[i]-1)
		if b[x[i]-1] == 0:
			a.remove(x[i]-1)
c = 0
for i in range(len(a)):
	if b[a[i]] != 0:
		c += 1
print(c)

Submission status: Accepted

Solution code:
n,m = [int(x) for x in input().split()]
x = []
for i in range(m):
  x.append([int(x) for x in input().split()])
count = [1] * (n+1)
flag = [0] * (n+1)
flag[1] = 1
for i in range(m):
  if flag[x[i][0]] == 1:
    flag[x[i][1]] = 1
  if count[x[i][0]] == 1:
    flag[x[i][0]] = 0
  count[x[i][0]] -= 1
  count[x[i][1]] += 1

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

Submission status: Accepted

Solution code:
import numpy as np

N,M=map(int,input().split())
length=np.ones(N+1)
aka=np.ones(N+1)
aka[1]=-1

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

print(len(aka[aka<0]))

Submission status: Accepted

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)
  if d[x] == -1 and x in r:
    r.remove(x)
ans = len(list(r))
for e in d:
  if d[e] == -1 and e in r:
    ans -= 1
print(ans)

Submission status: Accepted

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

#import
#import math
#import numpy as np
N, M = map(int, input().split())
Q = [list(map(int, input().split())) for _ in range(M)]

R = [False] * (N + 1)
num = [1] * (N + 1)

R[1] = True

for x, y in Q:
    R[y] = R[x] or R[y]
    num[x] -= 1
    num[y] += 1

    if num[x] <= 0:
        R[x] = False

print(R.count(True))

Submission status: Accepted

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

ball = [1] * n
flag = [1] + [0] * (n-1)

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

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

    if flag[x] == 1:
        flag[y] = 1

    if ball[x] == 0:
        flag[x] = 0

print(sum(flag))

Submission status: Accepted

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

        else:
            r[y] = 1
            b[x] -= 1
            b[y] += 1

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

print(sum(r))

Submission status: Accepted

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

Submission status: Accepted

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

for _ in range(m):
    x, y = map(int, input().split())
    if flg[x-1]:
        flg[y-1] = True
    if ball[x-1] == 1 and flg[x-1]:
        flg[x-1] = False
    ball[y-1] += 1
    ball[x-1] -= 1
    # print(x, y, ball, flg)

ans = flg.count(True)
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
r = [False for _ in range(N+1)]
b = [1 for _ in range(N+1)]
r[1] = True
r_cnt = 1
for _ in range(K):
  x, y = map(int, input().split())
  b[x] -= 1
  b[y] += 1
  if r[x]:
    if b[x] == 0:
      r[x] = False
      r_cnt -= 1
    if not r[y]:
      r[y] = True
      r_cnt += 1
print(r_cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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

box = [1 for i in range(n)]

chk = [False for i in range(n)]
chk[0] = True
for i in range(m):
  x, y = xy[i][0],xy[i][1]
  if chk[x]:
    if box[x] >= 2:
      chk[y] = True 
    else:
      chk[y] = True
      chk[x] = False
  box[y] += 1
  box[x] -= 1
cnt = 0
for i in range(n):
  cnt += chk[i]
print (cnt)

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
cnt = [1] * n
has_red = [False] * n
has_red[0] = True
for i in range(m):
    a, b = list(map(int, input().split()))
    has_red[b - 1] = has_red[b - 1] or has_red[a - 1]
    if cnt[a - 1] == 1:
        has_red[a - 1] = False
    cnt[a - 1] -= 1
    cnt[b - 1] += 1
print(has_red.count(True))

Submission status: Accepted

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

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

    if box[x][1]:
        box[y][1] = True

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

Submission status: Accepted

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

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

print(red.count(True))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
num=[1]*N
ex=[0]*N
ex[0]=1
P=[]
ans=1
for i in range(M):
    x,y=map(int,input().split())
    num[x-1]-=1
    num[y-1]+=1
    if ex[x-1]==1 and ex[y-1]==0:
        ex[y-1]=1
    if num[x-1]==0:
        ex[x-1]=0
print(sum(ex))

Submission status: Accepted

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