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

7 ljedd2 #152

Merged
merged 8 commits into from
Sep 25, 2024
Merged

7 ljedd2 #152

merged 8 commits into from
Sep 25, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Sep 2, 2024

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

๋ฐฑ์ค€ ์œ„์ƒ์ •๋ ฌ ๊ธฐ๋ณธ๋ฌธ์ œ | BOJ 2252 ์ค„ ์„ธ์šฐ๊ธฐ

๋ฌธ์ œ ์„ค๋ช…

  • N๋ช…์˜ ํ•™์ƒ์ด ๊ฐ ํ•™์ƒ์˜ ํ‚ค๋ฅผ ์ง์ ‘ ์žฌ์„œ ์ •๋ ฌํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒ ์ง€๋งŒ, ๋งˆ๋•…ํ•œ ๋ฐฉ๋ฒ•์ด ์—†์–ด์„œ ๋‘ ํ•™์ƒ์˜ ํ‚ค๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.
  • ๋ชจ๋“  ํ•™์ƒ๋“ค์„ ๋‹ค ๋น„๊ตํ•ด ๋ณธ ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ์ผ๋ถ€ ํ•™์ƒ๋“ค์˜ ํ‚ค๋งŒ์„ ๋น„๊ตํ•ด ๋ณด์•˜๋‹ค.
    ์ผ๋ถ€ ํ•™์ƒ๋“ค์˜ ํ‚ค๋ฅผ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ค„์„ ์„ธ์šฐ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ œํ•œ์‚ฌํ•ญ

  • ์ฒซ์งธ ์ค„์— N(1 โ‰ค N โ‰ค 32,000), M(1 โ‰ค M โ‰ค 100,000), M์€ ํ‚ค๋ฅผ ๋น„๊ตํ•œ ํšŒ์ˆ˜
  • M๊ฐœ์˜ ์ค„์—๋Š” ํ‚ค๋ฅผ ๋น„๊ตํ•œ ๋‘ ํ•™์ƒ์˜ ๋ฒˆํ˜ธ A, B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
    (์ด๋Š” ํ•™์ƒ A๊ฐ€ ํ•™์ƒ B์˜ ์•ž์— ์„œ์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ)

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

์œ„์ƒ์ •๋ ฌ ๊ณต๋ถ€ํ•˜๊ณ  ์—ฐ์Šต๋ฌธ์ œ๋กœ ํ’€์—ˆ๋‹ต๋‹ˆ๋‹ค :)

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

์œ„์ƒ์ •๋ ฌ ๊ฐœ๋… ์ •๋ฆฌ

๊ฐ“ํ˜œ์›๋‹˜์˜ ํ’€์ด ๊ธ€์„ ์ฐธ๊ณ ํ•˜์—ฌ ์œ„์ƒ์ •๋ ฌ ์ฐ๋จน์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๊ฐ€ ์ „ํ˜•์ ์ธ ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋ผ๊ณ  ํ•˜์—ฌ ์‹œ๋„ํ•ด๋ดค์Šต๋‹ˆ๋‹ค.
์ˆœ์„œ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ์ผ๋ จ์˜ ์ž‘์—…์„ ์ฐจ๋ก€๋Œ€๋กœ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฌธ์ œ์—์„œ๋Š” A๋Š” ๋ฌด์กฐ๊ฑด B๋ณด๋‹ค ์•ž์— ์™€์•ผํ•œ๋‹ค๋Š” ์ , A -> B๋ฅผ ๋งŒ์กฑ์‹œํ‚ค๋ฉด์„œ ์ •๋ ฌ์„ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์ƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค !

๋”ฐ๋ผ์„œ ๋ฌธ์ œ ์ ‘๊ทผ์€ ์œ„์ƒ์ •๋ ฌ์˜ ๊ธฐ๋ณธ ๊ณผ์ • ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํ•™์ƒ๋“ค์€ 4๋ช…์ด ์žˆ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์ด ์ฃผ์–ด์งˆ ๋•Œ,

4 -> 2
3 -> 1

