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

24-yuyu0830 #90

Merged
merged 3 commits into from
Sep 2, 2024
Merged

24-yuyu0830 #90

merged 3 commits into from
Sep 2, 2024

Conversation

yuyu0830
Copy link
Collaborator

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

์ค„ ์„ธ์šฐ๊ธฐ

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

10๋ถ„
๋ฐ€๋ฆฐ๊ฒŒ ๋งŽ์•„์„œ ๋ณต์Šต ๊ฒธ ์กฐ๊ธˆ ์‰ฌ์šด ๋ฌธ์ œ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

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

โ“ ๋ฌธ์ œ

image

๊ฐ„๋‹จํ•œ ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋‹ค. ๊ทธ๋ƒฅ ์œ„์ƒ์ •๋ ฌ ๊ทธ ์žก์ฑ„


โ— ํ’€์ด

์œ„์ƒ์ •๋ ฌ์— ๊ด€ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌํ–ˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์ž

๊ทผ๋ฐ ์ด๋ฒˆ ๋ฌธ์ œ๊ฐ€ ์œ„์ƒ์ •๋ ฌ +-0 ์ธ ๋ฌธ์ œ๋ผ ๋ธ”๋กœ๊ทธ ํ•ด์„ค ๋นผ๋ฉด ์„ค๋ช…ํ•  ๊ฒŒ ์—†๋‹ค..

์œ„์ƒ ์ •๋ ฌ์—๋Š” ํ์™€ ์Šคํƒ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ ๊ฐœ์ธ์ ์œผ๋กœ ํ๊ฐ€ ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•ด ํ๋ฅผ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐํ–ˆ๋‹ค.

์œ„์ƒ ์ •๋ ฌ์—์„œ๋Š” ์ง„์ž… ์ฐจ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” intํ˜• ๋ณ€์ˆ˜ rule ์™€ ๊ฐ„์„ ์„ ์ €์žฅํ•˜๋Š” vectorํ˜• ๋ณ€์ˆ˜ enter ๋ฅผ ํ†ตํ•ด ์ €์žฅํ–ˆ๋‹ค. ๊ฐ„์„ ์˜ ๊ฒฝ์šฐ ๋‹จ๋ฐฉํ–ฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์€ ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

์ดํ›„ enter๊ฐ€ 0์ธ ๋ณ€์ˆ˜๋“ค์„ empty ํ์— ๋„ฃ๊ณ  ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค.

๋ฐฉ๋ฌธํ•œ index์— ํ•ด๋‹นํ•˜๋Š” ๋…ธ๋“œ์˜ ๊ฐ„์„ ์„ ์ „๋ถ€ ์ œ๊ฑฐํ•˜๊ณ , empty ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค.

๋ฌดํ•œ ๋ฐ˜๋ณตํ•˜๋ฉด ๋—!

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

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

๊ฐ€์žฅ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๊ฒƒ๋“ค์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ฒƒ๋“ค์ด ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ›„์ˆœ์œ„์— ์žˆ๋Š” ์ˆซ์ž๋“ค์— ์ „์ˆœ์œ„์— ์žˆ๋Š” ์ˆซ์ž๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ๋„ฃ์–ด์คŒ์œผ๋กœ์จ ๋‚˜์ค‘์— ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ๋ฐฉ์‹ ์ธ์ƒ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ๊ฐ€ ์•ฝ๊ฐ„ ์กฐ๊ธˆ ๊ผฌ๋กฌํ•œ๊ฒŒ ์ถฉ๋ถ„ํžˆ ์ž˜ ๋ชป ์ดํ•ดํ•  ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ ์กฐ๊ธˆ ๊ทธ๋ ‡๋„ค์š”

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

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

์žฌ๋ฐŒ๋Š” ์œ„์ƒ์ •๋ ฌ ์ €๋„ ํ•œ ๋ฒˆ ํ’€์–ด๋ดค๋Š”๋ฐ์š”. ์ €๋ฒˆ์— ํ’€์—ˆ๋˜ ๊ฑฐ๋ผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋ณต์Šตํ–ˆ์Šต๋‹ˆ๋‹ค.
์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ํ์— ์ •๋ ฌ๋œ ์ˆœ์„œ๊ฐ€ ์œ„์ƒ์ •๋ ฌ ์ˆœ์„œ์ž„์„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ตํ˜”๋„ค์š”.

# ์ค„์„ธ์šฐ๊ธฐ (์œ„์ƒ์ •๋ ฌ)

from collections import deque

n,e = map(int,input().split())

indegree = [0] * (n+1) # ๋ชจ๋“  ์ •์ ์˜ ์ง„์ž…์ฐจ์ˆ˜
graph = [[] for i in range(n+1)]

for _ in range(e):
  a,b = map(int,input().split())
  graph[a].append(b)
  indegree[b] += 1

def topology_sort():
  q = deque()
  result = []

  # ์ง„์ž…์ฐจ์ˆ˜ 0์ธ ๋…ธ๋“œ ํƒ์ƒ‰
  for i in range(1,n+1):
    if indegree[i] == 0: # ํ์— ๋“ฑ๋ก
      q.append(i)

  while q:
    now = q.popleft() # ์„ ์ž…์„ ์ถœ
    result.append(now)
    for node in graph[now]: # ํ•ด๋‹น ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์˜ ์ง„์ž…์ฐจ์ˆ˜์—์„œ 1 ๋นผ๊ธฐ
      indegree[node] -= 1
      if indegree[node] == 0:
        q.append(node)

  for value in result:
    print(value,end=' ')

topology_sort()

@yuyu0830 yuyu0830 merged commit dbbb3c4 into main Sep 2, 2024
@yuyu0830 yuyu0830 deleted the 24-yuyu0830 branch September 2, 2024 07:49
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.

4 participants