-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_1260.java
More file actions
66 lines (55 loc) · 1.43 KB
/
Copy pathB_1260.java
File metadata and controls
66 lines (55 loc) · 1.43 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
import java.util.*;
import java.io.*;
public class B_1260 {
static ArrayList<ArrayList<Integer>> G;
static int N,M,S;
static boolean[] V1,V2;
static int[]que;
static int front, rear;
static StringBuilder sb;
static void dfs(int n) {
sb.append((n+1)+" ");
V1[n]=true;
for(int e:G.get(n))
if(V1[e]==false) dfs(e);
}
static void bfs(int n) {
que[rear++]=n;
V2[n]=true;
while(front!=rear) {
int nn=que[front++];
sb.append((nn+1)+" ");
for(int e:G.get(nn))
if(V2[e]==false) {
V2[e]=true;
que[rear++]=e;
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
N=Integer.parseInt(st.nextToken());
M=Integer.parseInt(st.nextToken());
S=Integer.parseInt(st.nextToken())-1;
G=new ArrayList<ArrayList<Integer>>();
for(int i=0;i<N;i++) {
G.add(new ArrayList<Integer>());
}
for(int i=0;i<M;i++) {
st=new StringTokenizer(br.readLine());
int n1=Integer.parseInt(st.nextToken())-1;
int n2=Integer.parseInt(st.nextToken())-1;
G.get(n1).add(n2); G.get(n2).add(n1);
}
for(int i=0;i<N;i++)
Collections.sort(G.get(i));
sb=new StringBuilder();
V1=new boolean[N]; dfs(S);
System.out.println(sb.toString());
sb=new StringBuilder();
que=new int[100000];
V2=new boolean[N]; bfs(S);
System.out.println(sb.toString());
}
}