Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved 부대복귀 - 131.21ms 198mb #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Programmers/부대복귀/부대복귀_김훈민.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.*;

class Solution {
static int[] visited;
static ArrayList<Integer>[] graph;
static int dest;
static Queue<Integer> queue = new LinkedList<>();
public int[] solution(int n, int[][] roads, int[] sources, int destination) {
visited = new int[n+1];
Arrays.fill(visited, Integer.MAX_VALUE);
graph = new ArrayList[n+1];
for(int i = 1; i < n+1; i++){
graph[i] = new ArrayList();
}
for(int[] road : roads){
graph[road[0]].add(road[1]);
graph[road[1]].add(road[0]);
}
bfs(destination);
int[] answer = new int[sources.length];
int cnt = 0;
for(int source : sources){
if(visited[source] == Integer.MAX_VALUE) answer[cnt++] = -1;
else answer[cnt++] = visited[source];
}
return answer;
}

static void bfs(int start){
queue.add(start);
visited[start] = 0;
int size = queue.size();
int time = 1;
while(!queue.isEmpty()){
for(int i = 0; i < size; i++){
int cur = queue.poll();
for(int next : graph[cur]){
if(visited[next] != Integer.MAX_VALUE) continue;
visited[next] = time;
queue.add(next);
}
}
size = queue.size();
time++;
}
}
}