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

8-ljedd2 #154

Merged
merged 10 commits into from
Sep 25, 2024
Merged

8-ljedd2 #154

merged 10 commits into from
Sep 25, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Sep 5, 2024

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

๋ฐฑ์ค€ ์œ„์ƒ์ •๋ ฌ ๊ธฐ๋ณธ๋ฌธ์ œ | BOJ 1766 ๋ฌธ์ œ์ง‘

๋ฌธ์ œ ์„ค๋ช…

  • ๋ฌธ์ œ๋Š” ๋‚œ์ด๋„ ์ˆœ์„œ๋กœ ์ถœ์ œ๋˜์–ด ์žˆ๋‹ค. ์ฆ‰ 1๋ฒˆ ๋ฌธ์ œ๊ฐ€ ๊ฐ€์žฅ ์‰ฌ์šด ๋ฌธ์ œ์ด๊ณ  N๋ฒˆ ๋ฌธ์ œ๊ฐ€ ๊ฐ€์žฅ ์–ด๋ ค์šด ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค.
  • '๋จผ์ € ํ‘ธ๋Š” ๊ฒƒ์ด ์ข‹์€ ๋ฌธ์ œ'๊ฐ€ ์žˆ๋‹ค.
    -> 1๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋‚˜๋ฉด 4๋ฒˆ ๋ฌธ์ œ๊ฐ€ ์‰ฝ๊ฒŒ ํ’€๋ฆฌ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.
  • N๊ฐœ์˜ ๋ฌธ์ œ๋Š” ๋ชจ๋‘ ํ’€์–ด์•ผ ํ•œ๋‹ค.
  • ๋จผ์ € ํ‘ธ๋Š” ๊ฒƒ์ด ์ข‹์€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๋ฌธ์ œ๋Š”, ๋จผ์ € ํ‘ธ๋Š” ๊ฒƒ์ด ์ข‹์€ ๋ฌธ์ œ๋ฅผ ๋ฐ˜๋“œ์‹œ ๋จผ์ € ํ’€์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ€๋Šฅํ•˜๋ฉด ์‰ฌ์šด ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€์–ด์•ผ ํ•œ๋‹ค.

๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜์™€ ๋จผ์ € ํ‘ธ๋Š” ๊ฒƒ์ด ์ข‹์€ ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด์„œ ๋ฏผ์˜ค๊ฐ€ ํ’€ ๋ฌธ์ œ์˜ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•ด ์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

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

2์‹œ๊ฐ„ +@

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

๋ฌธ์ œ์—์„œ ์•ž ๋ฒˆํ˜ธ์˜ ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ฝ๊ณ  ์ •๋ ฌ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋จผ์ € ํ•ด๋‘๊ณ  ํ’€์–ด์•ผํ•˜๋‚˜? ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์‚ฝ์งˆ์„ ํ•˜๋‹ค๋ณด๋‹ˆ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๋– ์˜ฌ๋ ธ๊ณ  ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๋‹ค์‹œ ๋ณต์Šตํ•˜์—ฌ ์œ„์ƒ์ •๋ ฌ์— ๋‚œ์ด๋„๋ฅผ ๊ฐ€์ค‘์น˜๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์•ˆ์“ฐ๊ณ  min() ์œผ๋กœ ํ’€๋ ค๊ณ  ํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์šฐ๋ฅด๋ฅด๋ฅดใ…ก๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์—์„œ๋„ ์ด ๋ฌธ์ œ๋Š” ์œ„์ƒ์ •๋ ฌ์ด๋‹ค ! ๋ผ๊ณ  ๋Œ€๋†“๊ณ  ํ‘œํ˜„์ด ๋˜์–ด์žˆ๊ธฐ๋„ ํ•˜๊ณ  ์œ„์ƒ ์ •๋ ฌ์„ ํ™œ์šฉํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ œ๋ผ ์•ž์„œ ๋ฐฐ์› ๋˜ ๊ธฐ๋ณธ ๋™์ž‘ ๊ณผ์ •์„ ์ตœ๋Œ€ํ•œ ์•ˆ๋ณด๊ณ  ๊ตฌํ˜„ํ•ด๋ณด๋ ค ๋…ธ๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค!

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

์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์˜ฌ ๋•Œ, ๊ทธ๋ž˜ํ”„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค.
image

