-문제-
https://www.acmicpc.net/problem/18352
-코드-
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
void bfs(const vector<vector<int>>& adj, int s, vector<int>& dist){
queue<int> q;
q.push(s);
dist[s] = 0;
while(!q.empty()){
int cur = q.front();
q.pop();
for(auto nxt: adj[cur]){
if(dist[nxt] != -1) continue;
dist[nxt] = dist[cur]+1;
q.push(nxt);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M, K, X;
cin >> N >> M >> K >> X;
vector<vector<int>> adj(N+1);
while(M--){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
vector<int> dist(N+1, -1);
bfs(adj, X, dist);
vector<int> answer;
for(int i = 1; i <=N; i++)
if(dist[i] == K) answer.push_back(i);
if(answer.empty()){
cout << -1 <<"\n";
}
for(int j : answer){
cout << j <<"\n";
}
return 0;
}
'IT > Algorithm' 카테고리의 다른 글
백준13565_침투 (0) | 2025.05.30 |
---|---|
백준5567_결혼식_bfs (0) | 2025.05.30 |
백준11724_연결 요소의 개수_bfs (0) | 2025.05.20 |
백준11725_트리의 부모 찾기 (0) | 2025.05.16 |
백준1260_DFS와 BFS (0) | 2025.05.16 |