문제 URL : https://www.acmicpc.net/problem/18352
문제접근법 :
아래 사진과 같이
출발점에서 시작해서 depth가 k인 노드들을 출력해주면됩니다.
bfs로 풀면 매우 쉽게 풀리기때문에 큰설명은 필요없을듯 합니다. 그리고
depth가 k보다 커지면 더이상 탐색할 필요가 없겠죠?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
//By 콩순이냉장고
#include<bits/stdc++.h>
using namespace std;
int n, m, k, start;
vector<int> v[300001];
void input() {
cin >> n >> m >> k >> start;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
}
}
void bfs() {
queue<int> q;
q.push(start);
bool visit[300001] = { 0 };
visit[start] = 1;
int height = 0;
vector<int> res;
while (!q.empty()) {
int qsize = q.size();
while (qsize--) {
int cur = q.front();
q.pop();
if (height == k)
res.push_back(cur);
for (int next : v[cur]) {
if (visit[next] == 0) {
visit[next] = 1;
q.push(next);
}
}
}
height++;
if (height > k)
break;
}
if (res.empty()) {
cout << -1 << "\n";
return;
}
sort(res.begin(), res.end());
for (int node : res)
cout << node << "\n";
}
void solve() {
bfs();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
solve();
}
|
cs |
모르는점 혹은 궁금한점이나 논리적인 오류등 어떤 질문이라도 댓글은 환영입니다.
'백준' 카테고리의 다른 글
백준 1331 나이트 투어 (0) | 2021.07.02 |
---|---|
백준 2616 소형기관차 (0) | 2021.06.28 |
백준 12767 Ceiling Function (0) | 2021.06.25 |
백준 9935 문자열 폭발 (0) | 2021.06.24 |
백준 1269 대칭 차집합 (0) | 2021.06.23 |