import
java.util.*;
@SuppressWarnings
(
"unchecked"
)
class
GFG {
static
class
Pair<T, V> {
T first;
V second;
Pair() {
}
Pair(T first, V second) {
this
.first = first;
this
.second = second;
}
}
static
int
sz = (
int
) 1e5;
static
Vector<Integer>[] tree =
new
Vector[sz];
static
boolean
[] vis =
new
boolean
[sz];
static
Vector<Integer>[] path =
new
Vector[sz];
static
void
addEdge(
int
a,
int
b) {
tree[a].add(b);
tree[b].add(a);
}
static
void
bfs(
int
node) {
Queue<Pair<Integer, Integer>> qu =
new
LinkedList<>();
qu.add(
new
Pair<>(node, -
1
));
vis[node] =
true
;
while
(!qu.isEmpty()) {
Pair<Integer, Integer> p = qu.poll();
vis[p.first] =
true
;
for
(
int
child : tree[p.first]) {
if
(!vis[child]) {
qu.add(
new
Pair<>(child, p.first));
path[child] = (Vector<Integer>) path[p.first].clone();
path[child].add(p.first);
}
}
}
}
static
void
displayPath(
int
node) {
for
(
int
k : path[node]) {
System.out.print(k +
" "
);
}
System.out.println(node);
}
public
static
void
main(String[] args) {
for
(
int
i =
0
; i < sz; i++) {
tree[i] =
new
Vector<>();
path[i] =
new
Vector<>();
vis[i] =
false
;
}
int
n =
6
;
addEdge(
0
,
1
);
addEdge(
0
,
2
);
addEdge(
1
,
3
);
addEdge(
3
,
4
);
addEdge(
3
,
5
);
bfs(
0
);
displayPath(
2
);
displayPath(
4
);
displayPath(
5
);
}
}