Problem:


Problem Statement

We have N boxes, numbered 1 through N. At first, box 1 contains one red ball, and each of the other boxes contains one white ball.

Snuke will perform the following M operations, one by one. In the i-th operation, he randomly picks one ball from box x_i, then he puts it into box y_i.

Find the number of boxes that may contain the red ball after all operations are performed.

Constraints

2≤N≤10^5

1≤M≤10^5

1≤x_i,y_i≤N

x_i≠y_i

Just before the i-th operation is performed, box x_i contains at least 1 ball.

Input

The input is given from Standard Input in the following format:

N M
x_1 y_1
:
x_M y_M

Output

Print the number of boxes that may contain the red ball after all operations are performed.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

2

Just after the first operation, box 1 is empty, box 2 contains one red ball and one white ball, and box 3 contains one white ball.

Now, consider the second operation. If Snuke picks the red ball from box 2, the red ball will go into box 3. If he picks the white ball instead, the red ball will stay in box 2.
Thus, the number of boxes that may contain the red ball after all operations, is 2.

Sample Input 2

3 3
1 2
2 3
2 3

Sample Output 2

1

All balls will go into box 3.

Sample Input 3

4 4
1 2
2 3
4 1
3 4

Sample Output 3

3



Sample input:
3 2
1 2
2 3


Sample output:
2

Submission status: Accepted

Solution code:

import sys
from collections import deque
import copy


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


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
m = {}
m[1] = {
    'count': 1,
    'passed': True
}
for i in range(2,N+1):
    m[i] ={ 
        'count': 1,
        'passed': False
    }
for _ in range(M):
    a, b = [int(x) for x in input().split()]
    m[b]['count'] += 1
    if m[a]['passed']:
        m[b]['passed'] = True
    m[a]['count'] -= 1
    if m[a]['count'] == 0:
        m[a]['passed'] = False
count = 0
for key in m:
    if m[key]['passed']:
        count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

redded = [False] * (N+1)
redded[1] = True
boll_num = [1] * (N+1)

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

ans = 0
for r, b in zip(redded, boll_num):
    if r and b:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
a = []
for i in range(M):
    a.append(input())
 
b = [0 for i in range(N)]
b[0] = 1
c = [1 for i in range(N)]
for d in a:
    fr,to = map(int,d.split())
    fr -=1
    to -=1
    c[fr ] -= 1
    c[to ] += 1
    if b[fr] > 0:
        b[to] = 1
        b[fr] = 1 if c[fr] > 0 else 0
 
ans = b.count(1)
print(ans)

Submission status: Accepted

Solution code:
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 for i in range(N)]
a[0]=1
b=[1 for i in range(N)]
for i in range(M):
    b[x[i]]-=1
    b[y[i]]+=1
    if a[x[i]]==1:
        a[y[i]]=1
    if b[x[i]]==0:
        a[x[i]]=0
print(sum(a))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
XY = [list(map(int, input().split())) for i in range(M)]
boxes = [1] * (N+1)
reded = [False] * (N+1)
boxes[0] = 0
reded[1] = True

for x, y in XY:
    boxes[x] -= 1
    boxes[y] += 1

    if reded[x]:
        reded[y] = True

    if boxes[x] == 0:
        reded[x] = False

ans = 0
for n, r in zip(boxes, reded):
    if n and r:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:


n, m = map(int, raw_input().split())
balls = [1] * n
reds = [False] * n
reds[0] = True

for _ in range(m):
    a, b = map(int, raw_input().split())
    a -= 1
    b -= 1
    balls[a] -= 1
    balls[b] += 1

    if reds[a]:
        reds[b] = True

    if balls[a] == 0:
        reds[a] = False

print(reds.count(True))

Submission status: Accepted

Solution code:
s = raw_input().split()
n = int(s[0])
m = int(s[1])

red = [False for x in range(n)]
red[0] = True

cnt = [1 for x in range(n)]

for _ in xrange(0, m):
    s = raw_input().split()
    x = int(s[0]) - 1
    y = int(s[1]) - 1

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

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

print(sum(red))

Submission status: Accepted

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

