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-janghw0126 #158

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

10-janghw0126 #158

wants to merge 7 commits into from

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Sep 9, 2024

πŸ”— 문제 링크

λ°±μ€€ | 쀄 μ„Έμš°κΈ°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

이 λ¬Έμ œλŠ” 두 ν•™μƒμ˜ ν‚€λ₯Ό λΉ„κ΅ν•˜λ©΄μ„œ 학생듀을 μœ„μƒ 정렬을 μ΄μš©ν•˜μ—¬ 쀄 μ„Έμš°λŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€. 문제의 지문에 따라, μ–΄λ–€ 학생이 λ‹€λ₯Έ 학생보닀 μ•žμ— μ„œμ•Ό ν•œλ‹€λŠ” 쑰건을 λ§Œμ‘±μ‹œν‚€λ©΄μ„œ λͺ¨λ“  학생을 λ‚˜μ—΄ν•΄μ•Ό ν•˜λ―€λ‘œ μˆœμ„œκ°€ μ •ν•΄μ Έ μžˆλŠ” μž‘μ—…μ„ μ°¨λ‘€λŒ€λ‘œ μˆ˜ν–‰ν•΄μ•Ό λ˜λŠ” μœ„μƒμ •λ ¬μ„ μ‚¬μš©ν•΄μ•Όκ² λ‹€λŠ” 생각을 ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

πŸ“ 문제 ν•΄κ²° κ³Όμ •

  1. 총 학생 μˆ˜μ™€ 비ꡐ 횟수λ₯Ό μž…λ ₯λ°›κ³ , 각 학생 κ°„μ˜ 비ꡐ 관계λ₯Ό μ €μž₯ν•  κ·Έλž˜ν”„μ™€ μ§„μž… 차수λ₯Ό μ €μž₯ν•  배열을 λ§Œλ“­λ‹ˆλ‹€.
  2. λ¨Όμ €, μ§„μž… μ°¨μˆ˜κ°€ 0인 학생(μ•žμ— μ„œμ•Ό ν•˜λŠ” 학생이 μ—†λŠ” 학생)을 큐에 μΆ”κ°€ν•©λ‹ˆλ‹€. -> μœ„μƒμ •λ ¬ 진행!
  3. νμ—μ„œ 학생을 ν•˜λ‚˜μ”© κΊΌλ‚΄λ©°, ν•΄λ‹Ή ν•™μƒμ˜ μ§„μž… 차수λ₯Ό 0으둜 λ§Œλ“€κ³  κ·Έ ν•™μƒμ˜ 뒀에 μ„œμ•Ό ν•˜λŠ” ν•™μƒλ“€μ˜ μ§„μž… 차수λ₯Ό ν•˜λ‚˜μ”© μ€„μ—¬μ€λ‹ˆλ‹€.
  4. λ§Œμ•½ μ–΄λ–€ ν•™μƒμ˜ μ§„μž… μ°¨μˆ˜κ°€ 0이 되면, κ·Έ 학생을 큐에 λ„£μ–΄ 쀄을 μ„Έμš°λŠ” 과정을 λ°˜λ³΅ν•©λ‹ˆλ‹€.

🚨 핡심 둜직

이 λ¬Έμ œλŠ” 싸이클이 μ—†κΈ° λ•Œλ¬Έμ— μœ„μ—μ„œ λ§ν–ˆλ“―μ΄ μœ„μƒ 정렬을 톡해 학생듀을 μ •λ ¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 큐가 λΉ„μ–΄κ°ˆ λ•ŒκΉŒμ§€, μ§„μž… μ°¨μˆ˜κ°€ 0인 학생듀을 μ°¨λ‘€λ‘œ μ²˜λ¦¬ν•΄μ£Όλ©΄ μˆœμ„œλŒ€λ‘œ 쀄을 μ„ΈμšΈ 수 μžˆμœΌλ―€λ‘œ μ΄λŸ¬ν•œ λ‘œμ§μ„ λ°”νƒ•μœΌλ‘œ 문제λ₯Ό ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

πŸ‘©β€πŸ’» μ΅œμ’… μ½”λ“œ

import sys
from collections import deque

# 학생 수(n)와 비ꡐ 횟수(m)λ₯Ό μž…λ ₯λ°›μŒ
n, m = map(int, sys.stdin.readline().split())

# κ·Έλž˜ν”„μ™€ μ§„μž… 차수 리슀트λ₯Ό μ΄ˆκΈ°ν™”ν•¨
graph = [[] for _ in range(n + 1)]
deg = [0] * (n + 1)
q = deque()
res = []

# 비ꡐ 관계 μž…λ ₯κ³Ό κ·Έλž˜ν”„λ₯Ό ꡬ성함
for _ in range(m):
    u, v = map(int, sys.stdin.readline().rstrip().split())
    # uκ°€ v μ•žμ— μ„œμ•Ό 함
    graph[u].append(v)
    # v의 μ§„μž… 차수λ₯Ό μ¦κ°€μ‹œν‚΄
    deg[v] += 1

# μ§„μž… μ°¨μˆ˜κ°€ 0인 λ…Έλ“œ 큐에 μ‚½μž…ν•¨
for i in range(1, n + 1):
    if deg[i] == 0:
        q.append(i)

# μœ„μƒ 정렬을 μ‹œμž‘ν•¨
while q:
    # μ§„μž… μ°¨μˆ˜κ°€ 0인 λ…Έλ“œλ₯Ό νμ—μ„œ 꺼냄
    cur = q.popleft()
    # κ²°κ³Ό λ¦¬μŠ€νŠΈμ— 좔가함
    res.append(cur) 

    # ν˜„μž¬ λ…Έλ“œ 뒀에 μ„œμ•Ό ν•˜λŠ” λ…Έλ“œλ“€μ˜ μ§„μž… 차수λ₯Ό κ°μ†Œμ‹œν‚΄
    for next_node in graph[cur]:
        deg[next_node] -= 1
        # μ§„μž… μ°¨μˆ˜κ°€ 0이 되면 큐에 좔가함
        if deg[next_node] == 0:
            q.append(next_node)

# κ²°κ³Ό 좜λ ₯
print(*res)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

μœ„μƒμ •λ ¬μ„ λ§ˆμŠ€ν„°ν•΄λ³΄κ³ μž μš”μ¦˜ 이 μœ ν˜• 문제만 죽도둝 파고 μžˆλŠ”λ° μ—­μ‹œ 쉽지 μ•Šμ€ μœ ν˜•μ΄κ΅°μ—¬,,,,
μ—΄μ‹¬νžˆ μ•„μžμž£ ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹·πŸ˜…πŸ˜‚πŸ˜‚

@jung0115
Copy link
Member

저도 ν˜œμ› λ‹˜ ν’€μ΄λž‘ λΉ„μŠ·ν•˜κ²Œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€! μœ„μƒμ •λ ¬ λ§ˆμŠ€ν„°λŠ” 쉽지 μ•Šμ€ 것 κ°™μ•„μš” πŸ˜… 저도 λ”°λΌμ„œ μ—΄μ‹¬νžˆ ν•˜κ² μŠ΅λ‹ˆλ‹€! 이번 μ°¨μ‹œλ„ μˆ˜κ³ ν•˜μ…¨μ–΄μš”

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)
}

Copy link
Collaborator

@LJEDD2 LJEDD2 left a 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)

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.

3 participants