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

4-H0ngJu #158

Merged
merged 2 commits into from
Mar 25, 2024
Merged

4-H0ngJu #158

merged 2 commits into from
Mar 25, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Mar 13, 2024

πŸ”— 문제 링크

λ¬Έμ œμ§‘

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

1μ‹œκ°„ 30λΆ„

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

이 문제 μ—­μ‹œ μš°μ„ μˆœμœ„ 큐이기 λ•Œλ¬Έμ— νž™μœΌλ‘œ μ ‘κ·Όν•˜μ˜€μŠ΅λ‹ˆλ‹€.
그런데 μž…λ ₯ 받은 κ°’μ—μ„œ b,c,d,e보닀 λ¨Όμ € ν’€μ–΄μ•Ό ν•˜λŠ” a λ¬Έμ œκ°€ μžˆλŠ” κ²½μš°μ— λŒ€ν•΄μ„œ μ–΄λ–»κ²Œ 처리λ₯Ό ν•΄μ•Ό 할지 λͺ°λΌ 고민을 쫌 ν–ˆμŠ΅λ‹ˆλ‹€.

3λ²ˆμ •λ„ μ œμΆœν–ˆλŠ”λ° 톡과가 μ•ˆλ˜μ–΄μ„œ(μ§€κΈˆ μƒκ°ν•΄λ³΄λ‹ˆ λ°˜λ‘€κ°€ λ§Žλ„€μš” ..) κ²°κ΅­ 문제 ν•˜λ‹¨μ— μžˆλŠ” μ•Œκ³ λ¦¬μ¦˜ λΆ„λ₯˜λ₯Ό ν™•μΈν–ˆκ³  μœ„μƒμ •λ ¬μ— λŒ€ν•œ κ°œλ…μ΄ ν•„μš”ν•œ 것 κ°™μ•„ μΆ”κ°€λ‘œ κ³΅λΆ€ν•˜κ³ , μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.


μœ„μƒμ •λ ¬μ΄λž€ 'μˆœμ„œκ°€ μ •ν•΄μ ΈμžˆλŠ” μž‘μ—…'을 μ°¨λ‘€λ‘œ μˆ˜ν–‰ν•΄μ•Ό ν•  λ•Œ, μˆœμ„œλ₯Ό κ²°μ •ν•˜κΈ° μœ„ν•œ μ•Œκ³ λ¦¬μ¦˜μ΄λ‹€. μ •ν™•ν•˜κ²Œ 이 λ¬Έμ œμ™€ λ§žμ•„λ–¨μ–΄μ§€λŠ” μ•Œκ³ λ¦¬μ¦˜μ΄λΌκ³  λ³Ό 수 μžˆλ‹€.

μœ„μƒ μ •λ ¬μ—μ„œ μ‚¬μš©λ˜λŠ” κ°œλ… 2가지가 μžˆλŠ”λ°, 'μ§„μž… 차수'와 'μ§„μΆœ 차수' 이닀.

  • μ§„μž… 차수 : νŠΉμ • λ…Έλ“œλ‘œ λ“€μ–΄μ˜€λŠ” κ°„μ„ μ˜ 개수
  • μ§„μΆœ 차수 : νŠΉμ •ν•œ λ…Έλ“œλ‘œ λ‚˜κ°€λŠ” κ°„μ„ μ˜ 개수

μ•„λž˜μ˜ 사진이 μ§„μž…μ°¨μˆ˜/μ§„μΆœμ°¨μˆ˜μ˜ μ˜ˆμ΄λ‹€.
μœ„μƒμ •λ ¬ μ˜ˆμ‹œ

기본적인 μœ„μƒμ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ˜ μˆ˜λ„μ½”λ“œλŠ” μ•„λž˜μ™€ κ°™λ‹€.

1. μ§„μž…μ°¨μˆ˜κ°€ 0인 λ…Έλ“œλ₯Ό 큐에 λ„£λŠ”λ‹€.
2. 큐가 빌 λ•ŒκΉŒμ§€ λ‹€μŒμ˜ 과정을 반볡
    1. νμ—μ„œ μ›μ†Œλ₯Ό κΊΌλ‚΄ ν•΄λ‹Ή λ…Έλ“œμ—μ„œ λ‚˜κ°€λŠ” 간선을 κ·Έλž˜ν”„μ—μ„œ 제거
    2. μƒˆλ‘­κ²Œ μ§„μž… μ°¨μˆ˜κ°€ 0이 된 λ…Έλ“œλ₯Ό 큐에 λ„£λŠ”λ‹€.
