Skip to content

Commit

Permalink
백준 2252번 줄 세우기
Browse files Browse the repository at this point in the history
  • Loading branch information
skysign committed Jan 30, 2025
1 parent 4709d1b commit 905a894
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 백준 2252번 줄 세우기 v2/main.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions 백준 2252번 줄 세우기 v2/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
3 2
1 3
2 3
1 change: 1 addition & 0 deletions 백준 2252번 줄 세우기 v2/test1_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 3
3 changes: 3 additions & 0 deletions 백준 2252번 줄 세우기 v2/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
4 2
4 2
3 1
1 change: 1 addition & 0 deletions 백준 2252번 줄 세우기 v2/test2_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4 2 3 1
26 changes: 26 additions & 0 deletions 백준 2252번 줄 세우기 v2/test_main.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 905a894

Please sign in to comment.