728x90
반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Main {
static int N,M,R,idx;
static ArrayList<Integer>[] edges;
static int [] answer;
static boolean [] visited;
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());
R = Integer.parseInt(st.nextToken());
edges = new ArrayList[N+1];
for (int i = 1; i <=N; i++) {
edges[i] = new ArrayList<>();
}
answer = new int [N+1];
visited = new boolean[N+1];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine()," ");
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
edges[u].add(v);
edges[v].add(u);
}
for (int i = 1; i <= N ; i++) {
Collections.sort(edges[i]);
}
visited[R] = true;
dfs(R);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= N ; i++) {
sb.append(answer[i]).append('\n');
}
System.out.println(sb);
}
private static void dfs(int cur) {
answer[cur] = ++idx;
ArrayList<Integer> edge = edges[cur];
for(int next : edge){
if(visited[next]) continue;
visited[next] = true;
dfs(next);
}
}
}
728x90
반응형
'알고리즘 > DFS, BFS, 시뮬, 백트래킹' 카테고리의 다른 글
[백준] 7562번 나이트의 이동 #Java (0) | 2022.05.27 |
---|---|
[백준] 2578번 빙고 #Java (0) | 2022.05.25 |
[백준] 2630번 색종이 만들기 (0) | 2022.05.17 |
[알고리즘] 미로탐색(DFS) with Java, 초기화 전략 (0) | 2022.04.22 |
[프로그래머스] N-Queen 문제 (0) | 2021.11.09 |