https://www.acmicpc.net/problem/2252
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Boj_2252 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int M = scan.nextInt();
int[] inDegree = new int[N+1];
ArrayList<Integer>[] list = new ArrayList[N+1];
for(int i=1; i<N+1; i++) {
list[i] = new ArrayList<Integer>();
}
for(int i=0; i<M; i++) {
int one = scan.nextInt();
int two = scan.nextInt();
list[one].add(two);
inDegree[two]++;
}
// Topological Sorting
Queue<Integer> q = new LinkedList<Integer>();
for(int i=1; i<N+1; i++) {
if(inDegree[i]==0) {
q.add(i);
}
}
while(!q.isEmpty()) {
System.out.print(q.peek()+" ");
int now = q.poll();
for(int i=0; i<list[now].size(); i++) {
int next = list[now].get(i);
inDegree[next]--;
if(inDegree[next]==0) {
q.add(next);
}
}
}
}
}
'언어 > JAVA' 카테고리의 다른 글
JAVA 최대공약수 구하기 (0) | 2021.07.23 |
---|---|
JAVA 입력 (0) | 2021.07.20 |
Constructor 생성자 (0) | 2021.05.29 |
Method Overloading (0) | 2021.05.29 |
Setters, Getters (0) | 2021.05.26 |
댓글