From 5b6570806e12dc360e125bcadd57abfc4d55f950 Mon Sep 17 00:00:00 2001 From: InSange Date: Wed, 22 May 2024 01:39:34 +0900 Subject: [PATCH] =?UTF-8?q?2024-05-22=20=ED=8A=B9=EC=A0=95=20=EA=B1=B0?= =?UTF-8?q?=EB=A6=AC=EC=9D=98=20=EB=8F=84=EC=8B=9C=20=EC=B0=BE=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- InSange/BFS/18352.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++ InSange/README.md | 3 +- 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 InSange/BFS/18352.cpp diff --git a/InSange/BFS/18352.cpp b/InSange/BFS/18352.cpp new file mode 100644 index 0000000..defc4c1 --- /dev/null +++ b/InSange/BFS/18352.cpp @@ -0,0 +1,143 @@ +//#include +//#include +//#include +// +//using namespace std; +// +//int N, M, K, X, a, b, minDist; +//deque> town; +//queue q; +//vector dist; +//vector answer; +// +//void Solve() +//{ +// cin >> N >> M >> K >> X; +// N++; +// town.assign(N, deque()); +// dist.assign(N, -1); +// +// for (int i = 0; i < M; i++) +// { +// cin >> a >> b; +// +// town[a].push_back(b); +// } +// +// minDist = -1; +// q.push(X); +// dist[X] = 0; +// +// while (!q.empty()) +// { +// int cur = q.front(); +// q.pop(); +// +// for (const int& val : town[cur]) +// { +// if (dist[val] != -1) continue; +// q.push(val); +// dist[val] = dist[cur] + 1; +// minDist = max(minDist, dist[val]); +// } +// } +// +// for (int i = 0; i < dist.size(); i++) +// { +// if (dist[i] == K) answer.push_back(i); +// } +// +// if (answer.size()) +// { +// for (const int& val : answer) +// { +// cout << val << "\n"; +// } +// } +// else cout << -1; +//} +// +//int main() +//{ +// cin.tie(nullptr); +// ios::sync_with_stdio(false); +// +// Solve(); +// +// return 0; +//} + +#include +#include +#include + +using namespace std; + +int N, M, K, X, A, B; +int cur, dist; +queue> q; +vector> towns; +vector visited; +priority_queue, greater> answer; + +void Solve() +{ + cin >> N >> M >> K >> X; + + N++; + towns.assign(N, vector()); + visited.assign(N, false); + + while (M--) + { + cin >> A >> B; + towns[A].push_back(B); + } + + q.push({ X, 0 }); + visited[X] = true; + + while (!q.empty()) + { + cur = q.front().first; + dist = q.front().second; + + q.pop(); + + if (dist == K) + { + answer.push(cur); + continue; + } + + for (auto v : towns[cur]) + { + if (visited[v]) continue; + q.push({ v, dist + 1 }); + visited[v] = true; + } + } + + if (answer.empty()) + { + cout << -1; + return; + } + + while (!answer.empty()) + { + cout << answer.top() << "\n"; + answer.pop(); + } + return; +} + +int main() +{ + cin.tie(nullptr); + ios::sync_with_stdio(false); + + Solve(); + + return 0; +} \ No newline at end of file diff --git a/InSange/README.md b/InSange/README.md index f05e8fa..1240270 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -13,6 +13,7 @@ | 9차시 | 2024.04.12 | 힙 | [Top K Frequent Words](https://leetcode.com/submissions/detail/1180988760/) | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)] | 10차시 | 2024.04.30 | 스택 | [오아시스 재결합](https://www.acmicpc.net/problem/3015) | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/40)] | 11차시 | 2024.05.03 | DFS | [숫자고르기](https://www.acmicpc.net/problem/2668) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/41] -| 12차시 | 2024.05.08 | DP | [전깃줄](https://www.acmicpc.net/problem/2565) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)] +| 12차시 | 2024.05.08 | DP | [전깃줄](https://www.acmicpc.net/problem/2565) | [#12](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)] +| 13차시 | 2024.05.22 | BFS | [특정 거리의 도시 찾기](https://www.acmicpc.net/problem/18352) | [#13](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)] ======= ---