Skip to content

Conversation

@g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Aug 9, 2025

πŸ”— 문제 링크

λ¬Έμ œμ§‘

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

30m

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

μƒˆλ‘œμš΄ μœ ν˜•μ˜ 문제λ₯Ό μ°Ύκ³  싢은데 생각보닀 쉽지가 μ•Šλ„€μš”. μ˜ˆμ „μ— ν•œλ²ˆ 닀뀄본적 μžˆλŠ” μœ ν˜•μž…λ‹ˆλ‹€.

λ°©ν–₯성이 있고 사이클이 μ—†λŠ” κ·Έλž˜ν”„μ—μ„œ 정점듀을 μˆœμ„œλŒ€λ‘œ λ‚˜μ—΄ν•˜λŠ” μœ„μƒμ •λ ¬ λ¬Έμ œμž…λ‹ˆλ‹€. μ œκ°€ μ‚¬μš©ν•œ 방법은 μΉΈ μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ μ§„μž… μ°¨μˆ˜κ°€ 0인 μš”μ†Œλ“€μ„ 계속 큐에 λ„£κ³ , 큐에 넣을 λ•Œ μ§„μž… 차수λ₯Ό μ€„μ—¬μ€λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ μœ„μƒμ •λ ¬μ„ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

μ‹œκ°„ λ³΅μž‘λ„λŠ” O(V+E) 즉, λ…Έλ“œμ˜ 개수 + μ—£μ§€μ˜ κ°œμˆ˜μž…λ‹ˆλ‹€.

ꢌ였흠 κ΅μˆ˜λ‹˜μ˜ μˆ˜μ—… λ•Œ 이 방법은 단점이 μžˆμ—ˆλ˜κ±° 같은데.. ν˜Ήμ‹œ κΈ°μ–΅λ‚˜μ‹œλŠ” λΆ„μžˆλ‚˜μš”?

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

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

문제λ₯Ό 보고 μœ„μƒμ •λ ¬μ΄ λ– μ˜¬λžμŠ΅λ‹ˆλ‹€. (사싀 1달전에 ν’€μ—ˆλ˜ λ¬Έμ œλ”κ΅°μš” λ³΅μŠ΅μ‚Όμ•„ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.)
μœ„μƒμ •λ ¬μ˜ μ΄λ¦„λ§Œ 보고 무언가 정렬을 ν•˜λ € μƒκ°ν•œλ‹€λ©΄, μ•„λ§ˆ 맀우 μ–΄λ €μšΈ κ±°μ—μš”.

μœ„μƒμ •λ ¬μ˜ 핡심은 κ· ν˜Έλ‹˜μ΄ λ§μ”€ν•œλŒ€λ‘œ μš°μ„ μˆœμœ„νμ— μ§„μž…κ°„μ„ μ΄ 0인 λ…Έλ“œλ“€μ„ μ§‘μ–΄λ„£κ³ ,
κ·Έ 큐λ₯Ό μˆœνšŒν•˜λ©° κ³„μ†ν•΄μ„œ μ§„μž…κ°„μ„ μ„ ν•˜λ‚˜ 쀄이고, μ§„μž…κ°„μ„ μ΄ 0 인 λ…Έλ“œλ₯Ό μ‚½μž…ν•˜λŠ” 것이죠.

μ˜ˆμ „μ— μ œκ°€ κΈ°λ‘ν•œ pdfλ₯Ό μ‚΄νŽ΄λ³΄λ‹ˆ ꢌ였흠 κ΅μˆ˜λ‹˜κ»˜μ„œ λ§μ”€ν•˜μ‹  이 λ°©μ‹μ˜ 단점은

  1. μ§„μž…κ°„μ„ μ΄ 0인 λ…Έλ“œλ₯Ό μ–΄λ–»κ²Œ 찾을 것인가? (μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄?)
  2. μ§„μΆœ 간선을 μ–΄λ–»κ²Œ μ œκ±°ν•  것인가?

라고 ν•©λ‹ˆλ‹€.

근데 μ•„λ§ˆ 이 방식은 "μ½”λ”© ν…ŒμŠ€νŠΈ" μ—μ„œλŠ” κ³ λ €ν•˜μ§€ μ•Šμ•„λ„ 될 문제 κ°™λ„€μš”.
κ΅μˆ˜λ‹˜μ€ 벑터 ν•˜λ‚˜κ°€ μ£Όμ–΄μ‘Œμ„λ•Œ μœ„μƒμ •λ ¬μ„ λ°˜ν™˜ν•˜λŠ” "μ•Œκ³ λ¦¬μ¦˜" κ·Έ 자체λ₯Ό κ΄€μ μœΌλ‘œ 보신 것 κ°™μŠ΅λ‹ˆλ‹€.

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

using namespace std;

vector<int> tSort(vector<vector<int>>& graph, vector<int> indegree, int N) {
    priority_queue<int, vector<int>, greater<>> pq;
    for (int i = 1; i <= N; i++) {
        if (indegree[i] == 0) pq.push(i);
    }

    vector<int> result;
    while (!pq.empty()) {
        int cur = pq.top();
        pq.pop();

        result.push_back(cur);

        for (int next : graph[cur]) {
            if (--indegree[next] == 0) pq.push(next);
        }
    }

    return result;
}

int main() {
    int N, M;
    cin >> N >> M;

    vector<int> indegree(N + 1, 0);
    vector<vector<int>> graph(N + 1);
    for (int i = 0; i < M; i++) {
        int A, B;
        cin >> A >> B;
        graph[A].push_back(B);
        indegree[B]++;
    }

    vector<int> answer = tSort(graph, indegree, N);
    for (int i : answer) cout << i << " ";

    return 0;
}

Copy link
Collaborator

@kokeunho kokeunho left a comment

