Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-alstjr7437 #33

Merged
merged 2 commits into from
Feb 24, 2024
Merged

10-alstjr7437 #33

merged 2 commits into from
Feb 24, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Feb 20, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํŠธ๋ฆฌ์˜ ๋ถ€๋ชจ ์ฐพ๊ธฐ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

ํŠธ๋ฆฌ๋ถ€๋ถ„์„ ๋งŽ์ด ๊นŒ๋จน์–ด์„œ ์ƒˆ๋กญ๊ฒŒ ์‹œ์ž‘ํ•˜๋Š” ๋Š๋‚Œ์œผ๋กœ ํŠธ๋ฆฌ์˜ ๊ธฐ๋ณธ ๊ฐ™์€ ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋ฅผ return ํ•ด์ฃผ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค!!




์ €๋Š” ์ผ๋‹จ ํŠธ๋ฆฌ๋ฅผ ์˜ค๋ž˜๋งŒ์— ๋งŒ๋“ค์–ด์„œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค!!
๋”ฐ๋กœ ๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด๋‹ˆ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ์„œ n์ด 6์ด๋ฉด 1~6๊นŒ์ง€์˜ ๋…ธ๋“œ๋งŒ ์žˆ์œผ๋ฉด ๋˜๊ฒ ๋”๋ผ๊ตฌ์š”
๊ทธ๋ฆฌ๊ณ  ๋ฃจํŠธ ๋…ธ๋“œ๋„ 1๋กœ ๊ณ ์ •์ด ๋˜์–ด์žˆ๋”๋ผ๊ตฌ์š”
๊ทธ๋ž˜์„œ 1 : [] 2: [] 3: [] ์ด๋Ÿฐ์‹์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์ƒ์„ฑ์‹œ์ผœ์คฌ์Šต๋‹ˆ๋‹ค.



๊ทธ๋ฆฌ๊ณ  BFS์™€ DFS๋ฅผ ์ด์šฉํ•œ ํƒ์ƒ‰์œผ๋กœ ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋ฅผ ๋“ค๊ณ ์™€ ๋ฐฐ์—ด์— ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค!



์˜ˆ์ œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค!




BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰)

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์ผ๋‹จ ํ(queue) ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ 

  1. queue ๋ฐฐ์—ด์— ๋ฃจํŠธ๋ฅผ ๋„ฃ๋Š”๋‹ค.
  2. queue๊ฐ€ ์—†์–ด์งˆ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    1. queue์— ์ œ์ผ ์™ผ์ชฝ ๋ถ€๋ถ„์„ ๋นผ์ค€๋‹ค - 1
    2. ์ œ์ผ ์™ผ์ชฝ๊ณผ ์—ฐ๊ฒฐ๋œ ๋ถ€๋ถ„์˜ ์ˆ˜๋ฅผ ๋„ฃ๋Š”๋‹ค. - 6, 4
    3. visited(๋ถ€๋ชจ ๋„ฃ์–ด๋‘๋Š” ๋ฐฐ์—ด)์— ๋บ€ ์™ผ์ชฝ ๋ถ€๋ถ„์„ ๋„ฃ์–ด์ค€๋‹ค. - 1์„ ์ถ”๊ฐ€
  3. visited ๋ฐฐ์—ด ์ถœ๋ ฅ

์œ„์™€ ๊ฐ™์ด ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด
1 - [6,4] - [4,3] - [3,2] - [3,2,7] - [2,7,5] - [7,5] - [5] - ์ข…๋ฃŒ
์‹์œผ๋กœ ์ง„ํ–‰๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!
image

์•„๋ž˜ ํ‘œ๋Š” visited๊ฐ€ ๋ฐ”๋€Œ๋Š” ์ˆœ์„œ์ž…๋‹ˆ๋‹ค!!

0 1 2 3 4 5 6 7
0 'Root' 0 0 1 0 1 0
0 'Root' 0 6 1 0 1 0
0 'Root' 0 0 0 0 1 0
0 'Root' 4 6 1 0 1 0
0 'Root' 4 6 1 0 1 4
0 'Root' 4 6 1 3 1 4




DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)

