Problem Solving/[ํ•ญํ•ด99] TIL

99ํด๋Ÿฝ ์ฝ”ํ…Œ ์Šคํ„ฐ๋”” 24์ผ์ฐจ TIL / ์™„์ „ํƒ์ƒ‰

geum 2024. 11. 20. 23:42

โœ๏ธ ๋ฌธ์ œ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Programmers] ์ „๋ ฅ๋ง์„ ๋‘˜๋กœ ๋‚˜๋ˆ„๊ธฐ(https://school.programmers.co.kr/learn/courses/30/lessons/86971)

 

n๊ฐœ์˜ ์†ก์ „ํƒ‘์ด ์ „์„ ์„ ํ†ตํ•ด ํ•˜๋‚˜์˜ ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ์ด ์ „์„ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Š์–ด์„œ ํ˜„์žฌ์˜ ์ „๋ ฅ๋ง ๋„คํŠธ์›Œํฌ๋ฅผ 2๊ฐœ๋กœ ๋ถ„ํ• ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๋‘ ์ „๋ ฅ๋ง์ด ๊ฐ–๊ฒŒ ๋˜๋Š” ์†ก์ „ํƒ‘์˜ ๊ฐœ์ˆ˜๋ฅผ ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•˜๊ฒŒ ๋งž์ถ”๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์†ก์ „ํƒ‘์˜ ๊ฐœ์ˆ˜ n, ๊ทธ๋ฆฌ๊ณ  ์ „์„  ์ •๋ณด wires๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ „์„ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Š์–ด์„œ ์†ก์ „ํƒ‘ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋น„์Šทํ•˜๋„๋ก ๋‘ ์ „๋ ฅ๋ง์œผ๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ, ๋‘ ์ „๋ ฅ๋ง์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†ก์ „ํƒ‘ ๊ฐœ์ˆ˜์˜ ์ฐจ์ด(์ ˆ๋Œ€๊ฐ’)๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. 

 

์ œํ•œ์‚ฌํ•ญ

  • n์€ 2 ์ด์ƒ 100 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • wires๋Š” ๊ธธ์ด๊ฐ€ n-1์ธ ์ •์ˆ˜ํ˜• 2์ฐจ์› ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • wires์˜ ๊ฐ ์›์†Œ๋Š” [v1, v2] 2๊ฐœ์˜ ์ž์—ฐ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์ „๋ ฅ๋ง์˜ v1๋ฒˆ ์†ก์ „ํƒ‘๊ณผ v2๋ฒˆ ์†ก์ „ํƒ‘์ด ์ „์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • 1 ≤ v1 < v2 ≤ n ์ž…๋‹ˆ๋‹ค.
  • ์ „๋ ฅ๋ง ๋„คํŠธ์›Œํฌ๊ฐ€ ํ•˜๋‚˜์˜ ํŠธ๋ฆฌ ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

๐Ÿค– ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

์ž…์ถœ๋ ฅ ์˜ˆ

 

๐Ÿง ๋‚œ์ด๋„/์†Œ์š” ์‹œ๊ฐ„

โœ… ๋‚œ์ด๋„: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Level.2

โœ… ์†Œ์š” ์‹œ๊ฐ„: 2์‹œ๊ฐ„ ์ดˆ๊ณผ(ํ’€๋‹ค๊ฐ€ ์ €๋… ๋จน์œผ๋Ÿฌ ๊ฐ€์„œ ๋ฆฌ์…‹ ํ•œ ๋ฒˆ ํ–ˆ์Œ)

โœ… ๊ถŒ์žฅ ์‹œ๊ฐ„: 1์‹œ๊ฐ„ 15๋ถ„

 

๋‹ค๋“ค ์ด๊ฑธ ์–ด๋–ป๊ฒŒ 1์‹œ๊ฐ„ 15๋ถ„ ๋งŒ์— ํ‘ธ๋Š” ๊ฑด๊ฐ€์š” …

 

๐Ÿ’ก ํ’€์ด

1) ๋ฌธ์ œ ์ดํ•ด

๋ฌธ์ œ ์„ค๋ช…๊ณผ ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ๊ฐ€ ์นœ์ ˆํ•ด์„œ ์ ํžŒ ๊ทธ๋Œ€๋กœ ๋ฐ›์•„๋“ค์ด๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ดํ•ด๋Š” ์–ด๋ ต์ง€ ์•Š์•˜๋‹ค.

 