for i in range(M):
  a,b=map(int,input().split())
  if A[a-1][1]==1:
    A[b-1][1]=1
  A[a-1][0]-=1
  A[b-1][0]+=1
  if A[a-1][0]==0:
    A[a-1][1]=0
    
ans=0
for j in range(N):
  if A[j][1]==1:
    ans+=1
print(ans)

Submission status: Accepted

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

print sum(red)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split(' '))
box=[1]*n
red=[0]*n
red[0]=1
for i in range(m):
  x,y=map(lambda x:x-1,map(int,input().split(' ')))
  if box[x] == 1:
    red[y]=max(red[y],red[x])
    red[x]=0
  else:
    red[y]=max(red[y],red[x])
  box[x]-=1
  box[y]+=1
print(red.count(1))

Submission status: Accepted

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

lst = [[int(i) for i in input().split()] for i in range(m)]

cnt = [1 for _ in range(n)]
r = [0 for _ in range(n)]
r[0] = 1
for e in lst:
    x = e[0] - 1
    y = e[1] - 1
    if r[x] == 1:
        r[y] = 1
    cnt[x] -= 1
    cnt[y] += 1
    if cnt[x] == 0:
        r[x] = 0

print(sum(r))

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = 10 ** 10

def main():
    N, M = map(int, input().split())
    num = [1] * N
    mark = [1] + [0] * (N - 1)
    for _ in range(M):
        x, y = map(int, input().split())
        x -= 1
        y -= 1

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

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

        if num[x] == 0:
            mark[x] = 0

    print(sum(mark))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(sum(red))

Submission status: Accepted

Solution code:
from sys import stdin

def main():
	N,M = [int(x) for x in stdin.readline().strip().split()]
	cantidad = [ 1 for x in range(N)]
	tenencia = [ 0 for x in range(N)]
	tenencia[0] = 1
	for i in range(M):
		x,y = [int(x) for x in stdin.readline().strip().split()]
		cantidad[y-1]+=1
		cantidad[x-1]-=1
		if tenencia[x-1] == 1:
			tenencia[y-1] = 1
		if cantidad[x-1] == 0:
			tenencia[x-1] = 0
	ans,i = 0,0
	while i < N:
		if cantidad[i] >= 1 and tenencia[i] == 1:
			ans+=1
		i+=1
	print(ans)
main()

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().strip().split()]
    lista = [1]*(n+1)
    vis = [False]*(n+1)
    vis[1] = True
    for w in range(m):
        x,y = [int(z) for z in stdin.readline().strip().split()]
        if(vis[x]):
            lista[y]+=1;
            lista[x]-=1;
            vis[y] = True
            if(lista[x] == 0):
                vis[x] = False
        else:
            lista[y]+=1;
            lista[x]-=1;
    print(vis.count(True))

main()

Submission status: Accepted

Solution code:
#coding=UTF-8

mozir=input()
hyo=mozir.split(' ')
Boxes=int(hyo[0])
Opes=int(hyo[1])

Red={0}
Balls=[1]*Boxes

for idx in range(0,Opes,1):
    mozir=input()
    hyo=mozir.split(' ')
    Sousa=((int(hyo[0])-1,int(hyo[1])-1))

    if Balls[Sousa[0]]==1:
        if Sousa[0] in Red:
            Red.remove(Sousa[0])
            Red.update({Sousa[1]})
        else:
            pass

    else:
        if Sousa[0] in Red:
            Red.update({Sousa[1]})
        else:
            pass
        
    Balls[Sousa[0]]-=1
    Balls[Sousa[1]]+=1
    #print(Red)

print(len(Red))

Submission status: Accepted

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

print(sum(prob))

Submission status: Accepted

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


flag = []
ball = []

for i in range(n):
    flag.append(False)
    ball.append(1)

flag[0] = True

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

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

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

for i in range(n):
    ans += flag[i]

print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,raw_input().split())
ans=[False]*n
ans[0]=True
cnt=[1]*n
for i in xrange(m):
    x,y=map(int,raw_input().split())
    x-=1;y-=1
    if cnt[x]==1 and ans[x]:
        ans[x]=False
        ans[y]=True
    elif ans[x]:
        ans[y]=True
    cnt[x]-=1
    cnt[y]+=1
