-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ24479.java
More file actions
67 lines (54 loc) · 1.9 KB
/
BJ24479.java
File metadata and controls
67 lines (54 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
// https://www.acmicpc.net/problem/24479
public class BJ24479{
final static int MAX = 100000 + 1;
static int N, M, R;
static ArrayList<Integer>[] graphs;
static boolean[] visitInformationList = new boolean[MAX];
static int[] answersList = new int[MAX];
static int orderNumber = 1;
public void dfs(int R) {
visitInformationList[R] = true;
answersList[R] = orderNumber++;
for (int i = 0; i < graphs[R].size() ; i++) {
int nextR = graphs[R].get(i);
if (!visitInformationList[nextR])
dfs(nextR);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
graphs = new ArrayList[MAX];
for (int i = 1; i <= N; i++) {
graphs[i] = new ArrayList<>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
graphs[u].add(v);
graphs[v].add(u);
}
for (int i = 1; i <= N ; i++) {
Collections.sort(graphs[i]);
}
BJ24479 bj24479 = new BJ24479();
bj24479.dfs(R);
for (int i = 1; i <= N ; i++) {
bw.write(String.valueOf(answersList[i]));
if (i == N) break;
bw.newLine();
}
br.close();
bw.close();
}
}