๊ฐ„์„ ์„ ํ•˜๋‚˜์”ฉ ๋‹ค ๋Š์–ด๋ณด๋ฉด์„œ ๊ฐ ์ „๋ ฅ๋ง์„ ๊ตฌ์„ฑํ•˜๋Š” ๋…ธ๋“œ ๊ฐœ์ˆ˜ ์ฐจ์ด์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„๊ฐ€๋ฉด ๋ผ์„œ ์™„์ „ํƒ์ƒ‰ ๋ฌธ์ œ๋กœ ๋ถ„๋ฅ˜๋˜์–ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ๋„ ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ๊ทธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ดํ•ดํ•˜๊ณ  ์ด ๋ฌธ์ œ์— ์ ์šฉํ•˜๋ ค๋ฉด ์˜ค๋Š˜ ์•ˆ์—๋Š” ๋ชป ํ’€ ๊ฒƒ ๊ฐ™์•˜์œผ๋ฏ€๋กœ ์™„์ „ํƒ์ƒ‰ ํ’€์ด ๋ฐฉ๋ฒ•๋งŒ ๊ณ ๋ฏผํ•ด๋ดค๋‹ค.

 

์™„์ „ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด BFS/DFS ์ค‘ ๋ญ˜ ์จ์•ผ ํ•˜๋‚˜ ์‹ถ์—ˆ๋Š”๋ฐ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒŒ ๋ชฉํ‘œ๋‹ˆ๊นŒ BFS๋กœ ์ ‘๊ทผํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

2) ์•„์ด๋””์–ด ํ๋ฆ„ ๋ฐ ์ฝ”๋“œ

โ‘  ๊ฐ„์„  ํ•˜๋‚˜์”ฉ ๋Š๊ธฐ

for i in range(len(wires)):
	splited_wire = wires[:i]+wires[i+1:]	# i๋ฒˆ์งธ ๊ฐ„์„  ์‚ญ์ œ
	network = make_graph(n, splited_wire)	# ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง„ ์ƒํƒœ๋กœ ํ•˜๋‚˜์˜ ์„œ๋ธŒํŠธ๋ฆฌ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ

 

  • splited_wire = wires[:i]+wires[i+1:]
    • wires = [[1, 2], [2, 3], [3, 4]]๋ผ๊ณ  ํ•  ๋•Œ 0๋ฒˆ์งธ ๊ฐ„์„ ์„ ์‚ญ์ œํ•˜๋ฉด splited_wire = [[2, 3], [3, 4]] ๊ฐ€ ๋œ๋‹ค. 2์™€ 3์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ ์„ ๋Š์—ˆ๋‹ค๋Š” ์˜๋ฏธ๋กœ ์ด ๊ฒฝ์šฐ์—๋Š” 1-2 / 3-4 ๋‘ ๊ฐœ๋กœ ํŠธ๋ฆฌ๊ฐ€ ๋‚˜๋‰˜๊ฒŒ ๋œ๋‹ค.
  • network = make_graph(n, splited_wire)
    • ์ดˆ๊ธฐ์— ์ฃผ์–ด์ง„ wires๋Š” ๋ชจ๋‘ ์—ฐ๊ฒฐ๋œ ์ƒํƒœ์ด์ง€๋งŒ ๊ฐ„์„ ์„ ํ•˜๋‚˜์”ฉ ๋Š์–ด์ฃผ๋ฉด์„œ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ํ™•์ธํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„์„ ์„ ๋Š์„ ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋„คํŠธ์›Œํฌ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค.
    • ์ฒ˜์Œ์—๋Š” ์—ฐ๊ฒฐ์ด ๋Š๊ธด ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ดˆ๊ธฐ ์ƒํƒœ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ–ˆ๋”๋‹ˆ ๊ณ„์† ์˜ˆ์‹œ์™€ ๋‹ค๋ฅธ ๋‹ต์ด ์ถœ๋ ฅ๋๊ณ  ๊ฐ„์„ ์„ ๋Š์„ ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋„คํŠธ์›Œํฌ๋กœ ๊ฐฑ์‹ ํ•˜์ง€ ์•Š์•„์„œ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋๋‹ค.

 

โ‘ก BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์„œ๋ธŒํŠธ๋ฆฌ ๋…ธ๋“œ ๊ฐœ์ˆ˜ ํ™•์ธ

def BFS(node, visited, graph):
        queue = deque([node])
        
        node_cnt = 1
        
        while queue:
            v = queue.popleft() 
            
            for i in graph[v]:
                if not visited[i]:
                    queue.append(i)
                    
                    visited[i] = True
                    
                    node_cnt += 1
        
        return node_cnt

 

  • splited_wire๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ๋…ธ๋“œ๋“ค์„ ์ด์šฉํ•ด ํ•œ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•œ๋‹ค.
  • ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋งŒ ์ž˜ ์„ ์–ธํ•ด์ฃผ๋ฉด ๋˜๊ณ  ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ BFS ๊ตฌํ˜„์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

