반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 프로그래머스
- 자바
- java
- 경제
- 코테
- 손에 잡히는 경제
- 경제뉴스
- 급등주
- 백준
- 급등주 분석
- 이진우의 손에 잡히는 경제
- Programmers
- 상한가 분석
- 주식 분석
- 상한가
- 테마주
- 파이썬
- 경제뉴스 요약
- 알고리즘
- 급등 이유
- Python
- 주식
- 코딩테스트
- boj
- 손경제 요약
- 이진우
- 상한가 이유
- 손경제
- 주식 상한가
- 손에 잡히는 경제 요약
Archives
- Today
- Total
Completion over Perfection
백준 2252 - 줄 세우기 (JAVA 자바) 본문
반응형
백준 2252 - 줄 세우기 (JAVA 자바)
2252번: 줄 세우기
첫째 줄에 N(1≤N≤32,000), M(1≤M≤100,000)이 주어진다. M은 키를 비교한 회수이다. 다음 M개의 줄에는 키를 비교한 두 학생의 번호 A, B가 주어진다. 이는 학생 A가 학생 B의 앞에 서야 한다는 의미이
www.acmicpc.net
문제집과 굉장히 유사한 문제입니다.
문제집도 같이 풀어보시면 좋을 것 같습니다.
PriorityQueue를 이용해서 풀면 됩니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import java.util.*;
public class Main {
static int n,m;
static boolean visits[];
static int indegree[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
indegree = new int [n+1];
visits = new boolean[n+1];
ArrayList <Integer> list [] = new ArrayList[n+1];
for(int i=0; i<=n; i++) {
list[i] = new ArrayList<Integer>();
}
for(int i=1; i<=m; i++) {
int tmp1 = sc.nextInt(); // 1 2
int tmp2 = sc.nextInt(); // 3 3
list[tmp1].add(tmp2);
indegree[tmp2]++;
}
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
for(int i=1; i<=n; i++) {
if (indegree[i]==0) {
q.offer(i); // 1, 2가 들어감
}
}
while(!q.isEmpty()) {
int out = q.poll(); // 1, 2가 나옴
System.out.print(out + " ");
for(int i=0; i<list[out].size(); i++) {
int next = list[out].get(i);
// System.out.println("next값은 : " + next);
indegree[next]--;
if (indegree[next]==0) {
q.offer(next);
}
}
}
}
}
|
cs |
반응형
'앨고리듬 알고리즘' 카테고리의 다른 글
백준 10026 - 적록색약 (JAVA 자바) (0) | 2021.01.28 |
---|---|
백준 2468 - 안전 영역 (JAVA 자바) (0) | 2021.01.28 |
백준 2075 - N번째 큰 수 (JAVA 자바) (0) | 2020.11.30 |
백준 4963 - 섬의 개수 (JAVA 자바) (0) | 2020.11.25 |
백준 11724 - 연결 요소의 개수 (JAVA 자바) (0) | 2020.11.24 |
Comments