SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
문제는 페이지에서 확인할 수 있습니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
/*
사용하는 클래스명이 Solution 이어야 하므로, 가급적 Solution.java 를 사용할 것을 권장합니다.
이러한 상황에서도 동일하게 java Solution 명령으로 프로그램을 수행해볼 수 있습니다.
*/
class Solution
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static Queue<Integer> q;
static Queue<Integer> p;
static int num = 0;
public static void main(String[] args) throws Exception{
for (int tc = 1; tc <= 10; tc++) {
q = new LinkedList<Integer>();
p = new LinkedList<Integer>();
st = new StringTokenizer(in.readLine(), " "); // 첫번째줄 입력받음
int count = Integer.parseInt(st.nextToken());
int start_point = Integer.parseInt(st.nextToken());
st = new StringTokenizer(in.readLine(), " "); // 데이터 입력 받음
int map[][] = new int[101][101];
boolean isSeleceted[] = new boolean[101];
for (int i = 0; i < count/2; i++) {
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
map[from][to] = 1;
}
q.offer(start_point);
isSeleceted[start_point] = true;
LinkedList<Integer> tmp_a;
while(true) {
tmp_a = new LinkedList<>();
for (int k : q) {
tmp_a.offer(k);
}
while(!q.isEmpty()) {
int tmp = q.poll();
for (int i = 1; i < map[tmp].length; i++) {
if(map[tmp][i] == 1 && !isSeleceted[i]) {
isSeleceted[i] = true;
p.offer(i);
}
}
}
if(p.isEmpty()) break;
q = p;
p = new LinkedList<>();
num++;
}
Collections.sort(tmp_a, Collections.reverseOrder());
System.out.println("#"+tc+" "+tmp_a.peek());
}
}
}
'Algorithms > SW expert' 카테고리의 다른 글
[Java] 1228. [S/W 문제해결 기본] 8일차 - 암호문1 (0) | 2021.03.24 |
---|---|
[Java] 1204. [S/W문제해결 기본] 1일차 - 최빈수 구하기 (0) | 2021.03.24 |
[Java] 3289. 서로소 만들기 (0) | 2021.03.18 |
[Java] 1247. 최적 경로 (0) | 2021.02.20 |
[Java] 1873. 상호의 배틀필드 (0) | 2021.02.04 |