โ‘ข ์†ก์ „ํƒ‘ ๊ฐœ์ˆ˜ ์ฐจ์ด ๊ฐฑ์‹ 

answer = min(answer, abs(subtree_node-(n-subtree_node)))

 

  • abs(subtree_node-(n-subtree_node)))
    • ์ฝ”๋“œ๋ฅผ ๋”ฐ๋กœ ๋–ผ์„œ ์ ๋‹ค ๋ณด๋‹ˆ ์ด ๊ณผ์ •์—์„œ๋Š” ์ƒ๋žต๋์ง€๋งŒ subtree_node๋Š” BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ตฌํ•œ ํ•œ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜์ด๋‹ค.
    • ์ฆ‰, 1-2 / 3-4 ๋กœ ํŠธ๋ฆฌ๊ฐ€ ๋‚˜๋ˆ ์กŒ๋‹ค๊ณ  ํ•  ๋•Œ subtree_node๋Š” 2๊ฐ€ ๋œ๋‹ค.
    • n-subtree_node๋Š” ์ „์ฒด ๋…ธ๋“œ ์ˆ˜์—์„œ ๋ฏธ๋ฆฌ ๊ตฌํ•ด๋‘” subtree_node๋ฅผ ๋นผ์„œ ๋‚จ์€ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.

 

3) ์ „์ฒด ์ฝ”๋“œ

from collections import deque

def make_graph(n, current_wires):
    graph = {k: [] for k in range(1, n+1)}
    
    for pair in current_wires:
        u, v = pair
        
        graph[u].append(v)
        graph[v].append(u)
    
    return graph

def solution(n, wires):
    answer = 999
    
    # ํ•˜์œ„ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜ ์„ธ๋Š” ์—ญํ• 
    def BFS(node, visited, graph):
        queue = deque([node])
        
        node_cnt = 1
        
        while queue:
            v = queue.popleft() 
            
            for i in graph[v]:
                if not visited[i]:
                    queue.append(i)
                    
                    visited[i] = True
                    
                    node_cnt += 1
        
        return node_cnt
    
    # ์„  ๋Š๊ธฐ
    for i in range(len(wires)):
        splited_wire = wires[:i]+wires[i+1:]
        
        network = make_graph(n, splited_wire)
        
        for src, dest in splited_wire:
            visited = [False]*(n+1)

            visited[src] = True
            
            subtree_node = BFS(src, visited, network)
        
        answer = min(answer, abs(subtree_node-(n-subtree_node)))
        
    return answer

 

6) ์–ด๋ ค์› ๋˜ ์ /๋ฐฐ์šด ์ 

๐Ÿšจ ์–ด๋ ค์› ๋˜ ์ 

  • BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋‹ต์„ ๊ตฌํ•˜๋Š” ๋ฐ์— ์ง์ ‘์ ์œผ๋กœ ์“ฐ์ด์ง€ ์•Š์•˜๋‹ค๋Š” ์ 
    • ์†Œ์ˆ˜ ์ฐพ๊ธฐ ๋ฌธ์ œ์—์„œ DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ˆซ์ž ์กฐํ•ฉ์„ ๋งŒ๋“œ๋Š” ๋ฐ์— ์‚ฌ์šฉ๋๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์ด ๋ฌธ์ œ์—์„œ๋„ BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์—ญํ• ์€ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.
    • ์ด ๋ถ€๋ถ„์„ ์ •์˜ํ•˜์ง€ ๋ชปํ•˜๋ฉด ์•„์˜ˆ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์—†์—ˆ์„ ๊ฑฐ๋ผ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ธ ๊ฑด ๋งž๋‹ค. ํ•˜์ง€๋งŒ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐ์„œ ๋์ด ์•„๋‹ˆ๋ผ ๊ทธ๊ฑธ ํ™œ์šฉํ•ด์„œ ์„œ๋ธŒํŠธ๋ฆฌ๊ฐ„ ๋…ธ๋“œ ๊ฐœ์ˆ˜ ์ฐจ์ด๋ฅผ ๊ณ„์† ๊ฐฑ์‹ ํ•ด๋‚˜๊ฐ€์•ผํ•˜๋Š” ๊ฑธ ๋– ์˜ฌ๋ฆฌ๋Š” ๊ฒŒ ์–ด๋ ค์› ๋‹ค.

โ— ๋ฐฐ์šด ์ 

  • ์ฒ˜์Œ์—๋Š” ์–ด๋ ค์›Œ๋ณด์ด๋Š” ๋ฌธ์ œ๋„ ๋‚ด๊ฐ€ ๊ฐ€์ง„ ์ง€์‹์„ ์ž˜ ์กฐํ•ฉํ•˜๋ฉด ํ’€ ์ˆ˜ ์žˆ๋‹ค.