From 905a894c26e1630ef8cab623ca98ef2f7c0512d8 Mon Sep 17 00:00:00 2001 From: ByeongKeon Kim Date: Thu, 30 Jan 2025 12:27:32 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B0=B1=EC=A4=80=202252=EB=B2=88=20=EC=A4=84?= =?UTF-8?q?=20=EC=84=B8=EC=9A=B0=EA=B8=B0=20=EB=AC=B8=EC=A0=9C=20=EB=A7=81?= =?UTF-8?q?=ED=81=AC:=20https://www.acmicpc.net/problem/2252?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main.py" | 36 +++++++++++++++++++ .../test1.txt" | 3 ++ .../test1_answer.txt" | 1 + .../test2.txt" | 3 ++ .../test2_answer.txt" | 1 + .../test_main.py" | 26 ++++++++++++++ 6 files changed, 70 insertions(+) create mode 100644 "\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/main.py" create mode 100644 "\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test1.txt" create mode 100644 "\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test1_answer.txt" create mode 100644 "\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test2.txt" create mode 100644 "\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test2_answer.txt" create mode 100644 "\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test_main.py" diff --git "a/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/main.py" "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/main.py" new file mode 100644 index 0000000..eb8ffd0 --- /dev/null +++ "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/main.py" @@ -0,0 +1,36 @@ +import sys +from collections import defaultdict + + +def solve(): + input = sys.stdin.readline + N, M = map(int, input().strip().split()) + edges_fr2to = defaultdict(list) + incoming = [0 for _ in range(N + 1)] + + for _ in range(M): + fr, to = map(int, input().strip().split()) + edges_fr2to[fr].append(to) + incoming[to] += 1 + + queue = [] + for n in range(1, N + 1): + if incoming[n] == 0: + queue.append(n) + + ans = [] + while queue: + fr = queue.pop(0) + ans.append(fr) + + while edges_fr2to[fr]: + to = edges_fr2to[fr].pop(0) + incoming[to] -= 1 + if incoming[to] == 0: + queue.append(to) + + print(' '.join(map(str, ans))) + + +if __name__ == '__main__': + solve() diff --git "a/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test1.txt" "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test1.txt" new file mode 100644 index 0000000..a292fd6 --- /dev/null +++ "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test1.txt" @@ -0,0 +1,3 @@ +3 2 +1 3 +2 3 diff --git "a/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test1_answer.txt" "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test1_answer.txt" new file mode 100644 index 0000000..b85905e --- /dev/null +++ "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test1_answer.txt" @@ -0,0 +1 @@ +1 2 3 diff --git "a/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test2.txt" "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test2.txt" new file mode 100644 index 0000000..537be02 --- /dev/null +++ "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test2.txt" @@ -0,0 +1,3 @@ +4 2 +4 2 +3 1 diff --git "a/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test2_answer.txt" "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test2_answer.txt" new file mode 100644 index 0000000..f3d90b3 --- /dev/null +++ "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test2_answer.txt" @@ -0,0 +1 @@ +4 2 3 1 diff --git "a/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test_main.py" "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test_main.py" new file mode 100644 index 0000000..cf8f35e --- /dev/null +++ "b/\353\260\261\354\244\200 2252\353\262\210 \354\244\204 \354\204\270\354\232\260\352\270\260 v2/test_main.py" @@ -0,0 +1,26 @@ +import sys +from pathlib import Path +from unittest import TestCase +from main import solve + + +class Test(TestCase): + def my_solve(self, testcase_input): + sys.stdin = open(testcase_input, 'r') + stdout = sys.stdout + sys.stdout = open('stdout.txt', 'w') + solve() + sys.stdout.close() + sys.stdout = stdout + + def test_solve(self, testcase_number: str): + self.my_solve('test' + testcase_number + '.txt') + self.assertEqual( + Path('test' + testcase_number + '_answer.txt').read_text().strip(), + Path('stdout.txt').read_text().strip()) + + def test1_solve(self): + self.test_solve('1') + + def test2_solve(self): + self.test_solve('2') \ No newline at end of file