fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define dbg(x) cout<<#x<<" : "<<x<<endl
  5. #define rep(i,a,b) for(int i=(a);i<=(b);i++)
  6. #define maxm 1000005
  7. #define maxn 100005
  8. #define inf 1000000000000000000
  9. vector<ll> edges[maxm];
  10. ll d[maxn],par[maxn],dau[maxn];
  11. ll n,m,s,t;
  12. vector<ll> res;
  13. void solve(ll destination){
  14. if(destination==par[destination]){
  15. res.push_back(destination);
  16. return;
  17. }
  18. res.push_back(destination);
  19. destination = par[destination];
  20. solve(destination);
  21. }
  22. void bfs(ll source){
  23. dau[source]=1;
  24. d[source]=0;
  25. queue<ll> q;
  26. q.push(source);
  27. while(!q.empty()){
  28. ll p = q.front();
  29. q.pop();
  30. for(auto v: edges[p]){
  31. if(dau[v]==1) continue;
  32. dau[v]=1;
  33. if(d[v]>d[p]+1){
  34. par[v]=p;
  35. d[v]=d[p]+1;
  36. }
  37. q.push(v);
  38. }
  39. }
  40. }
  41. int main(){
  42. // freopen("input.txt","r",stdin);
  43. // freopen("output.txt","w",stdout);
  44. cin>>n>>m>>s>>t;
  45. s--,t--;
  46. for(ll i=0;i<m;i++){
  47. ll u,v;
  48. cin>>u>>v;
  49. u--,v--;
  50. edges[u].push_back(v);
  51. }
  52. for(ll i=0;i<n;i++) sort(edges[i].begin(),edges[i].end());
  53. for(ll i=0;i<n;i++) d[i]=inf;
  54. par[s]=s;
  55. bfs(s);
  56. solve(t);
  57. reverse(res.begin(),res.end());
  58. for(auto v: res) cout<<v+1<<" ";
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 29184KB
stdin
Standard input is empty
stdout
0