=> 큐에 λ“€μ–΄μ˜¨ μˆœμ„œκ°€ μœ„μƒμ •λ ¬μ„ μˆ˜ν–‰ν•œ κ²°κ³Ό

λ˜‘κ°™μ΄ 이 λ¬Έμ œμ—λ„ μœ„μƒμ •λ ¬μ„ μ μš©ν•  수 μžˆλ‹€.

  1. λ¨Όμ €, μž…λ ₯으둜 받은 p1이 p2보닀 λ¨Όμ € ν’€μ–΄μ•Όν•˜λŠ” κ²½μš°μ— graph[p1]에 p2λ₯Ό λ„£λŠ”λ‹€.
    p1 - p2 둜 μ—°κ²°λ˜μ–΄ μžˆμœΌλ―€λ‘œ, p2의 κ°„μ„  + 1.
  2. μ§„μž… μ°¨μˆ˜κ°€ 0이면 큐(q)에 μ €μž₯
  3. 큐가 빌 λ•ŒκΉŒμ§€ λ‹€μŒμ˜ 과정을 반볡
    1. νμ—μ„œ κΊΌλ‚΄μ„œ answer에 λ„£κΈ°
    2. νμ—μ„œ κΊΌλ‚Έ λ¬Έμ œμ— λŒ€ν•˜μ—¬ 간선을 μ œκ±°ν•˜κ³ , λ‹€μ‹œ μ§„μž… μ°¨μˆ˜κ°€ 0인 것을 큐에 μ €μž₯

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

  • μœ„μƒμ •λ ¬μ€ μ‹œμž‘μ μ΄ μ‘΄μž¬ν•΄μ•Όν•œλ‹€.
  • μœ„μƒμ •λ ¬μ€ 닡이 μ—¬λŸ¬ κ°œκ°€ μ‘΄μž¬ν•  수 μžˆλ‹€.
  • DAG(Directed Acyclic Graph)μ—λ§Œ 적용 κ°€λŠ₯ -> 사이클이 μ—†μ–΄μ•Ό ν•œλ‹€. 있으면 λ¬΄ν•œνžˆ λ°˜λ³΅ν•˜κ²Œ 되기 λ•Œλ¬Έ

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

이 문제 쉽지 μ•Šμ€λ°..

κ³¨λ“œ2 문제λ₯Ό λ°”λ‘œ λ„μ „ν•˜μ…¨κ΅°μš”..

μœ„μƒ 정렬을 λͺ¨λ₯΄μ…¨λ”라도 μš°μ„ μˆœμœ„ 큐λ₯Ό μΊμΉ˜ν•˜μ‹  것이 λŒ€λ‹¨ν•˜μ‹œλ„€μš”.

이 λ¬Έμ œλŠ” μ €λ²ˆμ— PR둜 올렸던 적이 μžˆμ–΄μ„œ 링크둜 λŒ€μ‹ ν• κ²Œμš”!

#71 (comment)

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€~~~~~πŸ‘πŸ‘

n, m = map(int, sys.stdin.readline().rstrip().split())

graph = [[] for _ in range(n+1)]
inDegree = [0 for _ in range(n+1)]
Copy link
Member

Choose a reason for hiding this comment

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

μ‚¬μ†Œν•œ κ±°κΈ΄ ν•œλ°, νŒŒμ΄μ¬μ€ κ³΅μ‹μ μœΌλ‘œ snake 기법을 μ»¨λ²€μ…˜μœΌλ‘œ μ±„νƒν•˜κ³ μžˆμ–΄μš”!

Copy link
Member

Choose a reason for hiding this comment

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

μ™œμ£ ?


# μž…λ ₯λ°›μ•„μ„œ λ„£κΈ°
for _ in range(m):
p1, p2 = map(int, sys.stdin.readline().rstrip().split())
Copy link
Member

@tgyuuAn tgyuuAn Mar 13, 2024

Choose a reason for hiding this comment

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

이 뢀뢄도 λ―Έμ„Έ νŒμ΄κΈ΄ν•œλ°

def input(): return sys.stdin.readline().rstrip()

으둜 μ„ μ–Έν•΄λ†“μœΌλ©΄

p1, p2 = map(int, input().split())

으둜 μ‚¬μš©ν•  수 μžˆμ–΄μš”!

