문제 링크

문제 설명

예시

[input]
3 2
1 2
2 3

[output]
2


[input]
3 3
1 2
2 3
2 3

[output]
1

[input]
4 4
1 2
2 3
4 1
3 4

[output]
3

사고의 흐름

모범답안

1. 여기를 누르면 답이 보입니다
redBalles = []
currentRedBall = 0

def putBall(i, balles, routes):
    global redBalles
    global currentRedBall
    if i == len(routes):
        redBalles[currentRedBall] = 1
        return None
    _from = routes[i][0]-1
    _to = routes[i][1]-1
    if balles[_from]%2 == 0:
        balles[_from] -= 2
        balles[_to] += 2
        putBall(i+1, balles, routes)
    else:
        if balles[_from] == 1:
            balles[_from] -= 1
            balles[_to] += 1
            currentRedBall = _to
            putBall(i+1, balles, routes)
        else:
            _balles = [b for b in balles]
            _balles[_from] -= 1
            _balles[_to] += 1
            currentRedBall = _to
            putBall(i+1, _balles, routes)
            
            _balles = [b for b in balles]
            _balles[_from] -= 2
            _balles[_to] += 2
            currentRedBall = _from
            putBall(i+1, _balles, routes)

def answer(n, routes):
    global redBalles
    redBalles = [0]*n
    balles = [1] + [2]*(n-1)
    putBall(0, balles, routes)
    return sum(redBalles)

n, m = map(int, input().split())
routes = []
for _ in range(m):
    routes.append(list(map(int, input().split())))
print(answer(n, routes))

틀렸다..

2. 여기를 누르면 답이 보입니다