-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.java
More file actions
43 lines (38 loc) · 1.06 KB
/
1260.java
File metadata and controls
43 lines (38 loc) · 1.06 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
import java.util.*;
import java.io.*;
public class Main {
static int[][] connect=new int[1001][1001];
static int[] visited_dfs = new int[1001];
static int[] visited_bfs = new int[1001];
static int n,m,v;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); m = sc.nextInt(); v = sc.nextInt();
for(int i =0;i<m;i++) {
int s = sc.nextInt(); int e = sc.nextInt();
connect[s][e] = 1; connect[e][s]=1;
}
dfs(v);System.out.println();
bfs(v);
}
static void dfs(int i) {
visited_dfs[i]=1; System.out.print(i+" ");
for(int k=1;k<=n;k++) {
if(connect[i][k]==1 && visited_dfs[k]==0) {
dfs(k);
}
}
}
static void bfs(int i) {
Queue<Integer> q = new LinkedList<>();
q.add(i);visited_bfs[i]=1;System.out.print(i+" ");
while(!q.isEmpty()) {
int l = q.poll();
for(int k=1;k<=n;k++) {
if(connect[l][k]==1 && visited_bfs[k]==0) {
visited_bfs[k]=1;q.add(k);System.out.print(k+" ");
}
}
}
}
}