4๋ฒˆ์€ ๋ฐ˜๋“œ์‹œ 2๋ฒˆ ์•ž์— ์žˆ์œผ๋ฏ€๋กœ 4 -> 2 ๋กœ์˜ ๊ฐ„์„ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ 2๋ฒˆ์˜ ์ง„์ž…์ฐจ์ˆ˜๋Š” 1์ด ๋ฉ๋‹ˆ๋‹ค.
3๋ฒˆ์€ ๋ฐ˜๋“œ์‹œ 1๋ฒˆ ์•ž์— ์™€์•ผ ํ•˜๋ฏ€๋กœ 3 -> 1 ๋กœ์˜ ๊ฐ„์„ ์ด ์กด์žฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ 1๋ฒˆ์˜ ์ง„์ž…์ฐจ์ˆ˜๋Š” 1์ด ๋ฉ๋‹ˆ๋‹ค.

์ง„์ž…ํ•˜๋Š” ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ์ง„์ž…์ฐจ์ˆ˜ = 0์ธ 3๋ฒˆ๊ณผ 4๋ฒˆ์„ ํ์— ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์€ ๋‹ค์Œ
ํ์—์„œ ๋จผ์ € 3์„ ๊บผ๋‚ด 3๊ณผ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ๋“ค์„ ์ œ๊ฑฐํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค.

  1. ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ์ •์ ์„ ํ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
  2. ํ์—์„œ ์›์†Œ๋ฅผ ๊บผ๋‚ด ํ•ด๋‹น ์›์†Œ์™€ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์„ ๋ชจ๋‘ ์ œ๊ฑฐ
  3. ์ œ๊ฑฐํ•œ ํ›„์— ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ์ •์ ์„ ํ์— ์‚ฝ์ž…
  4. ์ดํ›„ 2๋ฒˆ, 3๋ฒˆ ๊ณผ์ •์„ ๋ฐ˜๋ณต

์œ„์ƒ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„๋Š” ์‚ฌ์ดํด์ด ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ๋ชจ๋“  ์›์†Œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์ „์— ํ๊ฐ€ ๋น„๊ฒŒ ๋œ๋‹ค๋ฉด ์‚ฌ์ดํด์ด ์กด์žฌํ•œ๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ตœ์ข… ์ฝ”๋“œ

# BOJ2252 ์ค„ ์„ธ์šฐ๊ธฐ
from collections import deque

n, m = map(int, input().split())
graph = [[] for _ in range(n + 1)]
indegree = [0] * (n + 1) # ์ง„์ž… ์ฐจ์ˆ˜ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ

for i in range(m):
    s,e = map(int, input().split())
    graph[s].append(e) # zz
    indegree[e] += 1 # ์ง„์ž… ์ฐจ์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ

# B๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด A๋ผ๋Š” ์ž‘์—…์„ ๋จผ์ € ํ•ด์•ผ ํ•˜๋Š” ๊ตฌ์กฐ๊ฐ€ ์žˆ์„ ๋•Œ, 
# ๊ทธ ์ž‘์—… ์ˆœ์„œ๋ฅผ ๊ตฌํ•ด์ฃผ๋Š” ๊ฒƒ = ์œ„์ƒ์ •๋ ฌ 

queue = deque()
for i in range(1, n + 1):
    if not indegree[i]:
        queue.append(i)

while queue: # ์œ„์ƒ ์ •๋ ฌ ์ˆ˜ํ–‰ , ์ง„์ž… ์ฐจ์ˆ˜ ์—†์• !
    now = queue.popleft()
    print(now, end=' ')

    for next in graph[now]:
        indegree[next] -= 1 # ์ง„์ž… ์ฐจ์ˆ˜ 0์ธ ์ •์  ํ์— ์‚ฝ์ž…
        if not indegree[next]:
            queue.append(next)

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

image

image

@jung0115
Copy link
Member

jung0115 commented Sep 4, 2024

