-
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
22-alstjr7437 #78
22-alstjr7437 #78
Conversation
์๋ queue ์ง์ ์ํ๋ ๊ฑฐ ์ข ๋์ฐํ๋ค;์ฐพ์๋ณด๋ ์ ๋ฐ ์์ผ๋ก head๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ฅผ ๋๊ณ ๊ฑ๋ฅผ ์ฌ๋ฆฌ๋ ์์ผ๋ก ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์๊ณ , stack 2๊ฐ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์๋ค์. ํน์ดํ ๊ฒ ๋ณดํต reverse๋ ๋ฉ์๋๋ |
์ค.. ๊ตํฉ๋ ๋ง์์ ๋ฃ๊ณ reversed์ reverse ๋ฉ์๋๋ฅผ ์กฐ๊ธ ์ฐพ์๋ณด๋๊น reverse๋ ๊ทธ๋ฅ ๋ฐฐ์ด์ ๋ฐ๋๋ก ๋ค์ง๋ ๋ฉ์๋๋ผ O(n)์ด ๋์ค๊ณ ๊ฐ์ฌํฉ๋๋ค ์๋ก์ด ์ง์์ ๋ฃ์ด์ฃผ์ ์! var originalArray = [1, 2, 3]
var reversedArray = originalArray.reversed()
print("0th index of OriginalArray \(originalArray[0])") // 1
print("0th index of ReversedBaseArray \(reversedArray._base[0])") // 1
print("0th index of OriginalArray \(originalArray.first)") // 1
print("0th index of ReversedBaseArray \(reversedArray.first)") // 3
originalArray[0] = 4 // Modification makes a separate copy of each instance
print("0th index of OriginalArray \(originalArray[0])") // 4
print("0th index of ReversedBaseArray \(reversedArray._base[0])") // 1
originalArray.reverse()
print(originalArray) // [3,2,4]
print(reversedArray) // ReversedCollection<Array<Int>>(_base: [1, 2, 3]) + index ์ ๊ทผ ๋ถ๊ฐ..์๋ ์์ ์ ๊ฐ์ด ReversedCollection์ Int ์ธ๋ฑ์ฑ์ด ๋ถ๊ฐ๋ฅํ๋๊ตฐ์.. let arr = [1,2,3,4]
let rev = arr.reversed() // ReversedCollection<Array<Int>> ์ด๊ฒ์ Int์ธ๋ฑ์ฑ์ด ๋ถ๊ฐ๋ฅํ๋ค.
let i = rev[1] // compile error! ๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด ๋๊ฐ ์ฐ๋๊ฒ ์ด๊ฒ ๋ง์๊น์..? func bfs3(start: Int){
visited[start] = true
var stack1: [Int] = [start]
while !stack1.isEmpty{
var temp: [Int] = []
for i in stack1.reversed() { temp.append(i) }
stack1 = temp
let current = stack1.removeLast()
for i in graph[current]{
if visited[i] == false {
stack1.append(i)
visited[i] = true
}
}
}
} |
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.
Swift๋ก Queue ๊ตฌํํ๊ธฐ ์ฌ๊ธฐ ๋ธ๋ก๊ทธ ๋ณด๊ณ ํด๋ณธ ์ฝ๋์ธ๋ฐ...
struct Queue<T> {
private var enqueue: [T] = []
private var dequeue: [T] = []
var isEmpty: Bool {
return enqueue.isEmpty && dequeue.isEmpty
}
mutating func push(_ input: T) {
enqueue.append(input)
}
@discardableResult
mutating func pop() -> T? {
if dequeue.isEmpty {
dequeue = enqueue.reversed()
enqueue = []
}
return dequeue.popLast()
}
}
์ด๋ฐ ์์ผ๋ก ๊ตฌํํ๋๋ฐ, ๋ณด๋ฉด ์ธ๋ฑ์ค๋ก ์ ๊ทผํ๋ ๊ฑด ์๋ฌด๊ฒ๋ ์๊ณ ์ ๋ถ ๋ด์ฅ ๋ฉ์๋๋ก ์ฒ๋ฆฌํ๋๋ผ๊ตฌ์.
ํด์
func bfs(start: Int) {
visited[start] = true
var q = Queue<Int>()
q.push(start)
while !q.isEmpty {
let current = q.pop()!
for i in graph[current] {
if visited[i] == false {
q.push(i)
visited[i] = true
}
}
}
}
์ด๋ ๊ฒ ์ฌ์ฉํฉ๋๋ค.
push๋ enqueue์๋ง, pop์ dequeue์ ๋ํด์๋ง ์ํํฉ๋๋ค.
reversed()๋
stack1์ reversed๋ฅผ ํ ๋๋ง๋ค ์ฌ ์์ฑํ๋ ๊ฒ์ด ์๋, popํ ๋ ๋น์์ ๋๋ง ํ๋๋ก ์ค์ ํด์ผ ์ฌ๋ฐ๋ฅด๊ฒ ๋์ํ ๊ฒ๋๋ค.
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**6)
input = sys.stdin.readline
def dfs(start,visited):
visited[start ] =mark
for node in graph[start]:
if visited[node] == 0:
dfs(node,visited)
N, M = map(int, input().split())
graph=[[] for _ in range(n+1)]
for i in range(m):
start, end = map(int, input().split())
graph[start].append(end)
graph[end].append(start)
visited=[0] * (N+1)
mark = 1
for i in range(1, N+1):
if visited[i] == 0:
dfs(i, visited)
mark += 1
print(max(visited))
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 ๋ฌธ์ ๊ตฐ์ swift ์ queue ์๋๊ฑด ์ง์ง..
ํ์ดํ
์
๋๋ค ํํ
gif ๋๋ฌด ์ด์๊ฒ ์๋ง๋ค์ด์ ์ง๊ด์ ์ด๊ณ ์ข์์ด์!!
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val (n, m) = br.readLine().split(" ").map { it.toInt() }
val graph = List(n + 1) { mutableListOf<Int>() }
val visited = Array<Boolean>(n + 1) { false }
repeat(m) {
val (x, y) = br.readLine().split(" ").map { it.toInt() }
graph[x].add(y)
graph[y].add(x)
}
println((1..n).count { isConnected(it, graph, visited) })
}
private fun isConnected(start: Int, graph: List<List<Int>>, visited: Array<Boolean>): Boolean {
if (visited[start]) {
return false
}
val deque = ArrayDeque<Int>()
deque.add(start)
while (deque.isNotEmpty()) {
val now = deque.removeFirst()
for (next in graph[now]) {
if (visited[next]) {
continue
}
deque.add(next)
visited[next] = true
}
}
return true
}
๐ ๋ฌธ์ ๋งํฌ
์ฐ๊ฒฐ ์์์ ๊ฐ์
โ๏ธ ์์๋ ์๊ฐ
40๋ถ
๐ก ๋ถ๊ฐ ์ค๋ช
๋ค์ด๋ฒ ๋ถ์คํธ ์บ ํ๊ฐ ์ง์ ์ธ์ด๋ก ์ณ์ผํ ๊ฒ ๊ฐ์์ ๋ค์ Swift๋ก ๋ณต๊ทํ์ต๋๋ค..
๊ทธ๋ฐ๋ฐ ์ ๋ง... ์ฌ๋ฌ๊ฐ์ง ํ๋ ๋ถ๋ถ์ด ๋ง์์ ์ผ๋จ ์ ์ด๋ดค์ต๋๋ค...
๊ทธ๋๋ ๊ตฌํ์ ์๋ฃ ํ์ต๋๋ค.
โจ ์๋ ์ฝ๋
์ฌ์ค ์๋์ฝ๋๊ฐ ํ์๊ฐ ์์ ์ ๋๋ก ๊ทธ๋ํ ํ์์ ์๋ฉด ๊ทธ๋ฅ ๋๊ตฌ๋ ํ ์ ์๋ ๋ฌธ์ ์์ต๋๋ค.
๐ ์ ๋ต ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
1๏ธโฃ Swift n,m ์๋ผ์ ์ ๋ ฅ ๋ฐ๊ธฐ
Swift์์ ์ ๋ ฅ์ ๋ฐ์ ๋ split๋ก ์๋ผ์ map์ผ๋ก ๊ฐ๊ฐ์ ๊ฐ์ ๋ฐํํด์ list๋ก ๋ง๋ค์ด ์ฃผ๊ณ
๊ทธ ๋ง๋ list๋ฅผ ์ด์ฉํ์ฌ n,m์ผ๋ก ๋๋ ์คฌ์ต๋๋ค..
2๏ธโฃ ๋น์ด์ง ๋ฐฐ์ด์ ๊ธฐ๋ณธ๊ฐ ๋ฐฐ์ด ๋ง๋ค๊ธฐ
graph ๋ฐฐ์ด์ ๋ง๋ค๋ ๋ ธ๋ ๊ฐฏ์๋งํผ ๋ฐ๋ณต๋ ๋ฐฐ์ด ๋ง๋ค๊ธฐ
์) [0, 0, 0, 0, 0]
3๏ธโฃ queue ์์ด BFS ๋๋ฆฌ๊ธฐ
Swift์์๋ queue๊ฐ ์ ๊ณต๋์ง ์์์ ๋ฐ๋ก
Array๋ removeFirst()ํจ์๊ฐ ์ ๊ณต ๋์ง๋ง O(n)์ผ๋ก ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ ธ์
์ด๋ป๊ฒ ๊ตฌํ์ ํ์ง ์๊ฐํ๋ค๊ฐ ๋ธ๋ก๊ทธ์์ ์ฐธ๊ณ ํ ๋ถ๋ถ์ด
์๋์ ๊ฐ์ ๋ก์ง์ ๋๋ค.
์๋ gif๋ ๋นจ๊ฐ์ ๋ถ๋ถ์ด idx์ด๊ณ queue๊ฐ ๋ฐฐ์ด์ ๋๋ค.
4๏ธโฃ ๋ฐํ์ ์๋ฌ ์ฐพ๊ธฐ
์ ์ฌ์ง๊ณผ ๊ฐ์ด ๊ณ์ํด์ ๋ฐํ์ ์๋ฌ๊ฐ ๋ฐ์ํ์๋ค..์ฒ์์๋ ๋ฐํ์ ์๋ฌ๊ธธ๋ ํฌ๊ธฐ๋ฅผ ์๋ชป์คฌ๋ ํ๋ฉด์
์ค์ํํธ ์์ ์์ฒญ ํ๊ณ ์๋๋ฐ
์ฝ๋๊ฐ ์ ํ ๋ค๋ฅธ ๋ถ๋ถ์ด ์๋ ๊ฒ ๊ฐ์ ์ฝ๋๋ฅผ ๋ค๊ณ ์์ ๋น๊ตํด๋ด๋ ํด๋น ์ฝ๋๋ ๋ง๊ณ ๋ด ์ฝ๋๋ ๋ฐํ์ ์๋ฌ๊ฐ ๋ฐ์ํ๊ธธ๋
์ญ ์ดํด๋ณด๋๊น....
์ด๋ ๊ฒ ์ฝ๋๊ฐ ๋์์๋๋ฐ
์์ธํ ์ดํด๋ณด๋ฉด _๋ก ์ฌ์ฉํด์ ๋ฐ๋ก ๋ฐ๋ณต๋ฌธ์ ํ์๋ง ์ค์ํ๋ค๊ณ ์๊ฐํด์ ๊ณ์ํด์ ๋์ด๊ฐ์ง๋ง
๋ฌธ์ ๋
์ด ์๋ค.
M์ด 0๋ถํฐ ์์์ด ๋์ 1...m์ ํ๋ฉด 1 -> 0์ผ๋ก ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๋ถ๋ถ์ด์๋ค..
๋ฌธ์ ๋ฅผ ์ ์ฝ์