print sum(ans)

Submission status: Accepted

Solution code:
n,m = map(int, raw_input().split())
rl = [False for _ in range(n)]
wl = [1 for _ in range(n)]
rl[0] = True
for _ in range(m):
    x,y = map(int, raw_input().split())
    if rl[x-1]:
        rl[y-1] = True
    wl[y-1] += 1
    wl[x-1] -= 1
    if wl[x-1] == 0:
        rl[x-1] = False
print rl.count(True)

Submission status: Accepted

Solution code:
temp=input().split()
N=int(temp[0])
M=int(temp[1])
temp=[input().split() for i in range(M)]

x=[int(temp[i][0])-1 for i in range(M)]
y=[int(temp[i][1])-1 for i in range(M)]
flag=[False for i in range(N)]
lest=[1 for i in range(N)]

flag[0]=True

for i in range(M):
	if flag[x[i]]==True:
		if lest[x[i]]==1:
			flag[x[i]]=False
		flag[y[i]]=True
	lest[x[i]]-=1
	lest[y[i]]+=1

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

Submission status: Accepted

Solution code:
ii=lambda:map(int,raw_input().split())
n,m=ii()
q=[ii() for _ in range(m)]
r=[1]+[0]*(n-1)
b=[1]*n
for x,y in q:
    b[x-1]-=1;b[y-1]+=1
    if r[x-1]==1:r[y-1]=1
    if b[x-1]==0:r[x-1]=0
print sum(r)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

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


def f():
    n,m = list(map(int, input().split()))
    fs = [0] * (n+1)
    fs[1] = 1
    a = [1] * (n+1)
    for _ in range(m):
        x,y = list(map(int, input().split()))
        a[x] -= 1
        a[y] += 1
        if fs[x]:
            fs[y] = 1
        if a[x] < 1:
            fs[x] = 0
    return sum(fs)

print(f())

Submission status: Accepted

Solution code:
n,m=map(int,raw_input().split())
rw=[1]*n
rball=[True]+[False]*(n-1)
for i in xrange(m):
    x,y=map(int,raw_input().split())
    x-=1;y-=1
    if rw[x]==1:
        if rball[x]:
            rball[x]=False
            rw[x]-=1
            rw[y]+=1
            rball[y]=True
        else:
            rw[x]-=1
            rw[y]+=1
    else:
        rw[x]-=1
        rw[y]+=1
        if rball[x]:
            rball[y]=True
ans=0
for i in xrange(n):
    if rball[i]:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
NM = input()
NM = "".join(NM).rstrip("\n").split(" ")
lis = [1 for s in range(int(NM[0]))]
lis2 =[0 for s in range(int(NM[0]))]
lis2[0]+=1
#print(lis)
for s in range(int(NM[1])):
	temp=input()
	temp = "".join(temp).rstrip("\n").split(" ")
	#print(lis2)
	t0 = int(temp[0])
	t1 = int(temp[1])
	lis[t1-1]+=1
	if lis2[t0-1]==1:
		lis2[t1-1]=1
	lis[t0-1]-=1
	if lis[t0-1] ==0:
		if lis2[t0-1]>0:
			lis2[t0-1]=0
#print(lis)
#print(lis2)
ans=0
for s in range(int(NM[0])):
	if lis2[s]==1:
		ans+=1
print(ans)

Submission status: Accepted

Solution code:
#AGC002B
n,m=map(int,raw_input().split())
a=[1 for _ in xrange(n)]
r=[0 for _ in xrange(n)]
r[0]=1
for i in xrange(m):
 x,y=map(int,raw_input().split())
 x-=1
 y-=1
 a[x]-=1
 a[y]+=1
 if r[x]==1: r[y]=1
 if a[x]==0: r[x]=0
 
