-
[KOITP] 그래프 순회자료구조 및 알고리즘/문제풀이 2017. 1. 16. 07:55
출 처 : http://koitp.org/problem/SDS_PRO_10_1/read/
시간 제한 메모리 제한 제출 횟수 정답 횟수 (비율) 정답자 수 1.0 초 512 MB 1474 222 (15%) 182 < Comment >
딱히 코드상에 바뀐거라고는 Dequeue를 Queue 로 바꾼 것과, Comparator를 재정의 해 준 것 정도뿐인데, 답이 나오니 조금 허무하기도 했습니다. 문제 자체는 그저 DFS와 BFS를 제대로 코드로 구현할 수 있는가 정도를 보는 것입니다. 막상 구현하려고 하다보면 헷갈리기도 하니 의사코드 정도는 암기해 두어도 좋을 것 같습니다.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class GraphSearch { public static int V = 0; public static int E = 0; public static int S = 0; public static int count = 0; public static ArrayList<Integer>[] adjust = null; public static boolean isVisited[] = null; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); V = Integer.parseInt(st.nextToken()); E = Integer.parseInt(st.nextToken()); S = Integer.parseInt(st.nextToken()); adjust = new ArrayList[V + 1]; isVisited = new boolean[V + 1]; for (int i = 1 ; i <= V ; i++) { adjust[i] = new ArrayList<>(); } int node1 = 0; int node2 = 0; for (int i = 1 ; i <= E ; i++) { st = new StringTokenizer(br.readLine()); node1 = Integer.parseInt(st.nextToken()); node2 = Integer.parseInt(st.nextToken()); adjust[node1].add(node2); adjust[node2].add(node1); } for (int i = 1 ; i <= V ; i++) { Collections.sort(adjust[i], new Comparator<Integer>() { @Override public int compare(Integer arg0, Integer arg1) { if(arg0 > arg1) return 1; if(arg0 < arg1) return -1; return 0; } }); } DFS(S); System.out.println(); Arrays.fill(isVisited, false); count = 0; BFS(S); } public static void DFS(int node) { if (isVisited[node]) return; isVisited[node] = true; System.out.print(node + " "); for (int to : adjust[node]) { if (isVisited[to]) continue; DFS(to); } } public static void BFS(int start) { Queue<Integer> q = new ArrayDeque<>(); q.add(start); while(!q.isEmpty()) { int node = q.poll(); if (isVisited[node]) continue; isVisited[node] = true; System.out.print(node + " "); for (int to : adjust[node]) { if(isVisited[to]) continue; q.add(to); } } } }