๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€ - 2024.01.15 ์˜ค๋Š˜์˜ ๋ฐฑ์ค€ ๋ฌธ์ œํ’€์ด

์œ ๋ น๊ฐœยท2024๋…„ 1์›” 15์ผ
0

PS-์•Œ๊ณ ๋ฆฌ์ฆ˜2

๋ชฉ๋ก ๋ณด๊ธฐ
13/34
post-thumbnail

1240๋ฒˆ - ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ

ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฟ๊ฐ’์ด ์ฃผ์–ด์งˆ ๋•Œ ๋…ธ๋“œ ๋‘ ๊ฐœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ํ•ด๋‹น ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.


์ด๋Ÿฐ ์”ฉ์œผ๋กœ ๋ง์ž…๋‹ˆ๋‹ค.


์ฒ˜์Œ ์ ‘๊ทผํ•œ ๋ฐฉ์‹

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  2์ฐจ์› ๋ฐฐ์—ด์˜ DFS๋กœ ํ’€๋˜ ๊ทธ๋ž˜ํ”„์˜ ๊ฐ’์— ๊ฑฐ๋ฆฌ๊ฐ’์„ ๋„ฃ์–ด ํ’€์ดํ•˜๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜! ํ•˜์—ฌ

for _ in range(N-1):
    a,b,distance = map(int,input().split())
    graph[a][b] = graph[b][a] = distance

์ด์ฐจ์› ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด a์™€ b๊ฐ€ ์ด์–ด์ ธ ์žˆ๋‹ค๋Š” ๊ฑธ ํ‘œํ˜„ํ•˜๊ณ  ํ•ด๋‹น ์ธ๋ฑ์Šค value์— ๊ฑฐ๋ฆฌ๊ฐ’์„ ๋„ฃ๊ณ 

    for i in range(len(graph[0])):
        if not visited[i] and graph[N][i] != 0:
            length += graph[N][i]
            length_record.append(graph[N][i])
            dfs(i,end)
    if length_record:
        length -= length_record.pop()

DFS ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜์—ฌ depth, ์ฆ‰ ๊นŠ์ด๊ฐ€ ์ฆ๊ฐ€ํ•  ๋–„๋งˆ๋‹ค graph[N][i]์˜ value(๊ฑฐ๋ฆฟ๊ฐ’)๋ฅผ ํ•ฉ์—ฐ์‚ฐํ•˜์—ฌ

    if N == end:
        ans.append(length)

์ตœ์ข…์ ์œผ๋กœ ๋๊ฐ’์„ ๋งŒ๋‚˜๋ฉด ans์— ์ถ”๊ฐ€๋˜๋„๋ก ์ฝ”๋”ฉํ•˜๋‹ˆ...!

์ œ๊ฐ€ ์žŠ๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค... DFS์—์„œ ์ด์ฐจ์› ๋ฐฐ์—ด์„ ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์ปค์ ธ ๊ฒฐ๊ณผ๋„ ์ž์œ ๋กญ๊ฒŒ ๋‚˜์™€๋ฒ„๋ฆฐ๋‹ค๋Š” ๊ฒƒ์„...


ํ•ด๊ฒฐ๋ฐฉ์•ˆ

์œ„๋Š” ๋ฌธ์ œ์˜ ์ œํ•œ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค.
N์˜ ๋ฒ”์œ„๋ฅผ 1000, M์˜ ๋ฒ”์œ„๋ฅผ 1000์œผ๋กœ ์ง€์ •ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ ์ƒ๊ฐํ•œ ๊ฒƒ์€ DFS ๊ทธ๋ž˜ํ”„๋Š” 1์ฐจ์›์  ๋ฐฐ์—ด๋กœ ์šฉ๋Ÿ‰์„ ์ค„์ด๋˜ 1000 x 1000 ํ•ด๋ด์•ผ ๋ฐฑ๋งŒ๋ฐ–์— ์•ˆ๋˜๋‹ˆ๊นŒ ๊ฑฐ๋ฆฟ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ทธ๋ž˜ํ”„๋Š” ๊ทธ๋Œ€๋กœ 2์ฐจ์› ํ‘œ๊ธฐ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ฒ ๋‹ค! ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.


ํ’€์ด

N,M = map(int,input().split())
visited = [0]*(N+1)
graph = [[]for _ in range(N+1)]
distance_graph = [[0]*(N+1) for _ in range(N+1)]
length = 0
ans = []
distance_tmp = []

