acmicpc
[BAEKJOON] 2458. 키 순서
코드와이
2021. 9. 5. 23:05
문제링크
https://www.acmicpc.net/problem/2458
2458번: 키 순서
1번부터 N번까지 번호가 붙여져 있는 학생들에 대하여 두 학생끼리 키를 비교한 결과의 일부가 주어져 있다. 단, N명의 학생들의 키는 모두 다르다고 가정한다. 예를 들어, 6명의 학생들에 대하여
www.acmicpc.net
package acmicpc.Gold4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class 키순서 {
static int n, m, map[][], ans;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new int[n+1][n+1];
int a, b;
for(int i = 0 ; i < m ; i++) {
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
map[a][b] = 1;
}
ans = 0;
for(int i = 1 ; i <= n ; i++) {
if(checkTall(i)) {
ans++;
}
}
System.out.println(ans);
}
public static boolean checkTall(int x) {
Queue<Integer> queue = new LinkedList<>();
boolean[] check = new boolean[n + 1];
queue.add(x);
check[x] = true;
int cnt = 1;
while(!queue.isEmpty()) {
int q = queue.poll();
for(int i = 1 ; i <= n ; i++) {
if(!check[i] && map[q][i] == 1) {
queue.add(i);
check[i] = true;
cnt++;
}
}
}
queue.clear();
queue.add(x);
while(!queue.isEmpty()) {
int q = queue.poll();
for(int i = 1 ; i <= n ; i++) {
if(!check[i] && map[i][q] == 1) {
queue.add(i);
check[i] = true;
cnt++;
}
}
}
return cnt == n ? true : false;
}
}