Choose a reason for hiding this comment

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

μœ„μƒμ •λ ¬μ΄μ—ˆκ΅°μš”...
μš°μ„ μˆœμœ„νλ₯Ό μ‚¬μš©ν•΄μ•Όν•  거라고 μƒκ°ν•˜κΈ°λŠ” ν–ˆλŠ”λ°
κ·Έ λ’€λ‘œλŠ” 풀이법이 λ– μ˜€λ₯΄μ§€ μ•Šμ•„μ„œ 풀이 μ°Έκ³ ν–ˆμŠ΅λ‹ˆλ‹€.

자꾸 μ˜€λ‹΅μ΄ λ– μ„œ λ΄€λ”λ‹ˆ μŠ΅κ΄€μ„± Scanner μ‚¬μš©μœΌλ‘œ μž…λ ₯에 μ‹œκ°„μ΄ 초과된 κ²ƒμ΄μ—ˆμŠ΅λ‹ˆλ‹€..
μ•žμœΌλ‘œ κ·Έλƒ₯ BufferReader μ¨μ•Όκ² μŠ΅λ‹ˆλ‹€.

μœ„μƒ μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜ μˆ˜μ—…μ—μ„œ λ“€μ—ˆμ—ˆλŠ”λ° κ΅¬ν˜„μ€ 기얡이 μ•ˆλ‚¬λ„€μš”
λ‹€μ‹œ ν•œλ²ˆ 볡슡 ν•  수 μžˆμ—ˆλ˜ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.
μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€~

+단점은 생각이 μ•ˆλ‚˜λŠ”λ° μœ„μƒμ •λ ¬ κ²°κ³Όκ°€ μ—¬λŸ¬κ°œκ°€ λ‚˜μ˜¬ 수 μžˆμ§€ μ•Šλ‚˜μš”?

Details

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static int N, M;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        List<Integer>[] graph = new ArrayList[N+1];
        for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>();

        int[] indegree = new int[N+1];
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            int A = Integer.parseInt(st.nextToken());
            int B = Integer.parseInt(st.nextToken());
            graph[A].add(B);
            indegree[B]++;
        }
        br.close();

        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int i = 1; i <= N; i++) {
            if (indegree[i] == 0) {
                pq.offer(i);
            }
        }

        StringBuilder sb = new StringBuilder();
        while (!pq.isEmpty()) {
            int current = pq.poll();
            sb.append(current).append(" ");
            for (int next : graph[current]) {
                indegree[next]--;
                if (indegree[next] == 0) {
                    pq.offer(next);
                }
            }
        }

        System.out.print(sb.toString().trim());
    }
}

Copy link
Member

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

일반적으둜 μœ„μƒ 정렬은 DFSλ₯Ό μ΄μš©ν•΄μ„œλ„ ν’€ 수 μžˆμ§€λ§Œ, 이 λ¬Έμ œλŠ” 쑰건 λ•Œλ¬Έμ— DFSλ‘œλŠ” 해결이 μ•ˆλ˜μ£ ... κ·Έλž˜μ„œ μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•œ λ°©μ‹μœΌλ‘œ 밖에 ν’€ 수 μ—†κ²Œ 되죠.

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N, M; cin >> N >> M;
    vector<vector<int>> graph(N + 1);
    vector<int> indegrees(N + 1, 0);
    
    while(M--) {
        int A, B; cin >> A >> B;
        graph[A].emplace_back(B);
        indegrees[B]++;
    }
    
    priority_queue<int, vector<int>, greater<>> pq;
    for(int i = 1; i <= N; ++i) {
        if(indegrees[i] == 0) {
            pq.emplace(i);
        }
    }
    
    vector<int> answer;
    while(N--) {
        if(pq.empty()) {
            break;
        }
        
        int u = pq.top();
        pq.pop();
        answer.emplace_back(u);
        
        for(int v : graph[u]) {
            if(--indegrees[v] == 0) {
                pq.emplace(v);
            }
        }
    }
    
    for(int i : answer) {
        cout << i << " ";
    }

    return 0;
}

23년도 1학기인가 ꡐ내 μ½”ν…Œμ—μ„œ μœ„μƒμ •λ ¬ λ‚˜μ˜¨ κ±° 뚝배기 κΉ¨μ Έμ„œ ν•œμ°½ μœ„μƒμ •λ ¬ 많이 ν’€μ—ˆμ—ˆλŠ”λ° ν•œλ™μ•ˆ μ•ˆν–ˆλ‹€κ³  또 κ°€λ¬Όκ°€λ¬Ό...

@g0rnn
Copy link
Collaborator Author

g0rnn commented Aug 13, 2025

+단점은 생각이 μ•ˆλ‚˜λŠ”λ° μœ„μƒμ •λ ¬ κ²°κ³Όκ°€ μ—¬λŸ¬κ°œκ°€ λ‚˜μ˜¬ 수 μžˆμ§€ μ•Šλ‚˜μš”?

λ§žμŠ΅λ‹ˆλ‹€. μœ„μƒ μ •λ ¬ μžμ²΄λŠ” μ—¬λŸ¬κ°€μ§€κ°€ λ‚˜μ˜¬ 수 μžˆμœΌλ‚˜ 문제 쑰건 3에 "κ°€λŠ₯ν•˜λ©΄ μ‰¬μš΄ λ¬Έμ œλΆ€ν„° ν’€μ–΄μ•Όν•œλ‹€." κ°€ μžˆμœΌλ―€λ‘œ λ‚˜μ˜¬ 수 μžˆλŠ” 문제 μˆœμ„œλŠ” 1κ°€μ§€μž…λ‹ˆλ‹€.

@g0rnn g0rnn merged commit 435d50c into main Nov 16, 2025
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.

5 participants