fork download
  1. #include<bits/stdc++.h>
  2. #define MAX 300001
  3. #define INF 1e9
  4.  
  5. using namespace std;
  6.  
  7. vector<int>adj[MAX];
  8. vector<int>city;
  9. int dis[MAX], k;
  10.  
  11. void dij(int i){
  12. fill(dis, dis+MAX, INF);
  13. queue<int>q;
  14. q.push(i);
  15. dis[i]=0;
  16.  
  17. while(!q.empty()){
  18. int x=q.front();
  19. q.pop();
  20.  
  21. for(int i=0;i<adj[x].size();i++){
  22. int nx=adj[x][i];
  23. if(dis[nx]>dis[x]+1){
  24. dis[nx]=dis[x]+1;
  25. q.push(nx);
  26. }
  27. }
  28. }
  29. }
  30.  
  31. int main(){
  32. ios_base::sync_with_stdio(NULL);
  33. cin.tie(0),cout.tie(0);
  34.  
  35. int n,m,x,a,b,cnt=0;
  36.  
  37. cin>>n>>m>>k>>x;
  38.  
  39. while(m--){
  40. cin>>a>>b;
  41.  
  42. adj[a].push_back(b);
  43. }
  44.  
  45. dij(x);
  46.  
  47. for(int i=1;i<=n;i++){
  48. if(dis[i]==k)
  49. city.push_back(i);
  50. }
  51.  
  52. sort(city.begin(), city.end());
  53.  
  54. if(city.empty())
  55. cout<<-1;
  56. else
  57. for(int i : city){
  58. cout<<i<<'\n';
  59. }
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0.01s 11704KB
stdin
4 4 1 1
1 2
1 3
2 3
2 4
stdout
2
3