[BOJ] 2252번 : 줄 세우기

2023. 8. 9. 21:05·Problem Solving/Baekjoon

https://www.acmicpc.net/problem/2252

 

2252번: 줄 세우기

첫째 줄에 N(1 ≤ N ≤ 32,000), M(1 ≤ M ≤ 100,000)이 주어진다. M은 키를 비교한 회수이다. 다음 M개의 줄에는 키를 비교한 두 학생의 번호 A, B가 주어진다. 이는 학생 A가 학생 B의 앞에 서야 한다는 의

www.acmicpc.net

 

 

 

알고리즘 : 위상 정렬

import itertools
import sys, heapq
si = sys.stdin.readline
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from math import factorial,sqrt
from itertools import permutations, combinations, accumulate
mn, mx = float('inf'), float('-inf')

def topology_sort() :
    res=[]
    q = deque([])
    for i in range(1,n+1) :
        if indegree[i] == 0 :
            q.append(i)
    while q :
        cur = q.popleft()
        res.append(cur)
        for i in graph[cur] :
            indegree[i] -= 1
            if indegree[i] == 0 :
                q.append(i)

    for i in res :
        print(i, end=' ')

n,m=map(int,si().split())
indegree=[0 for _ in range(n+1)]
graph=[[] for _ in range(n+1)]
for _ in range(m) :
    a,b=map(int,si().split())
    graph[a].append(b)
    indegree[b] += 1
topology_sort()
'Problem Solving/Baekjoon' 카테고리의 다른 글
  • [BOJ] 1991번: 트리 순회
  • [BOJ] 4485번 : 녹색 옷 입은 애가 젤다지?
  • [BOJ] 3085번: 사탕 게임
  • [BOJ] 1476번: 날짜 계산
kimdozzi
kimdozzi
끝까지 포기하지 않으면, 내가 다 이겨!
  • kimdozzi
    도브로
    kimdozzi
  • 전체
    오늘
    어제
    • 분류 전체보기 (132)
      • Problem Solving (49)
        • Baekjoon (29)
        • Programmers (0)
        • LeetCode (17)
        • 삼성 유형 (2)
      • Computer Science (27)
        • Operating System (2)
        • Algorithms (13)
        • Network (6)
        • DataBase (6)
      • Backend (33)
        • JavaScript (0)
        • TypeScript (6)
        • Java (7)
        • Spring Boot (7)
        • Spring Security (6)
        • JPA (2)
        • Mybatis (1)
        • Junit5 (1)
        • Redis (3)
      • DevOps (14)
        • Git, Github (5)
        • docker (4)
        • AWS (3)
        • nginx (2)
      • etc (6)
        • IntelliJ (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 티스토리
    • 설정
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    interface
    누적합
    PrefixSum
    오프라인 쿼리
    온라인 쿼리
    점 업데이트
    알고리즘
    삼성기출
    컨테이너
    도커
    백준
    티스토리챌린지
    S3
    구간합
    파이썬
    python
    segment tree
    인덱스 시그니처
    CORS
    인덱서블 타입
    Bucket
    세그먼트 트리
    docker image
    오블완
    구간 업데이트
    imos법
    인터페이스
    AWS
    TypeScript
    타입스크립트
    docker
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
kimdozzi
[BOJ] 2252번 : 줄 세우기
상단으로

티스토리툴바