๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 18243 - Small World Network

by HandHand 2021. 3. 18.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 18243๋ฒˆ

ํ’€์ด ๊ณผ์ •

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์€ ์„ธ์ƒ ๋„คํŠธ์›Œํฌ ๋ฅผ ๋งŒ์กฑํ•˜๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์นœ๊ตฌ์‚ฌ์ด์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ๊ฑฐ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๊ณ„์‚ฐํ•ด์„œ 6๋‹จ๊ณ„ ์ดํ•˜์ธ์ง€ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์„œ ๋ชจ๋“  ์ •์ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ ๊ฐ’์„ O(N^3) ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ๊ณ„์‚ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys

N, K = list(map(int, sys.stdin.readline().split()))
edges = [list(map(int, sys.stdin.readline().split())) for _ in range(K)]


def make_graph():
    graph = [[sys.maxsize] * N for _ in range(N)]

    for frm, to in edges:
        graph[frm - 1][to - 1] = 1
        graph[to - 1][frm - 1] = 1

    return graph


def floyd(graph):
    for v in range(N):
        graph[v][v] = 0

    for k in range(N):
        for u in range(N):
            for v in range(N):
                graph[u][v] = min(graph[u][v], graph[u][k] + graph[k][v])


def solution():
    graph = make_graph()
    floyd(graph)

    for frm in range(N):
        for to in range(N):
            if graph[frm][to] > 6:
                return 'Big World!'

    return 'Small World!'


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 1543 - ๋ฌธ์„œ ๊ฒ€์ƒ‰  (0) 2021.03.18
BOJ 11403 - ๊ฒฝ๋กœ ์ฐพ๊ธฐ  (0) 2021.03.18
BOJ 18238 - ZOAC2  (0) 2021.03.18
BOJ 14754 - Pizza Boxes  (0) 2021.03.18
BOJ 15723 - n๋‹จ ๋…ผ๋ฒ•  (0) 2021.03.18

๐Ÿ’ฌ ๋Œ“๊ธ€