-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_2606.java
More file actions
55 lines (46 loc) · 1.5 KB
/
boj_2606.java
File metadata and controls
55 lines (46 loc) · 1.5 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
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class boj_2606 {
static StringBuilder sb = new StringBuilder();
static int node,edge;
static int[][] arr; //인접 행렬로 구현
static boolean[] chk; //방문 여부
static int count=0; //결과
static Queue<Integer> q = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
node=Integer.parseInt(br.readLine());
edge=Integer.parseInt(br.readLine());
arr=new int[node+1][node+1];
chk=new boolean[node+1];
for(int i=0;i<edge;i++){ //인접행렬 입력받고 넣기
StringTokenizer st=new StringTokenizer(br.readLine());
int a=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
arr[a][b]=1;
arr[b][a]=1;
}
bfs(1);
sb.append(count);
System.out.println(sb);
}
public static void bfs(int start){
q.add(start);
chk[start]=true;
while(!q.isEmpty()){
start=q.poll();
for(int i=1;i<=node;i++){
if(arr[start][i]==1 && !chk[i]){
q.add(i);
chk[i]=true;
count++;
}
}
}
}
}