๋ฌธ์ œ์—์„œ์˜ 2๊ฐ€์ง€ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋จผ์ € ํ‘ธ๋Š” ๊ฒƒ์ด ์ข‹์€ ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€์–ด์•ผ ํ•œ๋‹ค๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ ์„ ํ–‰ ์กฐ๊ฑด์ด ์žˆ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”์ง€ ์‚ดํŽด๋ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
4,5๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  1๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•˜๋ฉฐ 2,3๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  5๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์„ ํ–‰ ์กฐ๊ฑด์ด ํ•„์š” ์—†์œผ๋ฉฐ ์‰ฌ์šด ๋ฌธ์ œ๋ถ€ํ„ฐ ํ์— ๋„ฃ๊ณ  ํ•˜๋‚˜์”ฉ ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
image

2๋ฒˆ, 3๋ฒˆ, 4๋ฒˆ์˜ ๊ฒฝ์šฐ ์„ ์ˆ˜ ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— (์ง„์ž…์ฐจ์ˆ˜ = 0) ์ˆœ์„œ๋Œ€๋กœ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

5๋ฒˆ์˜ ๊ฒฝ์šฐ 2๋ฒˆ, 3๋ฒˆ์ด ํ•ด๊ฒฐ๋˜๋ฉฐ ์„ ์ˆ˜๋ฌธ์ œ ์กฐ๊ฑด์ด -2๋˜์–ด ์ง„์ž… ์ฐจ์ˆ˜๊ฐ€ 0์ด ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 5๋ฒˆ ๋ฌธ์ œ๋„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ 5๋ฒˆ์ด ํ•ด๊ฒฐ๋˜์—ˆ์œผ๋‹ˆ 1๋ฒˆ์ด ๊ฐ™์ด ํ•ด๊ฒฐ๋˜๋ฉด์„œ
๋ฌธ์ œ ์ˆœ์„œ๋Š” 2 -> 3 -> 4 -> 5 -> 1 ์ด ๋ฉ๋‹ˆ๋‹ค.

image

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

import heapq

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)
    indegree[e] += 1  # ์ง„์ž… ์ฐจ์ˆ˜ ๋ฐ์ดํ„ฐ ์ €์žฅ

# ๋ฌธ์ œ์—์„œ ๊ฐ€๋Šฅํ•œ ์•ž ๋ฒˆํ˜ธ์˜ ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€์–ด์•ผ ํ•จ -> ์šฐ์„ ์ˆœ์œ„ํ,,?
# 1~N์˜ ๋‚œ์ด๋„ ์ˆœ ๋Œ€๋กœ

queue = []
# ์ˆœ์„œ
for i in range(1, n + 1):
    if indegree[i] == 0:
        heapq.heappush(queue, i) # ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ๊ฑฐ ๋จผ์ € ๋‚œ์ด๋„๋ž‘ ์ €์žฅ

while queue:
    now = heapq.heappop(queue)
    print(now, end=' ')

    for i in graph[now]: # ์œ„์ƒ ์ •๋ ฌ ์ˆ˜ํ–‰ , ์ง„์ž… ์ฐจ์ˆ˜ ์—†์• !
        indegree[i] -= 1

        if indegree[i] == 0:
            heapq.heappush(queue, i)

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

@jung0115
Copy link
Member

jung0115 commented Sep 7, 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.

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์œ„์ƒ์ •๋ ฌ์„ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์€ ํ–ˆ๋Š”๋ฐ ์šฐ์„ ์ˆœ์œ„ ํ๊นŒ์ง€ ๋– ์˜ฌ๋ฆฌ์‹  ๊ฒŒ ๋Œ€๋‹จํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ„์ƒ์ •๋ ฌ์— ๋‚œ์ด๋„๋ฅผ ๊ฐ€์ค‘์น˜๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ ์šฉํ•˜๋Š” ์•„์ด๋””์–ด๊ฐ€ ์ธ์ƒ๊นŠ์—ˆ์”€๋‹ˆ๋‹ค,,๐Ÿคฉ๐Ÿฅน

import heapq

n, m = map(int, input().split())  
graph = [[] for _ in range(n + 1)]  
indegree = [0] * (n + 1)  

for _ in range(m):
    start, end = map(int, input().split())
    graph[start].append(end)
    indegree[end] += 1 

queue = []
for i in range(1, n + 1):
    if indegree[i] == 0:
        heapq.heappush(queue, i)

result = [] 
while queue:
    current_problem = heapq.heappop(queue)  
    result.append(current_problem)

    for next_problem in graph[current_problem]:
        indegree[next_problem] -= 1
        if indegree[next_problem] == 0: 
            heapq.heappush(queue, next_problem)

print(' '.join(map(str, result)))

์ •์€๋‹˜์˜ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ณด๋ฉด์„œ ์กฐ๊ธˆ ๋” ๊ฐ€๋…์„ฑ์žˆ๊ฒŒ ์ˆ˜์ •ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!
์ด๋ฒˆ 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