ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 1325 ํšจ์œจ์ ์ธ ํ•ดํ‚น (BFS)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 2. 15:33

     

    ๐Ÿ“Œ ๋ฌธ์ œ

    ํ•ด์ปค ๊น€์ง€๋ฏผ์€ ์ž˜ ์•Œ๋ ค์ง„ ์–ด๋Š ํšŒ์‚ฌ๋ฅผ ํ•ดํ‚นํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด ํšŒ์‚ฌ๋Š” N๊ฐœ์˜ ์ปดํ“จํ„ฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊น€์ง€๋ฏผ์€ ๊ท€์ฐฎ๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ๋ฒˆ์˜ ํ•ดํ‚น์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚น ํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

     

    ์ด ํšŒ์‚ฌ์˜ ์ปดํ“จํ„ฐ๋Š” ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„์™€, ์‹ ๋ขฐํ•˜์ง€ ์•Š๋Š” ๊ด€๊ณ„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”๋ฐ, A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” B๋ฅผ ํ•ดํ‚นํ•˜๋ฉด, A๋„ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์†Œ๋ฆฌ๋‹ค.

     

    ์ด ํšŒ์‚ฌ์˜ ์ปดํ“จํ„ฐ์˜ ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ•œ ๋ฒˆ์— ๊ฐ€์žฅ ๋งŽ์€ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

    ์ž…๋ ฅ

    ์ฒซ์งธ ์ค„์—, N๊ณผ M์ด ๋“ค์–ด์˜จ๋‹ค. N์€ 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜, M์€ 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„๊ฐ€ A B์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ๋“ค์–ด์˜ค๋ฉฐ, "A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•œ๋‹ค"๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ปดํ“จํ„ฐ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ํ•˜๋‚˜์”ฉ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค.

    ์ถœ๋ ฅ

    ์ฒซ์งธ ์ค„์—, ๊น€์ง€๋ฏผ์ด ํ•œ ๋ฒˆ์— ๊ฐ€์žฅ ๋งŽ์€ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    5 4
    3 1
    3 2
    4 3
    5 3

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    1 2

     


     

    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    from collections import deque
    input = sys.stdin.readline
    
    
    def bfs(node):
        q = deque([node])
        visited = [0] * (n + 1)
        visited[node] = 1
        cnt = 1
        while q:
            v = q.popleft()
            for i in graph[v]:
                if not visited[i]:
                    q.append(i)
                    visited[i] = 1
                    cnt += 1
        return cnt
    
    
    if __name__ == '__main__':
        n, m = map(int, input().split())
        graph = [[] for _ in range(n + 1)]
    
        for _ in range(m):
            a, b = map(int, input().split())
            graph[b].append(a)
    
        maximum = 0
        idx = []
    
        for i in range(1, n + 1):
            ret = bfs(i)
            if maximum < ret:
                idx = [i]
                maximum = ret
            elif maximum == ret:
                idx.append(i)
    
        print(*idx, sep=' ')

     

    ๐Ÿ’ก Solution

    A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•  ๋•Œ, B๋งŒ ํ•ดํ‚นํ•˜๋ฉด A๋„ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ B์˜ ์—ฐ๊ฒฐ ์ •๋ณด์— A๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๋‹จ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋กœ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” B๋ฅผ ํ•ดํ‚นํ–ˆ์„ ๋•Œ ๋ช‡ ๊ฐœ์˜ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋Š”์ง€ 'B์˜ ์—ฐ๊ฒฐ ์ •๋ณด'๋งŒ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. A์˜ ์—ฐ๊ฒฐ ์ •๋ณด๋Š” ์ด ๋ฌธ์ œ์—์„  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

     

    bfs(i)ํ•จ์ˆ˜์— cnt ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•ด i๋ฅผ ํ•ดํ‚นํ•˜๋ฉด ๋ช‡ ๊ฐœ์˜ ์ปดํ“จํ„ฐ๋ฅผ ๋ค์œผ๋กœ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋Š”์ง€ i์™€ ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ์˜ ๊ฐฏ์ˆ˜์ธ cnt๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

    bfs(i)ํ•จ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœํ•˜๋ฉด์„œ cnt๋ฅผ ๋น„๊ตํ•ด์•ผ ํ•˜๋ฏ€๋กœ, visited ๋ฐฐ์—ด์˜ ์„ ์–ธ์„ mainํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ bfs(i)ํ•จ์ˆ˜ ์•ˆ์— ํ•˜์—ฌ bfs๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ๋ฐฉ๋ฌธ ์ •๋ณด๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์–ด์•ผํ•จ์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     


    Python3๊ณผ Pypy3์˜ ์ฐจ์ด

    Python3์œผ๋กœ ์ œ์ถœํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ธฐ ๋•Œ๋ฌธ์— Pypy3์œผ๋กœ ์ œ์ถœํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‘˜์ด ๋ญ๊ฐ€ ๋‹ค๋ฅธ์ง€ ๊ถ๊ธˆํ•ด ์ฐพ์•„๋ดค์Šต๋‹ˆ๋‹ค. ๐Ÿค”

     

    • PyPy3๋Š” ์‹คํ–‰์‹œ ์ž์ฃผ ์“ฐ์ด๋Š” ์ฝ”๋“œ๋ฅผ ์บ์‹ฑํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ฆ‰ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ Python3๋ณด๋‹ค ์กฐ๊ธˆ ๋” ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ด ์ฝ”๋“œ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์–ด ์‹คํ–‰์†๋„๋ฅผ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    • ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋Š” Python3๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ, ์†๋„ ์ธก์—์„œ ์šฐ์„ธํ•˜๊ณ 
      ๋ณต์žกํ•œ ์ฝ”๋“œ, ์ฃผ๋กœ ๋ฐ˜๋ณต์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—์„œ๋Š” PyPy3๊ฐ€ ์šฐ์„ธํ•˜๊ธฐ ๋•Œ๋ฌธ์—
      ์ฝ”๋“œ ์ƒํ™ฉ์— ๋งž์ถ”์–ด ๋‘˜์„ ์ ์žฌ์ ์†Œ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค! ๐Ÿ’ก

     

    defaultdict()

    ๋‹ค๋ฅธ ๋ถ„๋“ค์€ ์–ด๋–ป๊ฒŒ ํ‘ธ์…จ์„๊นŒ ๊ถ๊ธˆํ•ด์„œ ์ฐพ์•„๋ณด๋Š”๋ฐ graph๋ฅผ defaultdict๋กœ ์„ ์–ธํ•œ ํ’€์ด๊ฐ€ ๋งŽ์ด ๋ณด์˜€์Šต๋‹ˆ๋‹ค. defaultdict๋Š” ์ฒ˜์Œ ๋ณด๋Š” ์•„์ด๋ผ ๊ถ๊ธˆ์ฆ์ด ๋„์ ธ์„œ ์ง์ ‘ ๋Œ๋ ค๋ดค์Šต๋‹ˆ๋‹ค ๐Ÿค”

    • defaultdict ์‚ฌ์šฉ X
    graph = [[] for _ in range(n + 1)]
    …
    print(graph)
    # ์ถœ๋ ฅ๊ฒฐ๊ณผ [[], [3], [3], [4, 5], [], []]

    ์—ฐ๊ฒฐ์ •๋ณด๊ฐ€ ์—†๋Š” ์ปดํ“จํ„ฐ์˜ ๊ฒฝ์šฐ ๋นˆ ๋ฆฌ์ŠคํŠธ []๋กœ ๋‚จ์Šต๋‹ˆ๋‹ค.

    • defaultdict ์‚ฌ์šฉ O
    from collections import defaultdict
    graph = defaultdict(list)
    …
    print(graph)
    # ์ถœ๋ ฅ๊ฒฐ๊ณผ defaultdict(<class 'list'>, {1: [3], 2: [3], 3: [4, 5]})

    ์—ฐ๊ฒฐ์ •๋ณด๊ฐ€ ์—†๋Š” ์ปดํ“จํ„ฐ์˜ ๊ฒฝ์šฐ ์•„์˜ˆ key: value set์กฐ์ฐจ ์ƒ์„ฑ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    ์•„์ง ์–ด๋Š ์ƒํ™ฉ์—์„œ ์“ฐ๋ฉด ์ข‹์„์ง€ ๊ฐ์€ ์žกํž๋ž‘๋ง๋ž‘์ธ๋ฐ ์ข‹์€ ์ ‘๊ทผ์„ ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

    ๋Œ“๊ธ€

Designed by Tistory.