๋ฌธ์
ํด๋น ํฌ์คํ ์ ๋ฐฑ์ค์ 5567๋ฒ ๊ฒฐํผ์ ์ ์ ๊ทผ๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ค๋ช ํ ๊ธ ์ ๋๋ค.
์ ๋ต ์์ค ์ฝ๋๋ฅผ ํ์ธํ์๋ ค๋ฉด solve url ์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ด๋ค ๋ฐฉ์์ผ๋ก ์ ๊ทผํด์ผ ํ๋์ง๋ฅผ ๋จผ์ ์๊ฐํด๋ณด์.
๋ฌธ์ ์ ๊ทผ
์ด ๋ฌธ์ ๋ ์์ ๋ ธ๋๋ถํฐ ์ผ๋ง๋ ๋จ์ด์ ธ ์๋์ง๋ฅผ ํ์ธํ๊ณ ๋จ์ด์ง ๊ฑฐ๋ฆฌ๊ฐ 2 ์ด์ 3 ์ดํ๋ฉด count ๋ฅผ ์ฆ๊ฐ์ํค๋ฉด ๋๋ ๋ฌธ์ ์ด๋ค.
ํด๊ฒฐ๋ฒ
์์ ๋ ธ๋๋ถํฐ ๋ค๋ฅธ ๋ชจ๋ ๋ ธ๋๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๊ธฐ ์ํด BFS ๋ฅผ ์ด์ฉํ ์ ์๋ฐ.
์ ๋ต ์ฝ๋
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
String[] s = br.readLine().split(" ");
int from = Integer.parseInt(s[0]);
int to = Integer.parseInt(s[1]);
graph.get(from).add(to);
graph.get(to).add(from);
}
int[] depth = new int[n + 1];
Queue<Integer> queue = new LinkedList<>();
queue.add(1);
depth[1] = 1;
while(!queue.isEmpty()) {
int front = queue.remove();
for(int value : graph.get(front)) {
if(depth[value] == 0) {
depth[value] = depth[front] + 1;
queue.add(value);
}
}
}
int count = 0;
for (int i = 2; i < depth.length; i++) {
if(depth[i] == 2 || depth[i] == 3) {
count++;
}
}
bw.write(String.valueOf(count));
bw.flush();
bw.close();
}
}
๋๊ธ