-
문제
코드
computer = int(input()) # 컴퓨터 수 입력 network_num = int(input()) # 네트워크 수 입력 network_lines = [[0] * computer for i in range(computer)] // network matrix visited = [0 for i in range(computer)] // 방문여부 => 0으로 초기화 count = 0 for i in range(network_num): a, b = map(int, input().split()) network_lines[a - 1][b - 1] = 1 network_lines[b - 1][a - 1] = 1 // Creating adjacent matrix def dfs(line): visited[line] = 1 for i in range(computer): # 연결이 되어있고, 방문한 적이 없으면 DFS 재귀 if network_lines[line][i] == 1 and visited[i] == 0: dfs(i) dfs(0) for i in range(1, computer): # 컴퓨터 수만큼 루프를 돌리고, 방문한 곳이 있는 만큼 COUNT를 증가 if visited[i] == 1: count += 1 print(count)
'PS > 백준' 카테고리의 다른 글
[백준_1012] 유기농배추 by python ( DFS ) (0) 2021.01.16 [백준_2644] 촌수계산 by python ( BFS ) (0) 2021.01.16 [백준_1110] 더하기 사이클 ( 구현 [Implementation] ) (0) 2021.01.16 [백준_1783] 병든나이트 (구현, 그리디) (0) 2021.01.16 [백준_14470] 전자레인지( 구현[Implementation] ) (0) 2021.01.16 댓글