728x90
1. 백준 2660. 회장뽑기 (골5)
https://www.acmicpc.net/problem/2660
2660번: 회장뽑기
입력의 첫째 줄에는 회원의 수가 있다. 단, 회원의 수는 50명을 넘지 않는다. 둘째 줄 이후로는 한 줄에 두 개의 회원번호가 있는데, 이것은 두 회원이 서로 친구임을 나타낸다. 회원번호는 1부터
www.acmicpc.net
풀이
CODE
import math
import sys
input = sys.stdin.readline
n = int(input())
board = [[math.inf if i != j else 0 for j in range(n + 1)] for i in range(n + 1)]
a, b = map(int, input().split())
while a != -1:
board[a][b] = 1
board[b][a] = 1
a, b = map(int, input().split())
def floyd_warchall(graph):
# 플루이드-워셜 알고리즘
for k in range(1, n+1):
for i in range(1, n+1):
for j in range(1, n+1):
graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
return graph
result = floyd_warchall(board)
ans = {}
for i in range(1, n+1):
ans[i] = max(result[i][1:])
min_score = min(ans.values())
print(min_score, list(ans.values()).count(min_score))
for key, value in ans.items():
if value == min_score:
print(key, end=' ')
- 플로이드 워셜로 풀면서 메모리 초과가 나지 않을까 걱정했는데 다행히 안났다.
- 오예~
728x90
'알고리즘' 카테고리의 다른 글
[프로그래머스/Python] 이중우선순위큐 (0) | 2023.09.25 |
---|---|
[백준/Python] 25603. 짱해커 이동식 (0) | 2023.09.25 |
[백준/Python] 1107. 리모컨 (1) | 2023.09.24 |
[백준/Python] 14232. 보석도둑 (0) | 2023.09.24 |
[백준/Python] 6064. 카잉달력 (0) | 2023.09.24 |