print sum(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, l = (int(s) for s in input().strip().split(' '))
an = []
for a in range(0,l):
    an.append(input().strip().split())
box=[[1,0] for i in range(n)]
box[0][1] = 1
for move in an:
    box[int(move[0])-1][0] -= 1
    box[int(move[1])-1][0]+=1
    if box[int(move[0])-1][1] == 1:
        box[int(move[1])-1][1]= 1
    if box[int(move[0])-1][0]==0:
        box[int(move[0])-1][1]=0
print(str(sum([s[1] for s in box])))

Submission status: Accepted

Solution code:
import sys
nmlist = list(map(int,input().split(" ")))
N = nmlist[0]
M = nmlist[1]

moves = []
for i in range(M):
    moves.append(list(map(int,input().split(" "))))

box = [[1,0] for i in range(N)]
box[0][1] = 1

for i in range(M):
    before = moves[i][0] - 1
    after = moves[i][1] - 1
    if box[before][0] != 0:
        box[before][0] -= 1
        if box[before][1] == 1:
            box[after][1] = 1
        if box[before][0] == 0:
            box[before][1] = 0
        box[after][0] += 1

output = 0
for i in range(N):
    output += box[i][1]

print(output)

Submission status: Accepted

Solution code:
class Box:
    def __init__(self, num=1, mayred=False):
        self.num = num
        self.mayred = mayred

n, m = (int(s) for s in input().strip().split(' '))
boxes = [Box() for _ in range(n)]
boxes[0].mayred = True
for _ in range(m):
    x, y = (int(s) for s in input().strip().split(' '))
    boxes[x-1].num -= 1
    boxes[y-1].num += 1
    if boxes[x-1].mayred == True:
        boxes[y-1].mayred = True
        if boxes[x-1].num == 0:
            boxes[x-1].mayred = False
print(len(list(filter(lambda b: b.mayred, boxes))))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
move = []
for i in range(M):
	move.append(list(map(int,input().split())))
box = [1 for i in range(N)]
box_flag = [False for i in range(N)]
box_flag[0] = True
for i in range(len(move)):
	box[move[i][0]-1] -= 1
	box[move[i][1]-1] += 1
	if box_flag[move[i][0]-1]:
		box_flag[move[i][1]-1] = True
	if box[move[i][0]-1] == 0:
		box_flag[move[i][0]-1] = False
print(box_flag.count(True))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(count)

Submission status: Accepted

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())
    num[x] -= 1
    num[y] += 1
    if x in red:
        red.add(y)
        if num[x] == 0:
            red.remove(x)
print len(red)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  1 00:19:18 2016

