2252번: 줄 세우기
첫째 줄에 N(1 ≤ N ≤ 32,000), M(1 ≤ M ≤ 100,000)이 주어진다. M은 키를 비교한 회수이다. 다음 M개의 줄에는 키를 비교한 두 학생의 번호 A, B가 주어진다. 이는 학생 A가 학생 B의 앞에 서야 한다는 의
www.acmicpc.net
위상 정렬 문제다.
사이클이 없는 방향 그래프의 모든 노드들을 순서에 맞춰 정렬하는 알고리즘이다.
노드에 들어오는 간선의 개수인 진입차수가 0인 노드들은 수행할 수 있는 상태의 노드들이므로 큐에 넣는다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int n;
static int m;
static List<Integer>[] graph;
static StringBuilder sb;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
sb = new StringBuilder();
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
graph = new List[n + 1];
// 진입 차수가 0이면 큐에 삽입한다.
int[] degree = new int[n + 1];
for (int i = 1; i <= n; i++) graph[i] = new ArrayList<>();
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int p = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
// p에서 c로 가는 간선을 연결해준다.
graph[p].add(c);
degree[c]++;
}
Queue<Integer> queue = new ArrayDeque<>();
// 초기 상태에서 진입차수가 0인 노드를 모두 큐에 넣는다.
for (int i = 1; i <= n; i++) {
if (degree[i] == 0) queue.offer(i);
}
while (!queue.isEmpty()) {
int cur = queue.poll();
sb.append(cur).append(' ');
// current node와 연결된 노드의 간선을 제거한다.
for (int next : graph[cur]) {
degree[next]--;
// next로 진입하는 길이 없다면 큐에 집어넣는다.
// next를 수강해도 좋다는 뜻.
if (degree[next] == 0) queue.offer(next);
}
}
System.out.println(sb);
}
}
'BOJ' 카테고리의 다른 글
[BOJ] [JAVA] 31423번 신촌 통폐합 계획 (0) | 2024.02.21 |
---|---|
[BOJ] [JAVA] 1238번 파티 (1) | 2024.02.16 |
[BOJ] [JAVA] 1753번 최단경로 (1) | 2024.02.15 |
[BOJ] [JAVA] 11003번 최소값 찾기 (1) | 2024.02.10 |
[BOJ] [JAVA] 1167번 트리의 지름 (1) | 2024.02.07 |
[BOJ] [JAVA] 14442번 벽 부수고 이동하기 2 (1) | 2024.01.27 |
[BOJ] [JAVA] 1012번 유기농 배추 (0) | 2024.01.20 |
[BOJ] [JAVA] 4779번 칸토어 집합 (1) | 2024.01.14 |