File tree Expand file tree Collapse file tree 1 file changed +50
-5
lines changed Expand file tree Collapse file tree 1 file changed +50
-5
lines changed Original file line number Diff line number Diff line change 1
- // Authored by : BaaaaaaaaaaarkingDog
1
+ // Authored by : scsc3204
2
2
// Co-authored by : -
3
- // http://boj.kr/****************
3
+ // http://boj.kr/1dae319e478f4366b1fde2d55a31f55f
4
4
#include < bits/stdc++.h>
5
5
using namespace std ;
6
6
7
- int main (void ){
7
+ const int MX = 20'000 ;
8
+
9
+ vector<int > adj[MX + 2 ];
10
+ int dist[MX + 2 ], mx;
11
+
12
+ int main () {
8
13
ios::sync_with_stdio (0 );
9
14
cin.tie (0 );
10
-
11
- }
15
+
16
+ memset (dist, -1 , sizeof (dist));
17
+
18
+ int n, m; cin >> n >> m;
19
+
20
+ while (m--) {
21
+ int u, v; cin >> u >> v;
22
+ adj[u].push_back (v);
23
+ adj[v].push_back (u);
24
+ }
25
+
26
+ queue<int > q;
27
+ q.push (1 );
28
+ dist[1 ] = 0 ;
29
+
30
+ // bfs
31
+ while (!q.empty ()) {
32
+ int cur = q.front (); q.pop ();
33
+ for (int nxt : adj[cur]) {
34
+ if (dist[nxt] != -1 ) continue ;
35
+ dist[nxt] = dist[cur] + 1 ;
36
+ mx = max (dist[nxt], mx);
37
+ q.push (nxt);
38
+ }
39
+ }
40
+ int cnt = 0 ;
41
+ for (int i = 1 ; i <= n; i++) {
42
+ if (dist[i] == mx) {
43
+ if (!cnt) cout << i << ' ' ;
44
+ cnt++;
45
+ }
46
+ }
47
+ cout << mx << ' ' << cnt;
48
+ }
49
+ /*
50
+ BFS를 적용합니다.
51
+ dist 배열은 -1로 초기화해서 방문 배열의 역할도 수행합니다.
52
+
53
+ 모든 노드를 순회한 뒤 dist 배열을 확인하며
54
+ 가장 거리가 먼 헛간 번호와 그 거리,
55
+ 동일 거리에 있는 헛간 수를 출력합니다.
56
+ */
You can’t perform that action at this time.
0 commit comments