diff --git a/bomik0221/README.md b/bomik0221/README.md index 60e4ef8..2b3565a 100644 --- a/bomik0221/README.md +++ b/bomik0221/README.md @@ -15,5 +15,6 @@ | 11차시 | 2024.01.31 | 재귀 | [재귀함수가 뭔가요?](https://www.acmicpc.net/problem/17478) | [#45](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/45) | | 12차시 | 2024.02.03 | 사칙연산 | [저작권](https://www.acmicpc.net/problem/2914) | [#49](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/49) | | 13차시 | 2024.02.06 | DFS | [유기농 배추](https://www.acmicpc.net/problem/1012) | [#51](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/51) | +| 14차시 | 2024.02.15 | 위상정렬 | [줄 세우기](https://www.acmicpc.net/problem/2252) | [#57](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/57) | --- diff --git "a/bomik0221/\354\234\204\354\203\201\354\240\225\353\240\254/240215.cpp" "b/bomik0221/\354\234\204\354\203\201\354\240\225\353\240\254/240215.cpp" new file mode 100644 index 0000000..b86e8d6 --- /dev/null +++ "b/bomik0221/\354\234\204\354\203\201\354\240\225\353\240\254/240215.cpp" @@ -0,0 +1,41 @@ +#include +#include +#include + +int main() { + int N, M; + std::cin >> N >> M; + + std::vector> graph(N + 1, std::vector(0, 0)); + std::vector indegree(N + 1, 0); + std::vector sorted; + + for (int i = 0; i < M; i++) { + int a, b; + std::cin >> a >> b; + graph[a].push_back(b); + indegree[b]++; + } + + std::queue q; + for (int i = 1; i < N + 1; i++) { + if (indegree[i] == 0) q.push(i); + } + + while (!q.empty()) { + int temp = q.front(); + sorted.push_back(temp); + q.pop(); + for (int i = 0; i < graph[temp].size(); i++) { + indegree[graph[temp][i]]--; + if (indegree[graph[temp][i]] == 0) { + q.push(graph[temp][i]); + } + } + } + + for (int i = 0; i < sorted.size(); i++) { + std::cout << sorted[i] << " "; + } + return 0; +} \ No newline at end of file