결혼식
백준 5427: 불
풀이
인접행렬
소스코드
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
boolean[][] arr = new boolean[N+1][N+1];
LinkedList<Integer> freind = new LinkedList<>();
for(int i=0; i<M; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
arr[a][b] = true;
arr[b][a] = true;
}
for(int i=1; i<=N; i++) {
if(arr[1][i]) {
freind.add(i);
for(int j=1; j<=N; j++) {
if(arr[i][j])
freind.add(j);
}
}
}
//중복제거
LinkedList<Integer> ans = new LinkedList<>();
for(int i=0; i<freind.size(); i++) {
int num = freind.get(i);
if(!ans.contains(num)) {
if(num!=1)
ans.add(num);
}
}
System.out.println(ans.size());
}
}