-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.java
More file actions
50 lines (40 loc) · 1.45 KB
/
Copy pathA.java
File metadata and controls
50 lines (40 loc) · 1.45 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
import java.util.*;
import java.io.*;
public class A {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out= new PrintWriter(System.out);
StringTokenizer st=new StringTokenizer(br.readLine(), " ");
int nodes= Integer.parseInt(st.nextToken());
int edges= Integer.parseInt(st.nextToken());
ArrayList<Integer>[] graph= new ArrayList[nodes + 1]; //[] graph- is an array of Arraylist type
//arraylist initialize kora
for(int i=1; i<=nodes ; i++){
graph[i]= new ArrayList<>();
}
//Fill up the array
for(int i=0; i<edges; i++){
StringTokenizer st2= new StringTokenizer(br.readLine(), " ");
int u= Integer.parseInt(st2.nextToken());
int v= Integer.parseInt(st2.nextToken());
graph[u].add(v);
graph[v].add(u);
}
//BFS
boolean[] visited= new boolean[nodes+1];
Queue<Integer> q= new LinkedList<>();
visited[1]= true;
q.add(1);
while(!q.isEmpty()){
int u=q.poll();
out.print(u+ " ");
for(int v:graph[u]){ //ArrayList r prothom tay giye boshbe
if(!visited[v]){
visited[v]= true;
q.add(v);
}
}
}
out.flush();
}
}