

이 문제는 특정 노드와 연결되어 있는 노드의 개수를 세는 문제입니다.
BFS를 이용하여 시작정점에서 탐색을 시작해 인접 정점들을 모두 탐색하면 될 것 같습니다. 그냥 poll할때 마다 카운트를 찍어주면 됩니다! 짱쉬움
컴퓨터의 수가 주어지고, 두번째는 연결되어 있는 컴퓨터 쌍의 수가 주어집니다.
이후, 연결되어있는 컴퓨터 번호 쌍이 주어집니다.
1번 컴퓨터와 연결되어 있는 컴퓨터의 개수를 출력해주면 됩니다.
BFS는 시작 정점까지 포함해서 탐색하므로 자기 자신인 1을 제외하고 출력해주면 됩니다.
adjList리스트와 visited [ ] 배열은 인덱스와 정점에 적힌 숫자를 일치시켜
Collections.sort() 메서드를 이용해 각 정점에 연결된 인접 정점들을 정렬해줍니다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine()); // 정점의 수
int m = Integer.parseInt(br.readLine()); // 간선의 수
StringTokenizer st ;
LinkedList<Integer>[] adjList = new LinkedList[n+1];
boolean[] visited = new boolean[n+1];
for(int i=0; i<=n; i++){
adjList[i] = new LinkedList<Integer>();
}
for(int i=0; i<m; i++){
st = new StringTokenizer(br.readLine());
int v1 = Integer.parseInt(st.nextToken());
int v2 = Integer.parseInt(st.nextToken());
adjList[v1].add(v2);S
adjList[v2].add(v1);
}
for(int i=1; i<=n; i++){
Collections.sort(adjList[i]);
}
bfs(1,adjList,visited);
}
static void bfs(int v,LinkedList<Integer>[] adjList , boolean visited[]){
int count = 0;
Queue<Integer> q = new LinkedList<Integer>();
visited[v] = true;
q.add(v);
while(!q.isEmpty()){
v = q.poll();
++ count;
Iterator<Integer> iter = adjList[v].listIterator();
while(iter.hasNext()){
int w = iter.next();
if (!visited[w]){
q.add(w);
visited[w] = true;
}
}
}
System.out.println(count-1);
}
}