- DFS, BFS 기초 문제
- 노드를 방문할때마다 answer를 1 증가시키고, 탐색을 마치면 answer를 출력한다.
public class Main {
static int n,k,answer=0;
static boolean[] ch;
static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
public static void bfs(){
Queue<Integer> q = new LinkedList<>();
ch[1] = true;
q.offer(1);
while(!q.isEmpty()){
int v = q.poll();
for (int nv : graph.get(v)) {
if (!ch[nv]) {
answer++;
ch[nv] = true;
q.offer(nv);
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
graph = new ArrayList<ArrayList<Integer>>();
ch = new boolean[n+1];
for(int i=0; i<=n; ++i)
graph.add(new ArrayList<Integer>());
for(int i=0; i<k; ++i) {
int a = sc.nextInt();
int b = sc.nextInt();
graph.get(a).add(b);
graph.get(b).add(a);
}
bfs();
System.out.println(answer);
}
}