-
Notifications
You must be signed in to change notification settings - Fork 5
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-janghw0126 #158
base: main
Are you sure you want to change the base?
10-janghw0126 #158
Conversation
μ λ νμ λ νμ΄λ λΉμ·νκ² νμμ΅λλ€! μμμ λ ¬ λ§μ€ν°λ μ½μ§ μμ κ² κ°μμ π μ λ λ°λΌμ μ΄μ¬ν νκ² μ΅λλ€! μ΄λ² μ°¨μλ μκ³ νμ ¨μ΄μ import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer
import java.util.LinkedList
import java.util.Queue
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
var st = StringTokenizer(br.readLine())
val N = st.nextToken().toInt()
val M = st.nextToken().toInt()
var graph = Array(N+1, {mutableListOf<Int>()})
var edgeCnt = Array(N+1, {0})
for(i: Int in 0..M-1) {
st = StringTokenizer(br.readLine())
val A = st.nextToken().toInt()
val B = st.nextToken().toInt()
graph[A].add(B)
edgeCnt[B]++ // μ§μ
μ°¨μ κΈ°λ‘
}
var queue = LinkedList<Int>() as Queue<Int>
var answer = StringBuilder()
// μ§μ
μ°¨μ 0μΈ κ°μ νμ = λ³ΈμΈλ³΄λ€ μμ μμΌνλ μ¬λμ΄ μλ€
for(i: Int in 1..N) {
if(edgeCnt[i] == 0)
queue.offer(i);
}
while(!queue.isEmpty()) {
var student = queue.poll()
// λ¨μ νμ μ€μ λ³ΈμΈλ³΄λ€ μμ μμΌνλ μ¬λμ΄ μμΌλ―λ‘ λ°λ‘ μ€ μΈμμ€
answer.append(student).append(" ")
for(i: Int in 0..graph[student].size - 1) {
// νμ¬ νμλ³΄λ€ λ€μ μμΌνλ νμ => μμ μΈμμΌ νλ μ¬λμ΄ ν λͺ
μ€μ΄λ¦
var back = graph[student][i]
edgeCnt[back]--
// ν λͺ
μ€μ΄λ€λ©΄μ λμ΄μ μμ μμΌνλ μ¬λμ΄ μμΌλ©΄ νμ μΆκ°
if(edgeCnt[back] == 0) {
queue.offer(back)
}
}
}
print(answer)
} |
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.
μμμ λ ¬μ λν μ€λͺ
μ κΉλνκ² μμ±ν΄μ£Όμ
μ μ½κ² μ΄ν΄κ° λμμ΅λλ€!
κ²°κ³Ό μΆλ ₯ λΆλΆμ μ΄λ κ² κ³μ° κ²°κ³Όλ₯Ό μ¦μ μΆλ ₯νλλ‘ μ½λλ₯Ό μμ±ν΄λ λ©λλ€! π
while queue: # μμ μ λ ¬ μν , μ§μ
μ°¨μ μμ !
now = queue.popleft()
print(now, end=' ')
for next in A[now]:
indegree[next] -= 1 # μ§μ
μ°¨μ 0μΈ μ μ νμ μ½μ
if not indegree[next]:
queue.append(next)
π λ¬Έμ λ§ν¬
λ°±μ€ | μ€ μΈμ°κΈ°
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
μ΄ λ¬Έμ λ λ νμμ ν€λ₯Ό λΉκ΅νλ©΄μ νμλ€μ μμ μ λ ¬μ μ΄μ©νμ¬ μ€ μΈμ°λ λ¬Έμ μμ΅λλ€. λ¬Έμ μ μ§λ¬Έμ λ°λΌ, μ΄λ€ νμμ΄ λ€λ₯Έ νμλ³΄λ€ μμ μμΌ νλ€λ 쑰건μ λ§μ‘±μν€λ©΄μ λͺ¨λ νμμ λμ΄ν΄μΌ νλ―λ‘ μμκ° μ ν΄μ Έ μλ μμ μ μ°¨λ‘λλ‘ μνν΄μΌ λλ μμμ λ ¬μ μ¬μ©ν΄μΌκ² λ€λ μκ°μ νμμ΅λλ€.
π λ¬Έμ ν΄κ²° κ³Όμ
π¨ ν΅μ¬ λ‘μ§
μ΄ λ¬Έμ λ μΈμ΄ν΄μ΄ μκΈ° λλ¬Έμ μμμ λ§νλ―μ΄ μμ μ λ ¬μ ν΅ν΄ νμλ€μ μ λ ¬ν μ μμ΅λλ€. νκ° λΉμ΄κ° λκΉμ§, μ§μ μ°¨μκ° 0μΈ νμλ€μ μ°¨λ‘λ‘ μ²λ¦¬ν΄μ£Όλ©΄ μμλλ‘ μ€μ μΈμΈ μ μμΌλ―λ‘ μ΄λ¬ν λ‘μ§μ λ°νμΌλ‘ λ¬Έμ λ₯Ό νμμ΅λλ€.
π©βπ» μ΅μ’ μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μμμ λ ¬μ λ§μ€ν°ν΄λ³΄κ³ μ μμ¦ μ΄ μ ν λ¬Έμ λ§ μ£½λλ‘ νκ³ μλλ° μμ μ½μ§ μμ μ νμ΄κ΅°μ¬,,,,
μ΄μ¬ν μμμ£ ν΄λ³΄κ² μ΅λλ·π ππ