visited์— 0์ด ์•„๋‹ˆ๋ฉด ๊ณ„์†ํ•ด์„œ dfs ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ๋Œ๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค!

  1. visited์— 1์— ๋‹ค๋ฅธ๊ฑธ ๋„ฃ๊ณ  1๋ถ€ํ„ฐ ๋Œ๋ฆฐ๋‹ค.(์•ˆ๋„ฃ์œผ๋‹ˆ 1์˜ ๋ฃจํŠธ๋„ ์ƒ์„ฑํ•ด๋ฒ„๋ฆฌ๋”๋ผ๊ตฌ์š”..)
  2. ์žฌ๊ท€ํ•จ์ˆ˜ ์‹คํ–‰
    1. ์žฌ๊ท€ํ•จ์ˆ˜์—์„œ ์˜จ a์™€ ์—ฐ๊ฒฐ๋œ ์ˆ˜๊ฐ€ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋Œ์•„๊ฐ„๋‹ค. - 6
    2. ๋ฐ˜๋ณต๋ฌธ ์ค‘ ์žฌ๊ท€ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋„ ์‚ดํŽด๋ณธ๋‹ค - 3
    3. ๋‹ค ๋“ค์–ด๊ฐ”๋‹ค ๋‚˜์™”์œผ๋ฉด ๋‹ค์‹œ 1๋กœ ๋Œ์•„์™€์„œ ๋‹ค๋ฅธ ๋ถ€๋ถ„์„ ํ˜ธ์ถœํ•œ๋‹ค. - 4
  3. visited ๋ฐฐ์—ด ์ถœ๋ ฅ

์œ„์™€ ๊ฐ™์ด ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด 1 -> 6 -> 3 -> 5 -> 2 -> 7 -> ์ข…๋ฃŒ๋กœ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!
image

0 1 2 3 4 5 6 7
0 'Root' 0 0 0 0 1 0
0 'Root' 0 6 0 0 1 0
0 'Root' 0 6 0 3 1 0
0 'Root' 0 6 1 3 1 0
0 'Root' 4 6 1 3 1 0
0 'Root' 4 6 1 3 1 4




์ฝ”๋“œ

import sys
sys.setrecursionlimit(10**8) #์žฌ๊ท€ ๋ฆฌ๋ฏธํŠธ
from collections import deque

n = int(input())

# ํŠธ๋ฆฌ ๋งŒ๋“ค๊ธฐ
tree = {i:[] for i in range(1,n+1)}
for i in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)
# print(tree)

# ๋ถ€๋ชจ ์ €์žฅํ•˜๊ธฐ
visited = [0] * (n+1)

# DFS
def dfs(a):
    for i in tree[a]:
        if visited[i] == 0:
            visited[i] = a
            # print(visited, i, a)
            dfs(i)

# BFS
def bfs():
    while queue:
        a = queue.popleft()
        for i in tree[a]:
            if visited[i] == 0:
                queue.append(i)
                visited[i] = a
                # print(visited, queue)


visited[1] = 'Root'

# dfs(1)

queue = deque()
queue.append(1)
bfs()

for i in range(2, n+1):
    print(visited[i])




๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์˜ค๋ž˜๋งŒ์— ๋‹ค์‹œ ํŠธ๋ฆฌ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•˜๊ณ  ํŠธ๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ๋งŒ๋“ค์–ด๋ดค๊ณ 
BFS, DFS๋ฅผ ํ†ตํ•œ ํƒ์ƒ‰ํ•˜์—ฌ์„œ ๋ถ€๋ชจ์˜ ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜์—ฌ์„œ ๋‹ค์‹œ ํŠธ๋ฆฌ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๊ฒŒ ๋์Šต๋‹ˆ๋‹ค!!!

@9kyo-hwang
Copy link

image

๋ญ”๊ฐ€ ํ‚น๋ฐ›๋„ค์š” ์ด๊ฑฐ

@alstjr7437
Copy link
Member Author

๋ญ”๊ฐ€ ํ‚น๋ฐ›๋„ค์š” ์ด๊ฑฐ

์ด๊ฑธ ๋ณด๋‹ค๋‹ˆ.. ์•„๋ž˜์™€ ๊ฐ™์ด ์–ด๋จธ๋‹ˆ๊ฐ€ ์ž๊พธ ์•„๋“ค์„ ์–ด๋จธ๋‹ˆ๋กœ ์ธ์‹ํ•ด์„œ ๋„ฃ์–ด ๋ฒ„๋ฆฌ๋”๋ผ๊ตฌ์š”...
image

๊ทธ๋ž˜์„œ ์งœ์ฆ๋‚˜์„œ ๊ฐ’์„ ๋ฐ”๊ฟ”์ฃผ๋‹ค๊ฐ€..

@9kyo-hwang
Copy link

๋ณดํ†ต DFS๋Š” recursion์„ ํ†ตํ•ด ๋งŽ์ด ๊ตฌํ˜„ํ•˜์ง€๋งŒ, ์ด ๋ฌธ์ œ๋Š” stack์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์‹  ๋ถ„๋“ค์ด ๋งŽ๋„ค์š”? ์š”๊ฒŒ ์†๋„๋„ ๋น ๋ฅด๊ตฌ์š”.

input = open(0).readline

N = int(input())
tree = [[] for _ in range(N + 1)]

for _ in range(N - 1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)
    
parents = [0] * (N + 1)

stack = [1]
while stack:
    src = stack.pop()
    for dst in tree[src]:
        if not parents[dst]:
            parents[dst] = src
            stack.append(dst)

print(*parents[2:], sep='\n')

@alstjr7437
Copy link
Member Author