(dx, dy κΈ‰ λ―Έμ„Έ κΏ€νŒμž…λ‹ˆλ‹€.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

흐흐 λ°”μ•„λ‘œ 이번 λ¬Έμ œμ— μ μš©ν–ˆμŠ΅λ‹ˆλ‹€πŸ˜†πŸ˜Ž

@pknujsp
Copy link
Collaborator

pknujsp commented Mar 17, 2024

μ˜ˆμ „μ— μ΅œμ†Œ νž™μ„ 잘 λͺ°λΌμ„œ νž™μœΌλ‘œ μ•ˆ ν’€κ³  μ •λ ¬, 덱 λ“±λ“± μ˜¨κ°– λ°©λ²•μœΌλ‘œ μ‹œλ„ν•΄μ„œ
ν‘ΈλŠ”λ° ν•œμ°Έ 걸렸던 기얡이 λ‚˜λŠ” λ¬Έμ œμ΄κ΅°μš”.

μ €λŠ” μ•„μ˜ˆ νž™μ„ λ– μ˜¬λ¦¬μ§€λ„ λͺ»ν–ˆμ—ˆλŠ”데, λ– μ˜¬λ €μ„œ μ ‘κ·Όν•˜μ‹ κ²Œ λŒ€λ‹¨ν•˜μ„Έμš”.

λ³΄λ‹ˆκΉŒ 제 풀이와 거의 κ°™κ΅°μš”.
λ‹¨μˆœνžˆ μ§„μž…μ°¨μˆ˜κ°€ 0인 λ¬Έμ œλ“€μ„ νž™μ— λ„£μ–΄μ„œ μˆœνšŒν•˜λ©° 문제 풀이 μˆœμ„œλ₯Ό μ°ΎλŠ”κ±°λ‹€λ³΄λ‹ˆ
크게 λ‹€λ₯Έ ν’€μ΄λŠ” 없을 것 κ°™μŠ΅λ‹ˆλ‹€.

N, M = map(int, stdin.readline().split())

graph = [[] for _ in range(N + 1)]
indegrees = [0] * (N + 1)

for _ in range(M):
    a, b = map(int, stdin.readline().split())
    indegrees[b] += 1
    graph[a].append(b)

heap = []
for p in range(1, N + 1):
    if not indegrees[p]:
        heappush(heap, p)

while heap:
    p1 = heappop(heap)
    print(p1, end=' ')
    
    for p2 in graph[p1]:
        if indegrees[p2] < 0:
            continue
            
        indegrees[p2] -= 1
        if not indegrees[p2]:
            heappush(heap, p2)

Comment on lines +1 to +2
import sys
import heapq
Copy link
Collaborator

Choose a reason for hiding this comment

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

from heapq import *

λͺ¨λ“ˆ μž„ν¬νŠΈ ν•  λ•Œ μœ„μ²˜λŸΌ ν•˜λ©΄ μ’€ 더 νŽΈν•˜κ²Œ μ½”λ”©ν•  수 μžˆλ‹΅λ‹ˆλ‹€
λ”°λ‘œ heapqλ₯Ό μ•ˆ μ¨μ€˜λ„ λΌμš”

heappush(heap, value)

@Munbin-Lee
Copy link
Member

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> sufficient(n + 1), necessary(n + 1);
  vector<bool> isSolved(n + 1);

  while (m--) {
    int a, b;
    cin >> a >> b;
    sufficient[a].emplace_back(b);
    necessary[b].emplace_back(a);
  }

  priority_queue<int, vector<int>, greater<>> pq;
  for (int i = 1; i <= n; i++) {
    if (!necessary[i].empty()) continue;
    pq.emplace(i);
  }

  while (!pq.empty()) {
    int cur = pq.top();
    pq.pop();
    isSolved[cur] = true;
    cout << cur << ' ';
    for (int i: sufficient[cur]) {
      bool isAvailable = true;
      for (int j: necessary[i]) {
        if (isSolved[j]) continue;
        isAvailable = false;
        break;
      }
      if (!isAvailable) continue;
      pq.emplace(i);
    }
  }

  return 0;
}

μœ„μƒμ •λ ¬μ„ μ‚¬μš©ν•œ ν’€μ΄λŠ” ν™μ£Όλ‹˜κ³Ό λΉ„μŠ·ν•΄μ„œ μœ„μƒμ •λ ¬μ„ λͺ¨λ₯΄λ˜ μ‹œκΈ°μ— ν‘Ό μ½”λ“œλ₯Ό μ˜¬λ €λ΄€μ–΄μš”.

μš°μ„ μˆœμœ„ 큐 λ°”λ‘œ μ²΄ν¬ν•œ κ±° κ΅Ώμž…λ‹ˆλ‹€.

@H0ngJu H0ngJu merged commit bc817de into main Mar 25, 2024
@H0ngJu H0ngJu deleted the 4-H0ngJu branch March 25, 2024 14:40
@H0ngJu H0ngJu mentioned this pull request May 9, 2024
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