@author: dotha
"""


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

print(table2.count(True))

Submission status: Accepted

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 f[x]:
        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: Accepted

Solution code:
n, m = map(int, raw_input().split())
x, y = [], []
for i in range(m):
    x0, y0 = map(int, raw_input().split())
    x.append(x0-1)
    y.append(y0-1)
box = [1 for i in range(n)]
can = [False for i in range(n)]
can[0] = True
for i,j in zip(x,y):
    box[i] -= 1
    box[j] += 1
    if can[i]:
        if box[i] == 0:
            can[j] = True
            can[i] = False
        else:
            can[j] = True
print can.count(True)

Submission status: Accepted

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

from __future__ import division, print_function, absolute_import, unicode_literals

def main():
    N, M = map(int, input().split())
    p = [1] + [1] * (N-1)
    plob = [True] + [False] * (N-1)
    for _ in range(M):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        if p[x] == 0:
            continue
        p[x] -= 1
        p[y] += 1
        if plob[x] is True:
            if p[x] == 0:
                plob[x] = False
                plob[y] = True
            else:
                plob[y] = True
        # print(p)
        # print(plob)
    print(sum(plob))
    # for i in range(N):
    #     if plob[i]:
    #         print(i+1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, raw_input().split())
x = []
for i in xrange(M):
    x.append(map(int, raw_input().split()))
hako=[]
for i in xrange(N+1):
    if i==1:
        hako.append([1,1])
    else:
        hako.append([0,1])
for move in x:
    hako[move[0]][1]=hako[move[0]][1]-1
    hako[move[1]][1]=hako[move[1]][1]+1
    if hako[move[0]][0]==1:
        hako[move[1]][0]=1
    if hako[move[0]][1]==0:
        hako[move[0]][0]=0
s=0
for i in xrange(N+1):
    s=s+hako[i][0]
print s

Submission status: Accepted

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

Submission status: Accepted

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

N, M = map(int, input().split())
B = {x+1:1 for x in range(N)}
f = {x+1:0 for x in range(N)}
f[1] = 1
for i in range(M):
    x, y = map(int, input().split())
    B[x] -= 1
    B[y] += 1
    if f[x] : f[y] = 1
    if B[x] < 1 : f[x] = 0
ans = 0
for k, v in f.items() : ans += v
print(ans)

Submission status: Accepted

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

for i in range(m):
    xi, yi = map(int, input().split())
    xi-=1
    yi-=1
    ball[xi] = ball[xi] - 1
    ball[yi] = ball[yi] + 1
    if(flag[xi]==1):
        flag[yi] = 1
    if(ball[xi]==0):
        flag[xi]=0
for i in range(len(flag)):
    if(flag[i]==1):
        count+=1
print(count)

Submission status: Accepted

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.add(y)
        if b[x]==0 :
            lst.remove(x)
#ans=0
#for i in range(1,N+1):
    #if(b[i]!=0 and i in lst):
        #ans+=1

print(len(lst))

Submission status: Accepted

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: Accepted

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

import array


def main():
    n, m = map(int, input().split())
    deg = array.array("l", (0 for _ in range(n)))
    can_have_red = array.array("B", (False for _ in range(n)))
    can_have_red[0] = True
    for _ in range(m):
        x, y = map(lambda x: int(x) - 1, input().split())
        deg[x] -= 1
        deg[y] += 1
        if can_have_red[x]:
            can_have_red[y] = True
        if deg[x] < 0:
            can_have_red[x] = False
    print(sum(can_have_red))


if __name__ == '__main__':
    main()

Submission status: Accepted

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
    else:
        w[x-1]-=1
        w[y-1]+=1

    if w[x-1]==0:
        s[x-1]=0
print s.count(1)

Submission status: Accepted

Solution code:
import re
import functools

class IO_for_Contest(object):
    @staticmethod
    def my_input():
        #return raw_input()
        return input()

    @staticmethod
    def read_from_input():
        n, m = IO_for_Contest.read_n_int(2)
        x, y = [], []
        for i in range(m):
            xx, yy = IO_for_Contest.read_n_int(2)
            x.append(xx)
            y.append(yy)
        return n, x, y

    @staticmethod
    def read_line():
        return IO_for_Contest.my_input().strip()

    @staticmethod
    def read_int():
        return int(IO_for_Contest.my_input().strip())

    @staticmethod
    def read_n_int(n):
        return list(map(int, re.split('\W+', IO_for_Contest.my_input().strip())))[ : n]

def solve():
    n, x, y = IO_for_Contest.read_from_input()
    m = len(x)
    print(count_boxes_can_have_red_ball(n, m, x, y))

def count_boxes_can_have_red_ball(n, m, x, y):
    """
    >>> n, m, x, y = 3, 2, [1, 2], [2, 3]
    >>> count_boxes_can_have_red_ball(n, m, x, y)
    2
    """
    balls = [0]
    red_ball_can_exist = [None]
    for i in range(n):
        if i == 0:
            red_ball_can_exist.append(True)
        else:
            red_ball_can_exist.append(False)
        balls.append(1)
    for i in range(m):
        f, t = x[i], y[i]
        balls[f] -= 1
        balls[t] += 1
        if red_ball_can_exist[f]:
            if balls[f] <= 0:
                red_ball_can_exist[f] = False
            red_ball_can_exist[t] = True
        else:
            pass
        #print('{0:d}, {1:s}, {2:s}'.format(i, str(balls), str(red_ball_can_exist)))
    return red_ball_can_exist.count(True)

if __name__ == '__main__':
    #import doctest
    #doctest.testmod()
    solve()

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import repeat
def main():
    n, m = map(int, stdin.readline().split())
    dat = map(int, stdin.read().split(), repeat(10, 2 * m))
    a = [0] + [2] * n
    a[1] = 1
    for i in xrange(m):
        x, y = dat[i*2], dat[i*2+1]
	if a[x] == 0:
            continue
        a[y] += 2
        if a[x] & 1 and a[y] & 1 ^ 1:
            a[y] -= 1
        a[x] -= 2
	if a[x] < 0:
            a[x] = 0
    print sum(x & 1 for x in a)
main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
NM = map(int, raw_input().split(" "))
N = NM[0]
M = NM[1]
containRed = [True]
for i in xrange(N-1):
    containRed.append(False)

balles = [1]
for i in xrange(N-1):
    balles.append(1)

for i in xrange(M):
    xiyi = map(int, raw_input().split(" "))
    xi = xiyi[0] - 1
    yi = xiyi[1] - 1
    if containRed[xi]:
        if balles[xi] == 1:
            balles[xi] = 0
            containRed[xi] = False
            balles[yi] += 1
            containRed[yi] = True
        else:
            balles[xi] -= 1
            balles[yi] += 1
            containRed[yi] = True
    else:
        balles[xi] -= 1
        balles[yi] += 1
    
print sum(containRed)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys

def s():
    return raw_input().strip()
def n():
    return int(raw_input())
def d():
    return float(raw_input())

def ls():
    return raw_input().strip().split()
def ln():
    return map(int, raw_input().strip().split())
def ld():
    return map(float, raw_input().strip().split())

def fs():
    return [raw_input().strip() for i in xrange(input())]
def fn():
    return [int(raw_input().strip()) for i in xrange(input())]
def fd():
    return [float(raw_input().strip()) for i in xrange(input())]

N, M = ln()

XY = []
for i in xrange(M):
    XY.append(ln())

candidate = set([1])
num = [0] + [1] * N
for x, y in XY:
    num[x] -= 1
    num[y] += 1
    if x in candidate:
        candidate.add(y)
    if num[x] == 0:
        candidate.discard(x)

print len(candidate)

Submission status: Accepted

Solution code:
N, M = map(int, raw_input().split())
num_ball = [1] * N
red_ball = [1] + [0] * (N-1)
for i in range(M):
    x, y = map(int, raw_input().split())
    x -= 1
    y -= 1
    
    if red_ball[x]:
        red_ball[y] = 1
    
    num_ball[x] -=1
    num_ball[y] +=1
    
    if num_ball[x] == 0:
        red_ball[x] = 0
        
print sum(red_ball)

Submission status: Accepted

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

c = [1] * N
p = [0] * N

p[0] = 1

for counter in range(M):
    x, y = input().split()
    x, y = int(x) - 1, int(y) - 1

    if(c[x] > 0):
        c[x] -= 1
        c[y] += 1

        if(p[x]==1):
            p[y]=1
        if(c[x]==0):
            p[x]=0

sum = 0
for i in range(N):
    if(p[i]>0):
        sum+=1

print(sum)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [1 for k in range(N)]
B = [0 for k in range(N)]

ans = 0
B[0] = 1
for k in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    if B[x] == 1 and A[x] >= 2:
        A[x] -= 1
        A[y] += 1
        B[y] = 1
    elif B[x] == 1 and A[x] == 1:
        A[x] -= 1
        A[y] += 1
        B[x] = 0
        B[y] = 1
    else:
        A[x] -= 1
        A[y] += 1

print(B.count(1))

Submission status: Accepted

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 res

Submission status: Accepted

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[x] += 1
ans = 0
for ai in a:
    if ai > 0:
        ans += 1
print ans

Submission status: Accepted

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

Submission status: Accepted

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

##
dp = [False]*(N+1)
dp[1] = True
ball = [1]*(N+1)

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

cnt = 0
for i in range(1, N+1):
	if dp[i]:
		cnt += 1

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m= map(int,raw_input().split())
box = [1 for _ in range(n+1)]
red = [0 for _ in range(n+1)]
red[1] = 1
for i in xrange(m):
    x,y = map(int,raw_input().split())
    box[x] -= 1
    box[y] += 1
    if red[x] == 1:
        red[y] = 1
    if box[x] == 0:
        red[x] = 0

# print box
# print red
print sum(red)