-
Notifications
You must be signed in to change notification settings - Fork 1
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
10-alstjr7437 #33
Conversation
๋ณดํต 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') |
ํน์ ์ด ์ฝ๋๋ stack์ผ๋ก BFS๋ฅผ ์ด์ฉํด ํ ๋ถ๋ถ ์๋๊ฐ์!!? |
DFS์ ๋๋ค. ๊ฐ์ฌํฉ๋๋ค. |
There was a problem hiding this 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:])
There was a problem hiding this 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] = '์ด๋ฌด์ด' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ ์ด ์๋ค๋..์ด๊ฒ ์ธํฐํ๋ฆฌํฐ ์ธ์ด?
if visited[i] == 0: | ||
queue.append(i) | ||
visited[i] = a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if visited[i] == 0: | |
queue.append(i) | |
visited[i] = a | |
if visited[i] != 0: | |
continue | |
queue.append(i) | |
visited[i] = a |
์ด๋ ๊ฒ ํ๋ฉด depth ๋ฅผ ์ค์ผ ์ ์์ต๋๋ค.
์กฐ๊ฑด๋ฌธ์ด ๋ง์์ง๋ฉด ์ฅํ์ ํผ๋์ ์ผ๊ธฐํฉ์ฃ
There was a problem hiding this 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])
๐ ๋ฌธ์ ๋งํฌ
ํธ๋ฆฌ์ ๋ถ๋ชจ ์ฐพ๊ธฐ
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
โจ ์๋ ์ฝ๋
ํธ๋ฆฌ๋ถ๋ถ์ ๋ง์ด ๊น๋จน์ด์ ์๋กญ๊ฒ ์์ํ๋ ๋๋์ผ๋ก ํธ๋ฆฌ์ ๊ธฐ๋ณธ ๊ฐ์ ๊ฐ ๋ ธ๋์ ๋ถ๋ชจ๋ฅผ return ํด์ฃผ๋ ๋ฌธ์ ์ ๋๋ค!!
์ ๋ ์ผ๋จ ํธ๋ฆฌ๋ฅผ ์ค๋๋ง์ ๋ง๋ค์ด์ ๋์ ๋๋ฆฌ๋ฅผ ์ฌ์ฉํ์ต๋๋ค!!
๋ฐ๋ก ๋ฌธ์ ๋ฅผ ์ฝ์ด๋ณด๋ ๋ ธ๋์ ๊ฐ์๊ฐ ์ ํด์ ธ์ n์ด 6์ด๋ฉด 1~6๊น์ง์ ๋ ธ๋๋ง ์์ผ๋ฉด ๋๊ฒ ๋๋ผ๊ตฌ์
๊ทธ๋ฆฌ๊ณ ๋ฃจํธ ๋ ธ๋๋ 1๋ก ๊ณ ์ ์ด ๋์ด์๋๋ผ๊ตฌ์
๊ทธ๋์ 1 : [] 2: [] 3: [] ์ด๋ฐ์์ผ๋ก ๋์ ๋๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์์ฑ์์ผ์คฌ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ BFS์ DFS๋ฅผ ์ด์ฉํ ํ์์ผ๋ก ๊ฐ ๋ ธ๋์ ๋ถ๋ชจ๋ฅผ ๋ค๊ณ ์ ๋ฐฐ์ด์ ์ ์ฅํ์ต๋๋ค!
์์ ๋ ์๋์ ๊ฐ์ต๋๋ค!
BFS(๋๋น ์ฐ์ ํ์)
๋๋น ์ฐ์ ํ์์ ๋ฐฉ๋ฒ์ผ๋ก ์ผ๋จ ํ(queue) ๋ฐฐ์ด์ ๋ง๋ค๊ณ
์์ ๊ฐ์ด ์งํํ๊ฒ ๋๋ฉด
1 - [6,4] - [4,3] - [3,2] - [3,2,7] - [2,7,5] - [7,5] - [5] - ์ข ๋ฃ
์์ผ๋ก ์งํ๋๊ฒ ๋ฉ๋๋ค!
์๋ ํ๋ visited๊ฐ ๋ฐ๋๋ ์์์ ๋๋ค!!
DFS(๊น์ด ์ฐ์ ํ์)
visited์ 0์ด ์๋๋ฉด ๊ณ์ํด์ dfs ์ฌ๊ท ํจ์๋ฅผ ๋๋๋ก ํ์์ต๋๋ค!
์์ ๊ฐ์ด ์งํํ๊ฒ ๋๋ฉด 1 -> 6 -> 3 -> 5 -> 2 -> 7 -> ์ข ๋ฃ๋ก ์งํํ๊ฒ ๋ฉ๋๋ค!
์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ค๋๋ง์ ๋ค์ ํธ๋ฆฌ์ ๋ํด ์๊ฐํ๊ณ ํธ๋ฆฌ๋ฅผ ๊ตฌํํ๊ณ ๋ง๋ค์ด๋ดค๊ณ
BFS, DFS๋ฅผ ํตํ ํ์ํ์ฌ์ ๋ถ๋ชจ์ ์๋ฅผ ๋ฃ์ด์ฃผ๋ ๋ถ๋ถ๊น์ง ๊ฐ๋จํ๊ฒ ํ์ฌ์ ๋ค์ ํธ๋ฆฌ์ ๋ํด ๊ณต๋ถํ๊ฒ ๋์ต๋๋ค!!!