๋ณดํ†ต DFS๋Š” recursion์„ ํ†ตํ•ด ๋งŽ์ด ๊ตฌํ˜„ํ•˜์ง€๋งŒ, ์ด ๋ฌธ์ œ๋Š” stack์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์‹  ๋ถ„๋“ค์ด ๋งŽ๋„ค์š”? ์š”๊ฒŒ ์†๋„๋„ ๋น ๋ฅด๊ตฌ์š”.

ํ˜น์‹œ ์ด ์ฝ”๋“œ๋Š” stack์œผ๋กœ BFS๋ฅผ ์ด์šฉํ•ด ํ•œ ๋ถ€๋ถ„ ์•„๋‹Œ๊ฐ€์š”!!?

@9kyo-hwang
Copy link

๋ณดํ†ต DFS๋Š” recursion์„ ํ†ตํ•ด ๋งŽ์ด ๊ตฌํ˜„ํ•˜์ง€๋งŒ, ์ด ๋ฌธ์ œ๋Š” stack์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์‹  ๋ถ„๋“ค์ด ๋งŽ๋„ค์š”? ์š”๊ฒŒ ์†๋„๋„ ๋น ๋ฅด๊ตฌ์š”.

ํ˜น์‹œ ์ด ์ฝ”๋“œ๋Š” stack์œผ๋กœ BFS๋ฅผ ์ด์šฉํ•ด ํ•œ ๋ถ€๋ถ„ ์•„๋‹Œ๊ฐ€์š”!!?

DFS์ž…๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ๋˜‘๊ฐ™์ด ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋˜๋Š” ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋˜.. ์นดํ†ก์—์„œ ๋ฐ”๊ฟ”์ ์€ ์ฝ”๋“œ ๊ทธ๋Œ€๋กœ ์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹ ๊ฐ ์žก๊ธฐ์—” ๋”ฑ ์ข‹์€ ๋ฌธ์ œ๊ฐ™์•„์š”

from heapq import heappop, heappush

n = int(input())

tree = {i: [] for i in range(1, n + 1)}

for i in range(n - 1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)

visited = [0] * (n + 1)

q = []
heappush(q,1)

while q:
  node = heappop(q)
  for next_node in tree[node]:
    if not visited[next_node]:
      visited[next_node] = node
      heappush(q, next_node)

print(*visited[2:])

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” bfs ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    val graph = List<MutableList<Int>>(n + 1) { mutableListOf() }
    val parents = Array(n + 1) { 0 }

    for (i in 0 until n - 1) {
        val (nodeA, nodeB) = br.readLine().split(" ").map { it.toInt() }
        graph[nodeA].add(nodeB)
        graph[nodeB].add(nodeA)
    }

    val q = ArrayDeque<Int>()
    q.add(1)

    while (q.isNotEmpty()) {
        val parent = q.removeFirst()
        for (node in graph[parent]) {
            if (parents[node] != 0) {
                continue
            }
            parents[node] = parent
            q.add(node)
        }
    }

    for (i in 2..n) {
        println(parents[i])
    }
}

# print(visited, queue)


visited[1] = '์–ด๋ฌด์ด'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํƒ€์ž…์ด ์—†๋‹ค๋‹ˆ..์ด๊ฒŒ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด?

Comment on lines +31 to +33
if visited[i] == 0:
queue.append(i)
visited[i] = a
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if visited[i] == 0:
queue.append(i)
visited[i] = a
if visited[i] != 0:
continue
queue.append(i)
visited[i] = a

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด depth ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์กฐ๊ฑด๋ฌธ์ด ๋งŽ์•„์ง€๋ฉด ์žฅํ’์€ ํ˜ผ๋ž€์„ ์•ผ๊ธฐํ•ฉ์ฃ 

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์‰ฝ๊ฒŒ ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ.. ํ˜ผ์ž ํ’€์–ด๋ณผ๋ ค๋‹ค๊ฐ€ ์™œ ์•ˆ๋˜๋Š” ๊ฑฐ์ง€ ํ•˜๋ฉด์„œ ๋ฏผ์„๋‹˜ ์ฝ”๋“œ ์ด๋ž˜์ €๋ž˜ ์‚ดํŽด๋ณด๋ฉด์„œ ์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค ใ…œใ…œ ๊ทธ๋ฆฌ๊ณ  dfs ๋กœ ํ‘ธ๋‹ˆ๊นŒ ๊ฒ๋‚˜ ๋Š๋ฆฌ๋„ค์š” ..!

import sys
sys.setrecursionlimit(10**9)
N = int(input())
tree = {i:[] for i in range(1, N+1)}


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

answer = [0 for _ in range(N+1)]

def dfs(current_node) :
    for node in tree[current_node] :
        if answer[node] == 0 :
            answer[node] = current_node
            dfs(node)

answer[1] = 1
dfs(1)

for index in range(2,N+1) :
    print(answer[index]) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants