ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 6118 ์ˆจ๋ฐ”๊ผญ์งˆ with Python
    PS 2021. 12. 1. 22:23
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 6118 ์ˆจ๋ฐ”๊ผญ์งˆ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ํ—›๊ฐ„์˜ ๊ฐœ์ˆ˜ (2 <= N <= 20,000), 1๋ถ€ํ„ฐ ์„ธ์•„๋ฆฐ๋‹ค.

    2. ๋ชจ๋“  ํ—›๊ฐ„์€ (1<= M <= 50,000)๊ฐœ์˜ ์–‘๋ฐฉํ–ฅ ๊ธธ๋กœ ์ด์–ด์ ธ ์žˆ๋‹ค.

    3. ๋ƒ„์ƒˆ๋Š” 1๋ฒˆ ํ—›๊ฐ„์—์„œ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋ฉ€์–ด์งˆ์ˆ˜๋ก ๊ฐ์†Œํ•œ๋‹ค.
      ๊ฑฐ๋ฆฌ = ์ง€๋‚˜์•ผ ํ•˜๋Š” ๊ธธ์˜ ์ตœ์†Œ ๊ฐœ์ˆ˜.

    4. ์ˆจ์–ด์•ผ ํ•˜๋Š” ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ๊ฐ€ ๋จผ ํ—›๊ฐ„ ๋ฒˆํ˜ธ, ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ๊ฐ€ ๋จผ ํ—›๊ฐ„๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ, ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ๊ฐ€ ๋จผ ํ—›๊ฐ„๊ณผ ๊ฐ™์€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง€๋Š” ํ—›๊ฐ„์˜ ์ˆ˜
      ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฉฐ, ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ๊ฐ€ ๋จผ ํ—›๊ฐ„ ๋ฒˆํ˜ธ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋ผ๋ฉด ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

    5. ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    from collections import deque
    
    n, m = map(int, stdin.readline().split())
    arr = [[] for _ in range(n + 1)]
    for _ in range(m):
        a, b = map(int, stdin.readline().split())
        arr[a].append(b)
        arr[b].append(a)
    
    res = [[] for _ in range(20001)]
    max_dist = -1e9
    
    
    def bfs(n):
        global max_dist
        q = deque()
        q.append((0, n))
        visited = set()
        visited.add(n)
    
        while q:
            dist, now = q.popleft()
            max_dist = max(max_dist, dist)
            for node in arr[now]:
                if node not in visited:
                    visited.add(node)
                    q.append((dist + 1, node))
                    res[dist + 1].append(node)
    
    
    bfs(1)
    print(min(res[max_dist]), max_dist, len(res[max_dist]))

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    6 7
    3 6
    4 3
    3 2
    1 3
    1 2
    2 4
    5 2

    ์‹คํ–‰๊ฒฐ๊ณผ

    4 2 3

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋…ธ๋“œ๋“ค์„ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด 2์ฐจ์› ๋ฆฌ์ŠคํŠธ์ธ arr์„ ์ƒ์„ฑํ•œ ํ›„ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

    2. ๊ฒฐ๊ณผ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , ํ—›๊ฐ„์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜๋ฏธํ•˜๋Š” res 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
      ๊ฐ€์žฅ ๋จผ ํ—›๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•  max_dist ๋ณ€์ˆ˜๋„ ์ƒ์„ฑํ•œ๋‹ค.

    3. ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ํ์—๋Š” (๊ฑฐ๋ฆฌ, ๋…ธ๋“œ๋ฒˆํ˜ธ)๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
      ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ, ํ•ด๋‹น ํ—›๊ฐ„๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ํ—›๊ฐ„์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค.
      ํ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ pop() ํ•˜์—ฌ ๋‚˜์˜จ ๊ฑฐ๋ฆฌ ๋ฐ์ดํ„ฐ๊ฐ€ ํ˜„์žฌ max_dist ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ฐฑ์‹ ํ•œ๋‹ค.

    4. ๋ฐฉ๋ฌธํ•œ ํ—›๊ฐ„์€ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋˜ ํ—›๊ฐ„์ด๋ผ๋Š” ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด visited ์ง‘ํ•ฉ ์ž๋ฃŒํ˜•์— ๋…ธ๋“œ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•œ๋‹ค.

    5. ํ—›๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋Š” ๋ชจ๋‘ 1์”ฉ์ด๊ธฐ์—, ํ์— (ํ˜„์žฌ ํ—›๊ฐ„๊ณผ์˜ ๊ฑฐ๋ฆฌ + 1, ๋…ธ๋“œ๋ฒˆํ˜ธ) ๋ฅผ ์ €์žฅํ•œ๋‹ค.

    6. res ๋ฆฌ์ŠคํŠธ์— ๊ฑฐ๋ฆฌ + 1์— ํ•ด๋‹นํ•˜๋Š” ๋ฆฌ์ŠคํŠธ์— ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•œ๋‹ค.
      res ๋ฆฌ์ŠคํŠธ์—๋Š” ๊ฐ€์žฅ ๋จผ ๊ฑฐ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ์›์†Œ ๋ฆฌ์ŠคํŠธ์— ๊ฐ™์€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง„ ํ—›๊ฐ„์˜ ๋ฒˆํ˜ธ๋“ค์ด ์ €์žฅ๋œ๋‹ค.

    7. ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•œ ๊ฒƒ๋“ค์„ ์ถœ๋ ฅํ•œ๋‹ค.
      ์ˆจ์–ด์•ผ ํ•˜๋Š” ํ—›๊ฐ„์˜ ๋ฒˆํ˜ธ = min(res[max_dist])
      ์ˆจ์–ด์•ผ ํ•˜๋Š” ํ—›๊ฐ„๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ = max_dist
      ์ˆจ์–ด์•ผ ํ•˜๋Š” ํ—›๊ฐ„๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์™€ ๊ฐ™์€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง„ ํ—›๊ฐ„๋“ค = len(res[max_dist])

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. BFS ์‘์šฉ ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ’€์ด๋ฅผ ํ•˜๋Š”๋ฐ์—๋Š” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€ ์•Š์•˜๋‹ค.
    2. ๊ทธ๋ž˜ํ”„ ์ด๋ก  ๋ฐ BFS ๋ฌธ์ œ๋Š” ์กฐ๊ธˆ์”ฉ ๋” ํ’€์–ด์„œ ๋‚ด๊ฒƒ์œผ๋กœ ๋งŒ๋“ค์–ด๋„ ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.