-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1260.java
More file actions
70 lines (55 loc) · 1.15 KB
/
P1260.java
File metadata and controls
70 lines (55 loc) · 1.15 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
68
69
70
package BOJ;
import java.util.*;
public class P1260 {
static ArrayList<Integer>[] a;
static int[] check;
public static void dfs(int start){
if(check[start]==1)
return;
check[start] = 1;
System.out.print(start+" ");
for(int x : a[start]){
if(check[x]==0)
dfs(x);
}
}
public static void bfs(int start){
Queue<Integer> q = new LinkedList<Integer>();
q.add(start);
check[start] = 1;
while(!q.isEmpty()){
int x = q.remove();
System.out.print(x+" ");
for(int y : a[x]){
if(check[y]==0){
check[y] = 1;
q.add(y);
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int edge = sc.nextInt();
int line = sc.nextInt();
int start = sc.nextInt();
a = (ArrayList<Integer>[]) new ArrayList[edge+1];
for(int i=1;i<=edge;i++){
a[i] = new ArrayList<Integer>();
}
for(int i=0;i<line;i++){
int u = sc.nextInt();
int v = sc.nextInt();
a[u].add(v);
a[v].add(u);
}
for(int i=1;i<=edge;i++){
Collections.sort(a[i]);
}
check = new int[edge+1];
dfs(start);
System.out.println();
check = new int[edge+1];
bfs(start);
}
}