본문 바로가기

카테고리 없음

[백준 11377번] 열혈강호 3 (Python3)

import sys
input = sys.stdin.readline

def DFS(i):
  if visited[i]:
    return 0
  visited[i] = 1
  for w in graph[i]:
    if work[w]<0:
      work[w] = i
      return 1
  for w in graph[i]:
    if DFS(work[w]):
      work[w] = i
      return 1
  return 0

N,M,K = map(int,input().split())
graph = [[*map(int,input().split())][1:] for i in range(N)]
work = [-1]*(M+1)
for i in range(N):
  visited = [0]*N
  DFS(i)
i = k = 0
while i<N and k<K:
  visited = [0]*N
  k += DFS(i)
  i += 1
print(M+1-work.count(-1))

= 열혈강호 [백준 11375번] 열혈강호 (Python3) (tistory.com)

한번은 이분매칭을 1~N까지 쭉 돌리고, 두번째에는 1~N까지 돌리되, 매칭성공횟수가 K번이 될때까지만 돌리면 된다.