DFS๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋“ค๊ณผ ๋ฐฉ๋ฌธ์—ฌ๋ถ€, ๊ทธ๋ฆฌ๊ณ  ์œ„์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ๊ฑฐ๋ฆฟ๊ฐ’๋งŒ์„ ๋‚˜ํƒ€๋‚ด๋Š” distance_graph์™€ ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜๋“ฑ์„ ์„ ์–ธํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

for _ in range(N-1):
    a,b,distance = map(int,input().split())
    distance_graph[a][b] = distance_graph[b][a] = distance
    graph[a].append(b)
    graph[b].append(a)

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

์›๋ž˜์˜€๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด ๋‚˜์˜ฌ ๊ทธ๋ž˜ํ”„๋ฅผ

1์ฐจ์›์  ๋ฆฌ์ŠคํŠธ๋กœ ํ•จ์ถ•์‹œ์ผœ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์„ ํ”ผํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ans_tmp = []
for _ in range(M):
    a,b = map(int,input().split())
    ans_tmp.append([a,b])

๋‹ค์Œ์œผ๋กœ ๊ฑฐ๋ฆฟ๊ฐ’์„ ๊ตฌํ•ด์ค„ ๋…ธ๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.

for values in ans_tmp:
    dfs(values[0],values[1])
    distance_tmp = []
    visited = [0]*(N+1)
    length = 0

์ด์ œ DFS ํ•จ์ˆ˜๋ฅผ ์ค€๋น„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. values[0]์€ ํƒ์ƒ‰์˜ ์‹œ์ž‘์ , values[1]์€ ๋ณด๊ด€๋งŒ ํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ๋งŒ์•ฝ ๋ณ€์ˆ˜๊ฐ’์ด values[1]๊ณผ ๊ฐ™์•„์ง„๋‹ค๋ฉด ans์— ์ตœ์ข…๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ณ  return ํ•˜๋Š” ์”ฉ์œผ๋กœ ์ž‘์„ฑํ•  ์žฌ๊ท€ ์ข…๋ฃŒ์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ข…๋ฃŒ์— ํ•„์š”ํ•œ ํ•จ์ˆ˜๋“ค ์ž‘์„ฑํ•˜๊ณ  DFS ๋ถ€๋ถ„ ๋งˆ์ € ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


DFS

def dfs(N,end):
    global length
    visited[N] = 1

๋“ค์–ด์˜ค๋ฉด ๋ฐฉ๋ฌธํ‘œ์‹œ ํ•ด์ฃผ๊ณ 

    if N == end:
        ans.append(length)
        return

์œ„์— ๋ง์”€๋“œ๋ ธ๋“ฏ์ด ํƒ์ƒ‰์ค‘์ธ ๊ตฌ๊ฐ„์ด end์™€ ๊ฐ™๋‹ค๋ฉด ans์— ๋‹ต ์ถ”๊ฐ€ํ•˜๊ณ  return์œผ๋กœ ํ•จ์ˆ˜์ข…๋ฃŒ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    for values in graph[N]:
        if not visited[values]:

ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ๊ตฌ๊ฐ„๊ณผ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์—์„œ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด

            distance_tmp.append(distance_graph[N][values])
            length += distance_tmp[-1]

ํ˜„์žฌ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ distance_tmp์— ์ถ”๊ฐ€ํ•˜๊ณ  length(ํ˜„์žฌ ๊ธธ์ด)์—๋Š” distance_tmp[-1]์„ ํ•ฉ์—ฐ์‚ฐํ•ด์ค๋‹ˆ๋‹ค.

            dfs(values,end)

๊ทธ๋ฆฌ๊ณ  ์žฌ๊ท€ ๋„ฃ๊ณ 

    if distance_tmp:
        length -= distance_tmp.pop()

๊นŠ์ด๊ฐ€ ์ตœ๋Œ€๋กœ ๋„๋‹ฌํ•ด์„œ ๋” ์ด์ƒ ํƒ์ƒ‰ํ•  ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ํ˜„์žฌ ๋…ธ๋“œ์™€ ์ด์ „ ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ (distance_tmp.pop)๋งŒํผ ๋นผ์ฃผ๋ฉด์„œ ํ•˜์œ„๋…ธ๋“œ ํƒ์ƒ‰์„ ๋‚˜์™€์ค์‹œ๋‹ค.

for values in ans:
    print(values)

๋งˆ์ง€๋ง‰์œผ๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.




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


profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€