From e0454ee309f483a755dc9ab8a2a58f0457f7f48c Mon Sep 17 00:00:00 2001 From: yuyu0830 Date: Wed, 14 Aug 2024 20:13:22 +0900 Subject: [PATCH 1/2] =?UTF-8?q?24-08-14=20=EC=A4=84=20=EC=84=B8=EC=9A=B0?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yuyu0830/README.md | 1 + .../2252.cpp" | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 "yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/2252.cpp" diff --git a/yuyu0830/README.md b/yuyu0830/README.md index 96f05e2..5ff78b9 100644 --- a/yuyu0830/README.md +++ b/yuyu0830/README.md @@ -23,4 +23,5 @@ | 19차시 | 2024.07.12 | 재귀 | [우수마을](https://www.acmicpc.net/problem/1949) | - | | 20차시 | 2024.07.22 | LIS | [가장 긴 증가하는 부분 수열 5](https://www.acmicpc.net/problem/14003) | - | | 21차시 | 2024.07.23 | 수학 | [1의 개수 세기](https://www.acmicpc.net/problem/9527) | - | +| 24차시 | 2024.08.14 | 위상정렬 | [줄 세우기](https://www.acmicpc.net/problem/2252) | - | --- \ No newline at end of file diff --git "a/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/2252.cpp" "b/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/2252.cpp" new file mode 100644 index 0000000..d93ee51 --- /dev/null +++ "b/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/2252.cpp" @@ -0,0 +1,54 @@ +#include +#include +#include + +#define NUM 32001 + +using namespace std; + +int n, m; +int enter[NUM] = {0, }; +bool visited[NUM] = {0, }; + +vector rule[NUM]; + +int main() { + cin >> n >> m; + + for (int i = 0; i < m; i++) { + int a, b; cin >> a >> b; + + rule[a].push_back(b); + enter[b]++; + } + + queue empty; + vector ans; + + for (int i = 1; i <= n; i++) { + if (!enter[i]) { + empty.push(i); + visited[i] = true; + } + } + + while (!empty.empty()) { + int cur = empty.front(); + empty.pop(); + + ans.push_back(cur); + + for (int i : rule[cur]) { + enter[i]--; + + if (!enter[i]) { + empty.push(i); + visited[i] = true; + } + } + } + + for (int i : ans) { + printf("%d ", i); + } +} \ No newline at end of file From 63d2e574a47e527b847b618326a532871a929d29 Mon Sep 17 00:00:00 2001 From: yuyu0830 Date: Wed, 14 Aug 2024 20:36:25 +0900 Subject: [PATCH 2/2] =?UTF-8?q?visited=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2252.cpp" | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git "a/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/2252.cpp" "b/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/2252.cpp" index d93ee51..05bb3b8 100644 --- "a/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/2252.cpp" +++ "b/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/2252.cpp" @@ -8,7 +8,6 @@ using namespace std; int n, m; int enter[NUM] = {0, }; -bool visited[NUM] = {0, }; vector rule[NUM]; @@ -26,10 +25,8 @@ int main() { vector ans; for (int i = 1; i <= n; i++) { - if (!enter[i]) { + if (!enter[i]) empty.push(i); - visited[i] = true; - } } while (!empty.empty()) { @@ -41,14 +38,11 @@ int main() { for (int i : rule[cur]) { enter[i]--; - if (!enter[i]) { + if (!enter[i]) empty.push(i); - visited[i] = true; - } } } - for (int i : ans) { + for (int i : ans) printf("%d ", i); - } } \ No newline at end of file