๋‹ค์‹œ ๋ณต๊ท€ํ•˜์…จ๊ตฐ์š”!! ๐Ÿฅณ๐Ÿฅณ
์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋‹ค! ํŒŒ์•…ํ•˜๋Š” ๏ฟฝ๊ฒƒ๋„ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์•„์š” ์œ„์ƒ์ •๋ ฌ์— ์ต์ˆ™ํ•ด์ง€๋ฉด ์ข€ ๋‚˜์•„์งˆ๊นŒ์š”...๐Ÿฅฒ ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋ฅผ ์ข€ ๋” ๋งŽ์ด ํ’€์–ด๋ณด๊ณ  ๋งˆ์Šคํ„ฐ๋ฅผ ํ•ด์•ผ๊ฒ ์–ด์š”!!

์ค„ ์„ธ์šฐ๊ธฐ ๋ฌธ์ œ๋Š” ์ €๋„ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค ๐Ÿ‘

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

@LJEDD2
Copy link
Collaborator Author

LJEDD2 commented Sep 5, 2024

๋‹ค์‹œ ๋ณต๊ท€ํ•˜์…จ๊ตฐ์š”!! ๐Ÿฅณ๐Ÿฅณ ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋‹ค! ํŒŒ์•…ํ•˜๋Š” ๏ฟฝ๊ฒƒ๋„ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์•„์š” ์œ„์ƒ์ •๋ ฌ์— ์ต์ˆ™ํ•ด์ง€๋ฉด ์ข€ ๋‚˜์•„์งˆ๊นŒ์š”...๐Ÿฅฒ

๊ธฐ๋ณธ๋ฌธ์ œ ์Šค์œฝ ๊ตฌํ˜„ํ•ด๋ดค๋‹ค๊ณ  ์˜ค!! ์ด์ œ ๋‚˜๋„ ์œ„์ƒ์ •๋ ฌ ์ž…๋ฌธ์ž ?! ํ•˜๊ณ  ๋‘๊ทผ๋‘๊ทผํ•˜๋Š” ๋งˆ์Œ์œผ๋กœ ์‘์šฉ๋ฌธ์ œ ๋“ค์—ฌ๋‹ค๋ณด๋Ÿฌ ๊ฐ”๋‹ค ํ˜ผ๋‚˜๊ณ  ์˜ค๋Š” ๊ธธ์ž…๋‹ˆ๋‹ค ... ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค๊ณผ ์—ฐ๊ณ„ํ•ด์„œ ๋”ฑ ๋– ์˜ฌ๋ฆฌ๊ธฐ๋Š” ์•„์ง ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์•„์š”
ํ’€๋‹ค ๋ณด๋ฉด ์ต์ˆ™ํ•ด์ง€์ง€ ์•Š์„๊นŒ ...์š” ? ใ…Žใ…Ž

์ด๋ฒˆ์ฃผ๋„ ํ™”์ดํŒ…์ž…๋‹ˆ๋‹ค~!

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

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

์ €๋„ ์š”์ฆ˜ ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋“ค์„ ๋งˆ์Šคํ„ฐํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์ •์€๋‹˜๋„ ๋„์ „ํ•˜์…จ๊ตฐ์š”!
์ œ PR์„ ๋ณด๊ณ  ํ’€์–ด๋ณด์…จ๋‹ค๋‹ˆ,,์ „ ์•„์ง ์•„๋ฌด๊ฒƒ๋‘ ๋ชจ๋ฅด๋Š” ๊ฐ์ž์ด์ง€๋งŒ ๋ฟŒ๋“ฏํ•ฉ๋‹ˆ๋‹ค ํ—ˆํ—›๐Ÿ˜
์•ž์œผ๋กœ ๋”๋”์šฑ ์—ด์‹ฌํžˆ ํ•ด์•ผ ๋˜๊ฒ ๋Š”๊ฑธ์š”?!

์ €๋‘ ์ •์€๋‹˜๊ณผ ๋น„์Šทํ•œ ๋กœ์ง์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

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)

์ž์„ธํ•œ ํ’€์ด๋Š” 10์ฐจ์‹œ PR์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!

์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹น๐Ÿ˜„

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