Skip to content

Commit dda634c

Browse files
committed
[Silver II] Title: 결혼식, Time: 180 ms, Memory: 19872 KB -BaekjoonHub
1 parent c9180c4 commit dda634c

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Silver II] 결혼식 - 5567
2+
3+
[문제 링크](https://www.acmicpc.net/problem/5567)
4+
5+
### 성능 요약
6+
7+
메모리: 19872 KB, 시간: 180 ms
8+
9+
### 분류
10+
11+
그래프 이론, 그래프 탐색, 너비 우선 탐색
12+
13+
### 제출 일자
14+
15+
2025년 10월 4일 12:00:58
16+
17+
### 문제 설명
18+
19+
<p>상근이는 자신의 결혼식에 학교 동기 중 자신의 친구와 친구의 친구를 초대하기로 했다. 상근이의 동기는 모두 N명이고, 이 학생들의 학번은 모두 1부터 N까지이다. 상근이의 학번은 1이다.</p>
20+
21+
<p>상근이는 동기들의 친구 관계를 모두 조사한 리스트를 가지고 있다. 이 리스트를 바탕으로 결혼식에 초대할 사람의 수를 구하는 프로그램을 작성하시오.</p>
22+
23+
### 입력
24+
25+
<p>첫째 줄에 상근이의 동기의 수 n (2 ≤ n ≤ 500)이 주어진다. 둘째 줄에는 리스트의 길이 m (1 ≤ m ≤ 10000)이 주어진다. 다음 줄부터 m개 줄에는 친구 관계 a<sub>i</sub> b<sub>i</sub>가 주어진다. (1 ≤ a<sub>i</sub> < b<sub>i</sub> ≤ n) a<sub>i</sub>와 b<sub>i</sub>가 친구라는 뜻이며, b<sub>i</sub>와 a<sub>i</sub>도 친구관계이다. </p>
26+
27+
### 출력
28+
29+
<p>첫째 줄에 상근이의 결혼식에 초대하는 동기의 수를 출력한다.</p>
30+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.util.concurrent.atomic.AtomicBoolean;
4+
5+
public class Main {
6+
static int n, m, ans;
7+
static boolean[] visited;
8+
static List<Integer>[] friends;
9+
static StringTokenizer st;
10+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
public static void main(String[] args) throws Exception{
13+
inputSetting();
14+
dijka();
15+
System.out.println(ans);
16+
}
17+
18+
private static void dijka(){
19+
Deque<relationship> q = new ArrayDeque<>();
20+
q.add(new relationship(1, 0));
21+
visited[1] = true;
22+
23+
int next, now;
24+
relationship cur;
25+
while(!q.isEmpty()){
26+
cur = q.pop();
27+
now = cur.v;
28+
29+
for(int i = 0; i < friends[now].size(); i++){
30+
next = friends[now].get(i);
31+
32+
if(visited[next]) continue;
33+
if(cur.d + 1 <= 2){
34+
visited[next] = true;
35+
q.add(new relationship(next, cur.d + 1));
36+
ans++;
37+
}
38+
}
39+
}
40+
}
41+
private static void inputSetting() throws Exception{
42+
n = Integer.parseInt(br.readLine());
43+
m = Integer.parseInt(br.readLine());
44+
friends = new List[n + 1];
45+
visited = new boolean[n + 1];
46+
for(int i = 0; i < n + 1; i++){
47+
friends[i] = new ArrayList<>();
48+
}
49+
50+
int e, v;
51+
for(int i = 0; i < m; i++){
52+
st = new StringTokenizer(br.readLine());
53+
e = Integer.parseInt(st.nextToken());
54+
v = Integer.parseInt(st.nextToken());
55+
56+
friends[e].add(v);
57+
friends[v].add(e);
58+
}
59+
}
60+
61+
static private class relationship{
62+
int e, v, d;
63+
64+
relationship(int v, int d){
65+
this.v = v;
66+
this.d = d;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)